Search code examples
excelvbacombobox

VBA select value in combobox using ColumnBound


I have a combobox in a userform, which is populated as follows:

query from database:

select name, id from employees

the result is stored in a two-dimensional array V:

aaa    231
bbb    244
ccc    301

and the initialization works perfectly as follows:

ComboBox1.list = V

The BoundColumn property is set to 2 (the ID). When the user selects a value, he sees the name, and the value returns the ID, as it should.

But when i try to set the value with

 ComboBox1.value = 244

I get the following error:

enter image description here


Solution

  • It's interesting. After doing some tests, I found that only the first column can be imported as the value of the combo box.

    The assignment of its value should be done with text, not value. This is because the value becomes the value of the bound column. Therefore, if you want to assign an id, you will have to import and assign data in the order of id and name in the query statement.

    id,name
    boundcolumn=1
    
    ComboBox1.text = 244