I’m revisiting my ListBox control work from these two articles:
Access ListBox Row Source and Type | Access JumpStart
ListBox Column size adjustments | Access JumpStart
The second article discsusses how to adjust column sizes using the ColumnWidths property and the Join and Split functions to place the values in an array, updating each column width in twips.
Gotta love twips. They are the default measurement in Access for spacing and sizing things like forms and controls. Go to the trusty Wikipedia to get more info than you’d ever want to know about what a twip is:
In my example code I am referencing two functions. Here’s a call to those functions getting the larger of my minimum desired twips and the longest string of characters in a particular column of the list:
Here I will discuss the function GetLongestStringTwips. I am passing in a column name.
Let’s look at the function:
The majority of this function is simply queries to get the longest string in the temporary table column with the name passed the function. Once we have the string with the greatest length (although to be fair, this isn’t necessarily the longest string… 3 W’s “WWW” are wider than 4 i’s “iiii”), but as a spitball approach we will get generally close using the string with the longest length in an average data set.
The longest text then gets sent to the HALutil.getTextTwips function to calculate and return the twips. Notice that we are passing many things to this library function which all affect the actual width of the string. The font, the font size, the font weight, whether the font is italicized and finally whether the font is underlined. All info we can pull from the control.
Let’s see what the library function looks like:
The kind wizard builders on the Microsoft Access team left us with a nice undocumented “Wizhook” object we can access with some nice functions in it, one of which returns twips of a text string.
Here is a link to the German site referred to in our comments. Use your browser to translate to your favorite language: Willkommen bei Team-Moeller.de – Wizhook-Objekt
That is the secret there. A nice black box hidden in the undocumented recesses of Access. Ooooh…
Given that funtion, we get the desired twips of the defined string and then end up adding another 150 twips to give us some right hand margin in the column.