Click to See Complete Forum and Search --> : type mismatch error


mbw
October 5th, 2001, 02:04 PM
Ok, I know why I'm getting this error but can't figure out how to fix it.

I'm trying to create a string of index values based on what is selected in a listbox. The listbox is populated with two values: The itemdata and an index value tied to the item data. For example, the user sees itemdata such as 'Harry' in the listbox which is tied to an index value corresponding to let's say Harry's customer id. I want to be able to create a string of these customer id values hidden within the listbox's index. I also need to create a string of the itemdata value's as well.Here's a short sample of my code that tries to pull out a string of itemdata and index data:


Dim index as string
Dim itemdata as string
Dim i as Integer
Dim first as Boolean
first = true
for i = 0 to (list1.ListCount - 1)
If (list1.Selected(i)) then
If (first) then
itemdata = "(" & list1.ItemData(i)
index = "(" & list1.ItemData(list1.ListIndex)(i)
first = false
else
itemdata = itemdata & "," & list1.Itemdata(i)
index = index & "," & list1.ItemData(list1.ListIndex)(i)
End If
End If
next i
itemdata = itemdata & ")"
index = index & ")"




This code is tied to a command button. If item values 'Harry', 'Mary', 'Bob' are selected the output should be a string of customerids like (1,4,6) for example. But everytime I run it I get a 'type mismatch' error due to trying to create the string of index values. I've never tried creating a string of values based on a listbox's index, only it's itemdata. Any help would be great! Thanks.

d.paulson
October 5th, 2001, 02:18 PM
index = "(" & list1.ItemData(list1.ListIndex)(i)

Take out the (i)

David Paulson

Craig Gemmill
October 5th, 2001, 02:22 PM
Ok Im not exactly sure what you are looking for but I think that replacing the lines from this:


List1.itemdata(List1.ListIndex)(i)

to this

List1.itemdata(i)




will solve your Type-Mismatch problem.

mbw
October 5th, 2001, 02:37 PM
thanks for the suggestion but it won't fix it.
changing
List1.itemdata(List1.ListIndex)(i)to List1.itemdata(i)
will not solve the problem. that solution will give what's selected in the listbox, not the index associated with it.

taking the (i) off the end of the list1.itemdata(list1.listindex) statement did get rid of the type mismatch error. the only problem is that the string created is composed of the last index value selected in the listbox (ie. 6,6,6). still need something in there that will keep track of the index values. any suggestions?

DSJ
October 5th, 2001, 02:44 PM
Isn't the ListIndex equal to your loop counter, i? just use it's value....

mbw
October 5th, 2001, 02:54 PM
i can't use the loop counter because the index value is not the same. i populated the listbox with data from a database. when i did this i created a newindex equal to a unique field value from the database. I want to be able to pull this index value out. in other words the index of the listbox is not ordered from 0. its ordered/indexed by the database field value instead.

d.paulson
October 5th, 2001, 06:19 PM
Hi
Try this. I think this is what your after.


Dim index As String
Dim itemdata As String
Dim i As Integer
Dim first As Boolean
first = True
For i = 0 To (List1.ListCount - 1)
If (List1.Selected(i)) Then
If (first) Then
itemdata = "(" & List1.List(i)
index = "(" & List1.itemdata(i)
first = False
Else
itemdata = itemdata & "," & List1.List(i)
index = index & "," & List1.itemdata(i)
End If
End If
Next i
itemdata = itemdata & ")"
index = index & ")"




David Paulson