简体   繁体   中英

Merge mysql recordset in vba

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM