Figuring out selected item for list box without multiselect

  • Thread starter Thread starter BobRoyAce
  • Start date Start date
B

BobRoyAce

When the user clicks on an item in a list box I want to update other
controls on the form accordingly. When I enter the following code in the
Click event for the list box, it doesn't find a match for the selected item
(I selected second/last item in list of two items). In other words, it says
that Selected(0) and Selected(1) are both False. What am I missing?

---CODE BEGINS---

Private Sub lstUserNames_Click()
Dim i As Integer

' Figure out which user is selected
For i = 0 To lstUserNames.ListCount - 1
If lstUserNames.Selected(i) = True Then
m_sCurrentUserName = lstUserNames.Column(1, 0)
m_lCurrentUserID = GetUserIDForUserName(m_sCurrentUserName)

Exit For
End If
Next i
End Sub
 
If the listbox is set to a single value, then its value property will be the
value, in much the same way as a combo box.

e.g.
strValue = Me.lstUserNames.Value

If your listbox is multi-select then you should loop through the
ItemsSelected collection...
 
Back
Top