Populating combobox with SQL query prevents setting the value found in bound column

  • A activex combobox on a userform has been working with two columns, with the first column showing an item name and the second displaying the item code. The second column is set as bound and the item code is used elsewhere in the form. I populated it by setting the .RowSource = tablerange and the combobox displays the first column when an item is selected but the code in the bound column is what sets the value.

    I recently switched to populating it with a SQL query to an Access database rather than from a table in the excel file and use .List = Application.Transpose(var) using SQL query results , but this has caused it to stop working. The combobox populates fine but I can't select a value from VBA code. Previously, I could make one of the options become selected in the combobox by setting the value of the combobox (e.g. Combobox.Value = 123) to a number that was listed in the bound column (2) and it worked fine. Now, it gives me a run time error '380' "could not set the value property. Invalid property rule". However, if I select the item in the combobox and have the combobox.value returned (e.g., msgbox(Combobox.value)), it shows the value I am trying to select. So the value is there but for some reason I can't select the item using the code with VBA. I've tried changing the variable type of the value I'm trying to set to the combobo to variant and integer but still the same problem. Help is greatly appreciated!

  • FYI, it's more efficient to use:


    Code
    .Column = var

    rather than:

    Code
    .List = Application.Transpose(var)


    Anyway, can you post the full code you are using, as well as what the manually set properties for the combobox columncount, textcolumn and boundcolumn are?

    Rory
    Theory is when you know something, but it doesn’t work. Practice is when something works, but you don’t know why. Programmers combine theory and practice: nothing works and they don’t know why

  • Oh good to know, thanks, I've switched it to .Column. How is this different/more efficient, out of curiosity?


    for the combobox, boundcolumn is 2, textcolumn is 1. columncount is 2

    this is the function I use to get results from the database


    this is simplified for brevity but is the gist of what the code does that causes the problem. This worked find when I used a named range to populate the combobox.

    Code
    Public Sub ChangeChemList()
    Dim sampleid as Integer
    sampleid = 337
    
    SetupForm.ChemList.value = sampleid
    
    End Sub

    So if the item in the list has the id 337 and I do msgbox(SetupForm.Chemist.Value) it will display 337. But if I try to select this option by setting the value to 337 I get that error.

  • I think it's a bug. If you use List or Column to populate the control (rather than AddItem or RowSource), you can't assign a value to it directly. You'd need to find the value in the list and then set the ListIndex accordingly (remembering that that starts at 0 not 1).

    Rory
    Theory is when you know something, but it doesn’t work. Practice is when something works, but you don’t know why. Programmers combine theory and practice: nothing works and they don’t know why

  • Ok good to know thanks. Is there an optimal/most efficient way to do this? I need to pull the data from the access database but loading it into a spreadsheet first seems like it would be inefficient. Would iterating through the results from the query and doing additem be better?

  • I suppose it depends on how you know what ID you want to set the control to. It may be faster to just loop through the control's List to find the position of the value you want and then set the ListIndex.

    Rory
    Theory is when you know something, but it doesn’t work. Practice is when something works, but you don’t know why. Programmers combine theory and practice: nothing works and they don’t know why

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!