PDA

View Full Version : Combo box in Excel to query MySQL



azavgz
07-09-2011, 07:12 AM
Most posts relate to populating a combo box after with MySQL data. In my case, I wish to have a combobox in excel with preloaded values. When I select one of these values and I click OK with a command button, i would like this value to be part of the query sent to MySQL.

So far i have the combobox with the values, I can create a query to MySQL, but I cannot find the way to combine both, Here under the code to query MySQL without the combobox. When I select in the combobox the value 3 for example, I would like the query to be updated with the related value, thus returning only the data related to country_id=3

Src = "SELECT * FROM statistics where country_id=1"

Dim cntMyConnection As adodb.Connection
Set cntMyConnection = New adodb.Connection

CnctSource = "DRIVER={MySQL ODBC 5.1 Driver};Server=localhost;Database=secondattempt; Uid=root; PWD=P8195369x+;OPTION=3;"

cntMyConnection.Open ConnectionString:=CnctSource

Dim rstFirstRecordset As adodb.Recordset
Set rstFirstRecordset = New adodb.Recordset

rstFirstRecordset.Open Source:=Src, ActiveConnection:=cntMyConnection

i = 1
Worksheets("DatabaseConnection").Activate

With ActiveSheet
While Not rstFirstRecordset.EOF
.Cells(i, 1) = rstFirstRecordset(0).Value
.Cells(i, 2) = rstFirstRecordset(1).Value
rstFirstRecordset.MoveNext
i = i + 1
Wend
End With
Set rstFirstRecordset = Nothing
cntMyConnection.Close
Set cntMyConnection = Nothing
End Sub

Thanks for you input