Full Version: Autosize Button Text
UtterAccess Forums > Microsoft® Access > Access Forms
Hi guys,
I have a form on which are several buttons. These particular buttons get their captions from a DLookUp function performed on a table when the form opens.
The buttons must remain the same size, due to the layout of the form, but the text retrieved is different lengths.
Is there any way that I can use VBA to automatically resize the font/caption in order to fit the button properly?
(Using Access 2000 still )
Thanks in advance.
That should be do-able, though you'll have to create the code that automates the process. I would recommend creating a subroutine that:
1. Takes the caption text and the name of the button control as arguments,
2. Retrieve the .Height and .Width properties of the button,
3. Measure the length of the text,
4. Maximize the font size based on how much space is taken up vs. how much is available.
Obviously, the hard part will be #4, especially if your button font is proportional, like Arial. There's an article at the Microsoft Knowledge Base on how to limit how many characters can fill a textbox. I'm thinking there must be code in there that can calculate the size taken up by a string, even with a proportional font. Once you adapt that code for your needs, you should be well on your way.
On the other hand, if you use a non-proportional font for your buttons, like Courier New, you can more easily estimate how many letters will fit across a button, since each letter takes up the same amount of space.
Hope this helps,
This is a "lo-fi" version of UA. To view the full version with more information, formatting and images, please click here.