I'm trying to return a data value from MySQL into a cell in Excel. There will only be a single row returned from SQL as the Where clause if against the Primary Key.
I can get the SQL to execute but don't know how to push the returned value into the Excel cell.
Sheet1 Cell C2 contains a postcode the a user can enter. I want to return a value of ABC from the database based upon the postcode into cell C5.
Here's my code:
Sub GetABCForPostcode()
Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset
t = "table1"
iParam1 = Sheets("Sheet1").Range("$C$2").Value
strCon = "Driver={MySQL ODBC 5.3 ANSI Driver};DATABASE=world;UID=root;PWD=MyPassword"
Set cn = CreateObject("ADODB.Connection")
cn.Open strCon
Set rs = CreateObject("adodb.recordset")
rs.Open "SELECT abc FROM " & t & " WHERE postcode = '" & iParam1 & "'", cn
End Sub
The SQL works and doesn't return any errors but I don't know how to get the returned value into cell C5??? Any ideas welcome.
Thanks