Type Mismatch


Posted by Stacy on April 26, 2001 8:00 AM

Hello...I have created two list boxes and a button to add the selected items on the left(listbox1) to the list box on the right (listbox2). I copied the code from a book which is pasted below. I am receiving a 'type mismatch' error and I can't seem to find the problem. A friend suggested something about the active x controls....can someone please help??? Thank you very much in advance!!

Private Sub AddButton_Click()
If ListBox1.ListIndex = -1 Then Exit Sub
If Not cbDuplicates Then **(this is a check box)**
For i = 0 To ListBox2.ListCount - 1
If ListBox1.Value = ListBox2.List(i) Then
Beep
Exit Sub
End If
Next i
End If
ListBox2.AddItem ListBox1.Value ***(this is where the mismach is)***

End Sub

Posted by Jerid on April 26, 2001 9:49 AM

Hi Stacy,

What type of data do you have in ListBox1?

I copied your code into excel and added the controls onto a sheets and I didn't get the error. You do need to be careful because if you are filling ListBox1 with numbers, they get passed to ListBox2 as a string.

Jerid

Posted by Stacy on April 26, 2001 12:25 PM

Hey!! I detemined that my problem is becuase I allow the user to select 'multiple items' from list box1. If I change it back to Single, then it works fine. How do I set it so that the user can select multi items and still work??

Thanks!!

Posted by Jerid on April 26, 2001 1:05 PM

I use something like this.

'Find selected items
For i = 0 To ListBox1.ListCount - 1
ListBox1.ListIndex = i
If ListBox1.Selected(i) = True Then
'Add selected items to other list box
ListBox2.AddItem ListBox1.List(i)
End If
Next

'Deselect items once added
For i = 0 To ListBox1.ListCount - 1
ListBox1.Selected(i) = False
Next

'Reset ListBox
ListBox1.ListIndex = -1

Jerid

Posted by Stacy on April 26, 2001 1:20 PM


Hey Jerid thanks!! That worked...only one more small problem...when I move an Item from the left to the right, the list on the left jumps to the bottom. If I change the last line of your code to "ListBox1.ListIndex = 0", then it jumps to the top. How to I make it stay right where it was when the clicked the item...My list will eventually be very long, and it will be cumbersome to have to scroll back through to determine where I left off...Thanks so much!!

Stacy



Posted by Jerid on April 26, 2001 1:47 PM

Maybe this before the deselect section.

dim iCurLocation as integer
iCurLocation = ListBox1.ListIndex

'Deselect Section goes here

'Change the reset section to this
ListBox1.ListIndex = iCurLocation

Have fun.