PDA

View Full Version : populating combobox at runtime



garydp
09-23-2009, 02:52 PM
I have created a number of comboboxes that are created when a command button is pressed.

i used the following code to create the boxes

Dim UCLpar As Object

Set UCLpar = ActiveSheet.OLEObjects.Add _
(ClassType:="Forms.ComboBox.1", _
Link:=False, _
DisplayAsIcon:=False, _
Left:=782.25, _
Top:=170, _
Width:=45, _
Height:=18.75)

ok now this works fine except when i try to add code after this using the .additem and .value = "" to populate the combobox i get an error.

Run-time error '438':

Object doesnt support this property or method

when i populate using the dropbuttonclick event it works ok. this isnt going to work for me as the worksheet and comboboxes are created at runtime.

Bob Phillips
09-23-2009, 02:55 PM
You add to the combobox, not the container. i.e.



UCLpar.Object.AddItem "Bob"

garydp
09-23-2009, 03:00 PM
thanks for the quick reply but i get the same error

i also have 5 comboboxes that are created at the same time

GTO
09-23-2009, 03:47 PM
Your posted code, inclusive of XLD's, runs fine in 2003.

Sub AddCBox()
Dim UCLpar As Object
Set UCLpar = ActiveSheet.OLEObjects.Add _
(ClassType:="Forms.ComboBox.1", _
Link:=False, _
DisplayAsIcon:=False, _
Left:=782.25, _
Top:=170, _
Width:=45, _
Height:=18.75)

UCLpar.Object.AddItem "Bob"
End Sub


Where are you getting an error?

Mark

garydp
09-23-2009, 04:10 PM
i moved the code around and it now works thanks