TextWidth() returns 0

ajgarrison

New Member
Joined
Jun 16, 2019
Messages
10
I have a fixed size text box for printing labels. I would like to change the font size based upon the length of text in the field.

I have tried various methods I found on the internet, but all return a zero for my width.
This is the latest I tried and I have this on the Detail section onFormat:

Dim lngTextHeight As Long
With Me.NameTxt
'Copy the original Font height from the Tag property
.FontSize = .Tag
Do While .FontSize > 2
lngTextHeight = fTextHeight(Me.NameTxt)

'Does text fit? If yes then exit
If lngTextHeight < .Height - 72 Then Exit Do

'Decrease font height
.FontSize = .FontSize - 1
Loop
End With

By the way I am using Access 2013.

Can anyone help me understand why ftextheight() or ftextwidth() always returns 0? My text box is 6" x 3" and the shortest text I have would be something like Prednisone Tablets USP, 20 mg.

Thanks in advance!
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
I don't see you have fTextWidth() in your code, but I don't think it matters. What you probably want to test for is the overall length of the text itself and not the box (which presumably never changes?). For that, use Len(Me.NameTxt.Value) and then change the font's size accordingly to ensure it always fits how you want.
 
Upvote 0
I thought I did. However, I have realized the text box is pulling from a value on the form. Having the code on the On Format is before the value is pulled. So, No - I did not have anything in the text box.
Live and learn...

I have everything working, just not correctly now.
Thanks to all for the help!
 
Upvote 0

Forum statistics

Threads
1,213,517
Messages
6,114,085
Members
448,548
Latest member
harryls

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top