VB6 Select combobox text value based on database data

Gerardo picture Gerardo · Aug 24, 2013 · Viewed 17.7k times · Source

I can't find a way to set a value to the ComboBox object based on the value retrieved from a DB. When I fill the comboBox, I use this code:

Do while Not rs1.EOF
   Cboneighborhood.AddItem rs1!Description
   Cboneighborhood.ItemData(CboBarrio.NewIndex) = rs1!Idneighborhood
Loop

When I retrieve data for an employee (Employee table has a field called IdNeighborhood) I want the combobox to set the text value that matches this ID.

I can't use the property

Cboneighborhood.Text 

'cause it's a 2-DropDown List type.

Your help would be really appreciated. Thanks in advance

Answer

Mike Weir picture Mike Weir · Aug 25, 2013

You just need to iterate through the items like this when you get a Value:

'Reset to no item.
Cboneighborhood.ListIndex = -1
Dim X As Integer
'Iterate through items.
For X = 0 To Cboneighborhood.ListCount - 1
    'Compare value.
    If Cboneighborhood.ItemData(X) = Value Then
        'Select it and leave loop.
        Cboneighborhood.ListIndex = X
        Exit For
    End If
Next X