Using Visual Basic 6.0, I'm trying to populate one listbox with another listbox's items.
Can anyone please explain why the following sub fails to work properly, getting a Type mismatch when passing two listboxes as parameters.
Public Sub copyListboxes(Byref lstFrom as Listbox, Byref lstTo as Listbox)
Dim i as integer
For i = 0 to lstFrom.Listcount -1
lstTo.Additem lstFrom.List(i)
Next i
End Sub
'Call copyListboxes(Listbox1,Listbox2) 'Type Mismatch ... ?
Thanks in advance.
I was able to reproduce your error by using Comboxes instead of Listboxes:
'1 form with:
' 1 command button: name=Command1
' 2 combobox controls: name=Listbox1 name=Listbox2
Option Explicit
Private Sub Command1_Click()
Call copyListboxes(Listbox1, Listbox2) 'Type Mismatch!
End Sub
Public Sub copyListboxes(ByRef lstFrom As ListBox, ByRef lstTo As ListBox)
Dim i As Integer
For i = 0 To lstFrom.ListCount - 1
lstTo.AddItem lstFrom.List(i)
Next i
End Sub
Private Sub Form_Load()
Dim intIndex As Integer
For intIndex = 1 To 10
Listbox1.AddItem CStr(intIndex)
Next intIndex
End Sub
To solve this you can either use Listbox controls or change your sub to work with Combobox controls:
Public Sub copyListboxes(ByRef lstFrom As ComboBox, ByRef lstTo As ComboBox)
Dim i As Integer
For i = 0 To lstFrom.ListCount - 1
lstTo.AddItem lstFrom.List(i)
Next i
End Sub