Figuring out selected item for list box without multiselect

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
 
J

James Goodman

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...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top