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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with listboxes

Status
Not open for further replies.

mage

Programmer
Jul 17, 2001
7
ZA
Hi!

I'm having a problem extracting ItemData values from a listbox into an array

for example:

say i have an existing listbox list1 with 20 entries.
i create an array as follows

redim array(list1.listcount) as variant

now list1.listcount should = 20,

the following code gives me error ( invalid index type):
for i = 0 to ubound(array)
array(i) = list1.itemdata(i)
next i

for the life of me I don't know what the problem is and I'v e spent at least 3 hrs trying to figure it out. I've checked to see that the listbox entries have itemdata values and they all do. your help is greatly appreciated

 
Hi there
I think I know what the problem is: see, ListCount returns 20, the numbers of items in your list. However, they are indexed 0 to 19, so when you try to access list1.ItemData(20), you get an error message, cause that one doesn't exist..
Try to run your for loop from 0 to ListCount-1, should work...
Good luck,

Bourgui
 
Thanks for the tip but i've tried that too

the problem is that the code doesn't work no matter what the index is, ie. it doesn't even get passed index i=1

 
Here's the code I used to simulate your environment
[tt]

Dim i As Integer
Dim arrMyArray() As Variant
ReDim arrMyArray(lstGroceries.ListCount)

For i = 0 To (lstGroceries.ListCount - 1)
arrMyArray(i) = lstGroceries.ItemData(i)
Next i
[/tt]

I then had an array with each element in my listbox (lstGroceries).

HTH
Joe Miller
joe.miller@flotech.net
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top