Click to See Complete Forum and Search --> : Loop through Combobox?


nolc
May 30th, 2003, 11:24 AM
Does anyone know how to loop through a combobox to find an item's text and once it is found...select it? My code below is not working....thanks in advance!


Public Sub FindCboItem(ByVal text As String, ByRef cbo As ComboBox)
Dim i As Integer
MsgBox(cbo.Items.Count)

For i = 0 To cbo.Items.Count - 1
MsgBox(cbo.Items.Item(i).ToString & " = " & text)

If cbo.Items.Item(i).GetType.ToString = text Then
cbo.SelectedItem = cbo.Items.Item(i)
Exit For
End If
Next
End Sub

Stephanieb
May 30th, 2003, 02:48 PM
apologies if I have misunderstood your question, but can't you just set it like:

cbo.text = text

looping and selecting should not be needed this way.....

Stephanie :)

nolc
May 30th, 2003, 02:50 PM
I just figured it out...and this is how you do it........ :D


Public Sub FindCboItem(ByVal text As String, ByRef cbo As ComboBox)
Dim i As Integer
For i = 0 To cbo.Items.Count - 1
cbo.SelectedIndex = i
If cbo.SelectedValue = text Or cbo.Text = text Then Exit For
Next
End Sub