0
votes

I want to use combobox selected value as query to pull another data from mysql database. Let's say Combobox selected value as id. then I will use this id to pull another details related to this id such as supplier, cost etc. to display in label option. I used following code but it doesn't work.

sqlQa = "select Description from matcat_select where BOF like 'MAIN';"

rs.Open sqlQa, oConn, adOpenStatic

With rs

    'Set .ActiveConnection = Nothing 'Disconnect the recordset.
        k = .Fields.Count
     'Populate the array with the whole recordset.
    vaData = .GetRows
End With

'Manipulate the Combobox's properties and show the form.

With UserForm1
    With .ComboBox1
        .Clear
        .BoundColumn = k
        .List = Application.Transpose(vaData)
        .ListIndex = -1
    End With
End With

'Manipulate the Combobox's properties and show the form

Set rs1 = CreateObject("ADODB.Recordset")

sqlQb = "Select EOF From matcat_select Where Description = '" & ComboBox1.Value & "';"

rs1.Open sqlQb, oConn

While Not rs1.EOF

Label6.Caption = rs1("EOF")

rs1.MoveNext

Wend

Please check my code and correct me if I am wrong. Also, each time I made query in mysql via vba am I need to establish a connection?

1

1 Answers

0
votes
If Not rs1.EOF Then

Label6.Caption = rs1("EOF")

Else

Label6.Caption = ""

End If

Try this

While Not rs1.EOF
 Label6.Caption = rs1("EOF")
 rs1.MoveNext
Wend

For the select statement

sqlQb = "Select EOF From MyTable Where Description = '" & ComboBox1.Value & "';"

ComboBox is not a text box. To get the selected value, use .Value method.

Yes you need a connection when you want to execute any queries, your VBA is not a database itself, it doesn't knows what you want.

recordset.Open Source, ActiveConnection, CursorType, LockType, Options

http://msdn.microsoft.com/en-us/library/windows/desktop/ms675544(v=vs.85).aspx