Consulting

Results 1 to 5 of 5

Thread: populating combobox at runtime

  1. #1
    VBAX Regular
    Joined
    Sep 2009
    Posts
    57
    Location

    populating combobox at runtime

    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.

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    You add to the combobox, not the container. i.e.

    [vba]

    UCLpar.Object.AddItem "Bob"

    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    VBAX Regular
    Joined
    Sep 2009
    Posts
    57
    Location
    thanks for the quick reply but i get the same error

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

  4. #4
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    Your posted code, inclusive of XLD's, runs fine in 2003.
    [vba]
    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
    [/vba]

    Where are you getting an error?

    Mark

  5. #5
    VBAX Regular
    Joined
    Sep 2009
    Posts
    57
    Location
    i moved the code around and it now works thanks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •