Setting Combo Column Width via VBA (1 Viewer)

LanaR

Member
Local time
Today, 16:25
Joined
May 20, 2021
Messages
113
I'm currently playing with some code to deal with plurals in a combo box.

So depending on a text box that accepts a numeric value, the combo will show either singular or plural depending on the value in the text box. The code is
Code:
    If Me.Quantity.Value > 1 Then
        Me.Combo0.ColumnWidths = "0 ;0 ;2.542 cm"
    Else
        Me.Combo0.ColumnWidths = "0 ;2.542 cm ;0 "
    End If
and is fired in the form's On Current event and the Quantity field's On Lost Focus event

This works fine when the combo drops down. However, once the combo looses focus, the data displayed reverts to the Column widths based on the data in the first record when the form initially loads. So if the first record is one or less the combo will always display the singular despite what is in the dropdown list and visa a versa if the first quantity is greater than one.

Is there another property, that I've missed, that needs to be set to overcome this?
 

theDBguy

I’m here to help
Staff member
Local time
Yesterday, 23:25
Joined
Oct 29, 2018
Messages
21,473
Not sure I follow. Can you post some screenshot to clarify what you're seeing?
 

LanaR

Member
Local time
Today, 16:25
Joined
May 20, 2021
Messages
113
Better than that, here's a copy of the DB
 

Attachments

  • Combo Columns.accdb
    576 KB · Views: 63

June7

AWF VIP
Local time
Yesterday, 22:25
Joined
Mar 9, 2014
Messages
5,471
Might want to change field name Ingrdeient to Ingredient.

Don't think I would bother with this. I would probably just have the units use (s): Ounce(s), etc. Okay, Leaf(s) is not grammatically correct but how important is this for data entry? I might do calculation in report to return singular/plural based on value.

I did a quick test in Current event with an UNBOUND listbox in form header and column widths did change per value of Quantity and different column displayed.
 

LanaR

Member
Local time
Today, 16:25
Joined
May 20, 2021
Messages
113
@June7 Yes; I'm starting to agree. It was just something that I thought might be quite simple to implement. But it's starting to look as if the juice isn't worth the squeeze.
 

CJ_London

Super Moderator
Staff member
Local time
Today, 07:25
Joined
Feb 19, 2013
Messages
16,612
I would also argue 'gram' only applies when the quantity=1

to me' 0.5 gram' doesn't seem right - I would argue you would write '0.5 grams'

Not sure why the change of columns widths doesn't work as you expect , but suspect it might be something to do with retaining the setting on form open

Providing you are using a single form, you could just change the combo recordsource - change the column count to 2 and the columnwidths to 0cm (you can specify a width for the 2nd column if you want, but nor required)

Code:
Private Sub Quantity_LostFocus()
Const rSSql = "SELECT MeasureID, [] FROM TBL_Measures ORDER BY Measure"

    If Not IsNull(Me.Quantity) Then Combo0.RowSource = Replace(rSSql, "[]", IIf(Quantity > 1, "MeasurePlu", "Measure"))

End Sub

edit - no need to repeat the code for the current event, just put

Code:
Private Sub Form_Current()

    Quantity_LostFocus
    
End Sub
 

Users who are viewing this thread

Top Bottom