Must I use the Index Number To Select items in a listbox with code

  • Thread starter Thread starter Gary S
  • Start date Start date
G

Gary S

Hi,

If I want to select items in a list box using vba, must I use the index
number
to do that? I would like to be able to use bound item, not an index number.

This is how I am doing it currently:
Me.LstQryNames.Selected(39) = True

It would be nice to to be able to do this:
Me.LstQryNames.Selected("Canada") = True

I would greatly appreciate any help!
 
Yes, you have to use numbers.

You could always create a sub that accepts the text as input and loops
through the list box looking for that value:

Sub SetListboxSelect(InputValue As String)
Dim lngLoop As Long

For lngLoop = 0 To Me.LstQryNames.ListCount
If Me.LstQueryNames.ItemData(lngLoop) = InputValue Then
Me.LstQuerynames.Selected(lngLoop) = True
Exit For
End if
Next lngLoop

End Sub
 
Doug,

I can't thank you enough, works great, thanks
for coming up with a work around!
--
Thanks!
Gary S


Douglas J. Steele said:
Yes, you have to use numbers.

You could always create a sub that accepts the text as input and loops
through the list box looking for that value:

Sub SetListboxSelect(InputValue As String)
Dim lngLoop As Long

For lngLoop = 0 To Me.LstQryNames.ListCount
If Me.LstQueryNames.ItemData(lngLoop) = InputValue Then
Me.LstQuerynames.Selected(lngLoop) = True
Exit For
End if
Next lngLoop

End Sub
 
Back
Top