Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Cycle through listbox and get values

Status
Not open for further replies.

ghloid

IS-IT--Management
Mar 11, 2002
85
US
Trying to do a loop here to cycle through a list box and get the value at each index in the list. For some reason it's not working though. Here's the code:

Private Sub Command9_Click()
Dim Test As Integer
Dim Choice As String
Dim Y As Integer
'get the total number of items in the list box here
Test = Me.lstQueryList.ListCount
' start the loop to cycle through and get the values
For Y = 0 To (Test - 1)
' select the first value in the list box
lstQueryList.Selected(Y) = True
' assign the value to a variable
Choice = lstQueryList.Value
' prompt the variable in a mesage box
MsgBox "it is " & Choice
' deselect the item
lstQueryList.Selected(Y) = False
' go to the next item
Next Y
End Sub

It's weird, because when I'm viewing the text box on the form, I can see it cycling through the list properly. Howver, my message box keeps popping up with the first value from the list and does not progress like I would expect.

Surely, there's an easy way to do this, and I'm just to tire to get it right now.

Any help (as always) is greatly apprectiated.

THANKS!!
 
HI

Dim ctl As ListBox
Dim varItm As Variant

Set ctl = lstAddressNotSelected
For Each varItm In ctl.ItemsSelected
'ctl.ItemData(varItm) is the selected item
' do whatever you want here

Next varItm Regards

Ken Reay
Freelance Solutions Developer
Boldon Information Systems Ltd
Website needs upgrading, but for now - UK
 
Doesn't seem to be working right. It keeps reporting that the list box is null. Here's what I did:

Private Sub Command9_Click()
Dim Ctl As ListBox
Dim VarItm As Variant

Set Ctl = lstQueryList
For Each VarItm In Ctl.ItemsSelected
MsgBox "it is " & Ctl.ItemData(VarItm)
Next VarItm

End Sub

Is this right? I set the Ctl variable = to the listbox control on the form. I thought that's what you were referring to in your code above.

The listbox on my form gets its row source populated via a query. Then, once the listbox is populated, I want the code above (or some variation of it) to cycle through the values in the listbox and perform a function for each value For this instance, a msgbox should pop up stating the value that is currently selected by the code.

So, for example, let's say the lisbox is populated with 4 values from it's rowsource query - Blue, Yellow, and Red. The code enacts on the button click, and the user should see:
it is Blue
it is Yellow
it is Red

in continuous message boxes that pop up. The listbox is hidden from the user at this point, so the user never actually gets to physically select from it themselves.

I think that sums it up. What am I doing wrong?

Any more help?
 
GOT IT!!! I used a little of your code and mine, and it seems to be doing exactly what I want it to do. Here it is (in case you're interested):

Dim Ctl As ListBox
Dim VarItm As Variant
Dim X As Integer

'start a loop of all the items in the listbox
For X = 0 To (Me.lstQueryList.ListCount - 1)
'select each item one at a time here
Me.lstQueryList.Selected(X) = True
'run your code to get the value
Set Ctl = Me.lstQueryList
For Each VarItm In Ctl.ItemsSelected
MsgBox "it is " & Ctl.ItemData(VarItm)
Next VarItm
'progress to next item
Next X
End Sub

Seems to work nicely. I'll let you know if it doesn't.

THANKS!!
 
Hi

Sorry, I miss-understood, I thought you wanted to pick out the selected item(s), if you are going to pick them all out, you could just iterate through the recordset, foremed by the query on which the list box is based, or do as you have done. Regards

Ken Reay
Freelance Solutions Developer
Boldon Information Systems Ltd
Website needs upgrading, but for now - UK
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top