Add Item and Index to combobox

Asked

Viewed 155 times

0

Hello, I would like to insert in a combobox the Item and the index of that Item.

The item (function_name) I do through a query in a database and I wanted the index to be the working id_function.

I’ve tried several ways and I haven’t been able to.

Note: The error it causes is "It was expected: = "

Follow the Code below.

Private Sub UserForm_Initialize()


Dim rs As ADODB.Recordset
Dim cn As ADODB.Connection
Dim TotalCursos As Variant

'Parametrização do Banco de Dados
Set cn = New ADODB.Connection
strDB = ThisWorkbook.Path & "\SistemaGerenciamento.accdb"
cn.ConnectionString = _
          "Provider=Microsoft.ACE.OLEDB.12.0;" & _
          "Data Source=" & strDB & ";"
cn.Open


strSQL = "SELECT id_funcionario, nome_funcionario FROM tbl_funcionario"
 
Set rs = cn.Execute(strSQL)
    
    Do While Not rs.EOF
        
        cmb_funcionario.AddItem (rs("nome_Funcionario"),rs("id_funcionario"))
        
    rs.MoveNext
    
    Loop

cn.Close

End Sub

1 answer

1


In this line:

cmb_funcionario.AddItem (rs("nome_Funcionario"),rs("id_funcionario"))

Remove the parentheses, Window adc. the comma but placed inside the parentheses making the Additem method understand as only a parameter. Now the error about: "It was expected: = " means that Voce is doing an operation between (rs("function_name"),rs("function_id_work")). And I don’t understand what you want with the vigula and it returns the error.

Soon the expected should be:

cmb_funcionario.AddItem rs("nome_Funcionario"),rs("id_funcionario")

Note that Index is only the order the items appear in the combobox. And depending on the order the records were registered can cause inconvenience to the user.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.