dbTalk Databases Forums  

Retrieve selected value from ListBox

comp.databases.ms-access comp.databases.ms-access


Discuss Retrieve selected value from ListBox in the comp.databases.ms-access forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
M Skabialka
 
Posts: n/a

Default Retrieve selected value from ListBox - 06-09-2010 , 09:38 AM






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

Reply With Quote
  #2  
Old   
Rich P
 
Posts: n/a

Default Re: Retrieve selected value from ListBox - 06-09-2010 , 10:21 AM






To retrieve the selected item from a listbox you can do this:

Private Sub List0_Click()
Dim i As Integer
i = List0.ListIndex
Debug.Print List0.Column(1, i + 1)
End Sub

Note: the ListIndex property begins counting at 0 and the column
property -- row parameter -- begins at 1 so you have to add 1 to the
listIndex value to retrieve the data from the correct row of the
Listbox. The parameters for the listbox are column#, row#.

Rich

*** Sent via Developersdex http://www.developersdex.com ***

Reply With Quote
  #3  
Old   
Douglas J. Steele
 
Posts: n/a

Default Re: Retrieve selected value from ListBox - 06-09-2010 , 10:29 AM



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.

--
Doug Steele, Microsoft Access MVP
http://www.AccessMVP.com/DJSteele
Co-author: Access 2010 Solutions, published by Wiley
(no e-mails, please!)

"M Skabialka" <mskabialka (AT) NOSPAMdrc (DOT) com> wrote

Quote:
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

Reply With Quote
  #4  
Old   
M Skabialka
 
Posts: n/a

Default Re: Retrieve selected value from ListBox - 06-09-2010 , 12:03 PM



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

"Douglas J. Steele" <NOSPAM_djsteele (AT) NOSPAM_gmail (DOT) com> wrote

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

--
Doug Steele, Microsoft Access MVP
http://www.AccessMVP.com/DJSteele
Co-author: Access 2010 Solutions, published by Wiley
(no e-mails, please!)

"M Skabialka" <mskabialka (AT) NOSPAMdrc (DOT) com> wrote in message
news:huo91h$fje$1 (AT) speranza (DOT) aioe.org...
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


Reply With Quote
  #5  
Old   
David W. Fenton
 
Posts: n/a

Default Re: Retrieve selected value from ListBox - 06-09-2010 , 04:43 PM



Is it really helpful to crosspost to microsoft.public.access and
CDMA?

--
David W. Fenton http://www.dfenton.com/
usenet at dfenton dot com http://www.dfenton.com/DFA/

Reply With Quote
  #6  
Old   
David Kaye
 
Posts: n/a

Default Re: Retrieve selected value from ListBox - 06-09-2010 , 06:51 PM



"M Skabialka" <mskabialka (AT) NOSPAMdrc (DOT) com> wrote:

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

Reply With Quote
  #7  
Old   
Douglas J. Steele
 
Posts: n/a

Default Re: Retrieve selected value from ListBox - 06-10-2010 , 04:58 AM



"David Kaye" <sfdavidkaye2 (AT) yahoo (DOT) com> wrote

Quote:
"M Skabialka" <mskabialka (AT) NOSPAMdrc (DOT) com> wrote:

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.
Sorry, David, but that's not correct. Selected does not refer to checkboxes
which you can turn on or off.

--
Doug Steele, Microsoft Access MVP
http://www.AccessMVP.com/DJSteele
Co-author: Access 2010 Solutions, published by Wiley
(no e-mails, please!)

Reply With Quote
  #8  
Old   
M Skabialka
 
Posts: n/a

Default Re: Retrieve selected value from ListBox - 06-11-2010 , 07:51 AM



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.

"David W. Fenton" <XXXusenet (AT) dfenton (DOT) com.invalid> wrote

Quote:
Is it really helpful to crosspost to microsoft.public.access and
CDMA?

--
David W. Fenton http://www.dfenton.com/
usenet at dfenton dot com http://www.dfenton.com/DFA/

Reply With Quote
  #9  
Old   
David W. Fenton
 
Posts: n/a

Default Re: Retrieve selected value from ListBox - 06-11-2010 , 06:29 PM



"M Skabialka" <mskabialka (AT) NOSPAMdrc (DOT) com> wrote in
news:hutbgr$u9i$1 (AT) speranza (DOT) aioe.org:

Quote:
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

--
David W. Fenton http://www.dfenton.com/
usenet at dfenton dot com http://www.dfenton.com/DFA/

Reply With Quote
  #10  
Old   
Douglas J. Steele
 
Posts: n/a

Default Re: Retrieve selected value from ListBox - 06-12-2010 , 12:31 AM



"David W. Fenton" <XXXusenet (AT) dfenton (DOT) com.invalid> wrote

Quote:
"M Skabialka" <mskabialka (AT) NOSPAMdrc (DOT) com> wrote in
news:hutbgr$u9i$1 (AT) speranza (DOT) aioe.org:

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
Or perhaps comp.databases.ms-access <g>

--
Doug Steele, Microsoft Access MVP
http://www.AccessMVP.com/djsteele
Co-author: "Access 2010 Solutions", published by Wiley
(no e-mails, please!)

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.