Link to home
Start Free TrialLog in
Avatar of goingolfn99
goingolfn99Flag for United States of America

asked on

Listbox Column Width

How do I, in VB, automatically change the column width in a list box going on what the maximum Len count for each column.  


Please refer to question Q_22046000 in the 5th posting. Thanks KRIS
Avatar of rockiroads
rockiroads
Flag of United States of America image

hello Kris, I dont know about resizing dynamically to length of text. I am not aware of any AutoResize type function

I guess u know how to do fixed widths though
e.g.


lstBucket.ColumnWidths = "2.54cm;3cm;1cm"
try this

    Dim rs As DAO.Recordset
    Dim strCol1 As String
    Dim strCol2 As String
    Dim strCol3 As String
    Dim dblWidth As Double

    'There are 1440 twips to and inch, 567 twips to a centimeter
    dblWidth = 125
    'Len to return the maximum number of characters in each of the three columns
    Set rs = CurrentDb.OpenRecordset("Select max( len(Field1)) as lenX From TableX")
    strCol1 = (rs!lenX * dblWidth)
    Set rs = CurrentDb.OpenRecordset("Select max( len(Field2)) as lenX From TableX")
    strCol2 = (rs!lenX * dblWidth)
    Set rs = CurrentDb.OpenRecordset("Select max( len(Field3)) as lenX From TableX")
    strCol3 = (rs!lenX * dblWidth)
    Set rs = CurrentDb.OpenRecordset("Select max( len(Field4)) as lenX From TableX")
    strCol4 = (rs!lenX * dblWidth)

    Me.listboxname.ColumnWidths = strCol1 & ";" & strCol2 & ";" & strCol3 & ";" & strCol4
    Me.Repaint



ah, it seems link I posted is what cap has posted
ASKER CERTIFIED SOLUTION
Avatar of rockiroads
rockiroads
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Well there u are. I didnt know how to do it before but now I know. Good question kris!!!
Again Rock!! Very cool
cheers Jeff, but I dont know if it works yet, I gave it a test, it seems ok. Wait for feedback from Kris
The usual issue with doing such as this is that
iiiiiiiiiiiiiiiiiiii
is a very different width to
wwwwwwwwwwwwwwwwwwww
but they contain the same number of characters.

Stephen Lebans has a solution (shock horror) which can be ammended to work for a Listbox with a bit of fudging.
http://www.lebans.com/autosize_textbox.htm
So it was enough then?
(Not trying to debunk Rocki's good work or anything - they don't call me Scully - and am always happy for him to have the points/credit in a question... as one day he might buy me a beer... yeah right! :-S)
But I'd have thought the limitation I mentioned would mean the result would be, at best, an average - an approximation of what the width needs to be.

Stephen Lebans' (he doesn't pay me to say this stuff) excellent work allows you to exactly predict such things - using a clever little virtual Device Context (which Access can't provide predictably enough for the API calls).
It's been around since like forever dude. (Well, used it in the native download 97 a while back anyway)
Leigh, I hadnt done col resizing before so 1st attempt. It seems to work okay, no problems with i's and w's
It may not be the greatest attempt though. And certainly what u have suggested, I aint seen that either.

And if your ever in the midlands, I'd be happy to buy u a beer, after you buy me 3 of course!!!

So is Dana your middle name then?
I'm a bit taller than her - and not as pretty.
Besides that we're like twins!..
(:-S)

OK - call me Fox instead.
Other's can debunk my work. :-)
Actually I envisaged you more like Skinner!
Not sure how much hair u got on top though
Less than I had a couple of years ago!  
(In more ways then one... Maiden fan remember? ;-)

Skinner??
Assistant Director or Principal? :-)
Neither particularly flattering.
Well we were talking about X-Files but if u liken yourself to Simpsons :)

Well Brucie had a haircut once!