PDA

View Full Version : Solved: Listbox items



Touni102
05-28-2009, 01:46 PM
This seems like a simple thing to do but I can't seem to find the answer... There is a property to return the list of selected items from a list box (ListBox1.ItemsSelected), but is there a property for returning the entire list of items? Unselected included? I'm using a "For Each varItem In ListBox1" but .Items doesn't work...

Movian
05-29-2009, 05:31 AM
I tried to do this very thing last week and was unable to do it in this manner.
I ended up using a dynamic Sql query to give me a record set with the same items in it. Then i used that record set as my list. This was the only way i could achieve the same result.

You can find that thread here (http://www.vbaexpress.com/forum/showthread.php?t=26431)

OBP
05-29-2009, 07:13 AM
This returns all items in the List box called list0.
You can do what you like with instead of just showing them in the msgbox.

Private Sub Command2_Click()

Dim ctlList As Control, varItem As Variant

' Return Control object variable pointing to list box.
Set ctlList = Me.List0
' Enumerate through selected items.
For varItem = 0 To ctlList.ListCount - 1
' Print value of bound column.
MsgBox ctlList.ItemData(varItem)
Next varItem



End Sub

Touni102
06-01-2009, 08:42 AM
OK, thanks all. I know the option of just manually counting through the list, but I was wondering if there was some property like .ListItems or something that returns what I need. My guess is there isn't...

OBP
06-01-2009, 10:10 AM
Didn't you read my post???

Touni102
06-02-2009, 09:11 AM
I read your post, within your code you manually enumerated through the list using the indexes. Right now i have to resort to using this option, which I was already aware of. What I was originally asking was a property of the ListBox that returns the full list of items. Just like there is a property .ItemsSelected that returns the list of selected items. I'm just confused why there wouldn't be a property to get the items in the list, and only a property to return the selected items.

CreganTur
06-02-2009, 12:06 PM
read your post, within your code you manually enumerated through the list using the indexes. Right now i have to resort to using this option, which I was already aware of. What I was originally asking was a property of the ListBox that returns the full list of items. Just like there is a property .ItemsSelected that returns the list of selected items. I'm just confused why there wouldn't be a property to get the items in the list, and only a property to return the selected items.

Tony's code is acutally enumerating through the ItemData collection of the listbox. Generally,the only way to see all data within a collection is to loop through it, just like he did. There is no single property (that I am aware of) that will return all data within a listbox, and that's because all of that data is held in a collection.

It may not be what you want, but it's the best way to do what you want.

Touni102
06-02-2009, 12:36 PM
I ask because i'm familiar with C# where you can get the items and use "for each string str in List.Items" or something like that. I saw that it was possible to use "For Each varItem In ListBox1.ItemsSelected" so i wondered about a property for the entire list of items instead of just the Selected ones.