Hi everybody,
Can anybody teach me how this work? thnx.

private sub command1_click
dim vcombo1 as combobox,vcombo2 as combobox
call populate(vcombo1) 'there's an error here
vcombo2=vcombo1 'another error here
end sub

private sub populate(oCombo as combobox)
oCombo.clear
ocombo.additem "One"
ocombo.additem "Two"
ocombo.additem "Three"
end sub

'Basically what i want is populate combobox 1 then copy it into combobox 2.
NOTE: I don't want to put the combo box in a form.

Pls. Help.

vbnewguy

VBNewGuy or NewVBGuy (whichever you prefer),

First of all you need to have a ComboBox control somewhere, you cannot create an object variable that is not pointing to a specific instance of that object type.

Once this is done you can set your vcombo1 variable to point to that specific ComboBox, i.e. assuming your ComboBox is called Combo1
Set vcombo1 = Combo1

This should get rid of the first error message.

You could, should, also change your call statement from
Call populate(vcombo1)
To
populate vcombo1
This will allow you to use functions or subs with equal ease.

Next, your statement "vcombo2 = vcombo1" will not work since you are using object variables. When using object variables you have to use the "Set" keyword, i.e.
Set vcombo2 = vcombo1

However, this code will only make vcombo2 point to the exact same ComboBox as vcombo1 does and will NOT make a second ComboBox contain the same values as the first one.

In order to make a second ComboBox contain the same values as the first one you need a Sub that will copy the values one by one, just like your "populate" Sub does. Like so:

Private Sub CopyCombo(srcCombo As ComboBox, dstCombo As ComboBox)
   Dim i As Integer
   
   dstCombo.Clear
   For i = 0 To srcCombo.ListCount - 1
      dstCombo.AddItem srcCombo.List(i)
   Next
End Sub

Your full code would then look like in the project I have attached.

Hope this helps

Happy coding

Yomet

Hey man, it make sense. thanks a lot.
nwvbguy

VBNewGuy or NewVBGuy (whichever you prefer),

First of all you need to have a ComboBox control somewhere, you cannot create an object variable that is not pointing to a specific instance of that object type.

Once this is done you can set your vcombo1 variable to point to that specific ComboBox, i.e. assuming your ComboBox is called Combo1
Set vcombo1 = Combo1

This should get rid of the first error message.

You could, should, also change your call statement from
Call populate(vcombo1)
To
populate vcombo1
This will allow you to use functions or subs with equal ease.

Next, your statement "vcombo2 = vcombo1" will not work since you are using object variables. When using object variables you have to use the "Set" keyword, i.e.
Set vcombo2 = vcombo1

However, this code will only make vcombo2 point to the exact same ComboBox as vcombo1 does and will NOT make a second ComboBox contain the same values as the first one.

In order to make a second ComboBox contain the same values as the first one you need a Sub that will copy the values one by one, just like your "populate" Sub does. Like so:

Private Sub CopyCombo(srcCombo As ComboBox, dstCombo As ComboBox)
   Dim i As Integer
   
   dstCombo.Clear
   For i = 0 To srcCombo.ListCount - 1
      dstCombo.AddItem srcCombo.List(i)
   Next
End Sub

Your full code would then look like in the project I have attached.

Hope this helps

Happy coding

Yomet

My pleasure, if this solved your problem please mark the thread as solved.

Thx and happy coding

Yomet

This article has been dead for over six months. Start a new discussion instead.