Retrieve selected value from ListBox

M

M Skabialka

I have a ListBox called lstInventory, with two columns, a text value, and
the ID primary key for that value. When I click an item in the listbox I
would like to use that value to display details about that record based on
the ID. This code never shows any item as selected, i.e.
lstInventory.Selected(i) is always false - what is wrong with the code?

Private Sub lstInventory_Click()
Dim i As Integer
i = -1
Do Until i = Me.lstInventory.ListCount
i = i + 1
If lstInventory.Selected(i) Then Me!InventoryIDSelect =
Me.lstInventory.Column(1, i)
Loop
End Sub
 
D

Douglas J. Steele

Try:

Private Sub lstInventory_Click()

If Me!lstInventory.ItemsSelected.Count = 0 Then
MsgBox "Nothing's been selected."
Else
Me!InventoryIDSelect = Me!lstInventory.Column(1,
Me!lstInventory.ItemsSelected(0))
End If

End Sub

That'll give you the value from the second column of the selected row.
 
M

M Skabialka

Solved with:

Dim rst As dao.Recordset

Set rst = Me.RecordsetClone

rst.FindFirst "InventoryID = " & Me.lstInventory.Column(1)
Me.Bookmark = rst.Bookmark

rst.Close
Set rst = Nothing

I think I was trying to make something easy into something complicated.
Mich
 
D

David Kaye

M Skabialka said:
the ID. This code never shows any item as selected, i.e.
lstInventory.Selected(i) is always false - what is wrong with the code?

Because you're using Selected and not ItemData. Selected refers to the
checkboxes which you can turn on or off. ItemData refers to the item you
clicked.
 
D

Douglas J. Steele

David Kaye said:
Because you're using Selected and not ItemData. Selected refers to the
checkboxes which you can turn on or off. ItemData refers to the item you
clicked.

Sorry, David, but that's not correct. Selected does not refer to checkboxes
which you can turn on or off.
 
M

M Skabialka

After Microsoft announced the microsoft.public groups would be going away, I
wasn't sure where I was going to get answers, so cross posted. I see now
that probably won't be necessary. However I am not sure what you mean by
CDMA.
 
D

David W. Fenton

After Microsoft announced the microsoft.public groups would be
going away, I wasn't sure where I was going to get answers, so
cross posted. I see now that probably won't be necessary.
However I am not sure what you mean by CDMA.

comp.catabases.ms-access
 

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