TextWidth() returns 0

ajgarrison

New Member
Joined
Jun 16, 2019
Messages
8
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!
 

Some videos you may like

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.

JonXL

Active Member
Joined
Feb 5, 2018
Messages
302
Office Version
365, 2016
Platform
Windows
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.
 

ajgarrison

New Member
Joined
Jun 16, 2019
Messages
8
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!
 

Forum statistics

Threads
1,089,220
Messages
5,406,927
Members
403,113
Latest member
ms_excel_recal_or_die

This Week's Hot Topics

Top