This macro will return to a message box the SQL statement but how do I have it query the database using ODBC and return the results to the open worksheet?


Sub testSQLString()
Dim strSQL As String
Dim mySheet As Worksheet
Dim myRange As Range


Set mySheet = ThisWorkbook.Sheets("Sheet1")
mySheet.Activate

'If the variable is a number use:
Set myRange = mySheet.Range("A1")
strSQL = "SELECT cola, colb FROM Table WHERE cola = " &
CStr(myRange.Value)
MsgBox strSQL

'if the variable is a string use:
Set myRange = mySheet.Range("A2")
strSQL = "SELECT cola, colb FROM Table WHERE cola = '" & myRange.Value &
"'"

MsgBox strSQL

'NOTES:
'1. Excel is particular about having the sheet and range that we want
'to get data from be active, so you will want to navigate from where you are to the data cell as above.
'2. The SQL string must show numbers where we have numbers, and
'mystring' where we have string parameters.


End Sub