In the sample you have 2 different comboboxes: cmbAnimal
and cmbSeqCarrier
. Which one are you using?
You have the right idea about using ItemData (assuming it stores the weight of the animal). In the sample below, ItemData
property holds the weights. If Text1
is the box where you enter the weight, the sample below should do the task you are asking.
'Load weights with the names to a combobox
Private Sub Form_Load()
cmbAnimal.AddItem "APPLE"
cmbAnimal.ItemData(cmbAnimal.NewIndex) = 2
cmbAnimal.AddItem "BEAR "
cmbAnimal.ItemData(cmbAnimal.NewIndex) = 100
cmbAnimal.AddItem "CAT"
cmbAnimal.ItemData(cmbAnimal.NewIndex) = 7
cmbAnimal.AddItem "DOG"
cmbAnimal.ItemData(cmbAnimal.NewIndex) = 20
cmbAnimal.AddItem "ELK"
cmbAnimal.ItemData(cmbAnimal.NewIndex) = 30
End Sub
If you have weights loaded into ItemData
, then this will work just fine:
Private Sub Text1_LostFocus()
Dim i As Integer
For i = 0 To cmbAnimal.ListCount - 1
' compare weight entered into text box with weights stored in the combobox
If Trim(cmbAnimal.ItemData(i)) = Trim(Text1.Text) Then
cmbAnimal.ListIndex = (i)
Exit For
End If
Next i
End Sub