Hi All,
I wasn't sure if this was the right forum for this but here goes. I'm using Excel VBA to create a database cell query that connects to SQL Server and returns the result to the active cell (the result is always a single value).
The idea is that all of the WHERE clauses in the SQL are controlled by dropdown lists down column A and across the top row of maybe 20 columns of an Excel sheet. I'm a complete beginner when it comes to SQL and am not sure of the correct syntax for inserting VBA variable names into SQL.
My problem:
It just don't work! It falls over with a runetime error 13 when it tries to execute the SQL. I've pasted my SQL string below and an example of how I have dimmed and worked out my variables. But I think the problem lies with the syntax of concatenating SQL and VBA variable names (ie. a comma in the wrong place).
Any help or advice, or even a prod in the right direction would be much appreciated.
Roy
Dim measureIDv As Variant
measureIDv = Cells(iRow, iCol - iCol + 1).Value
'DON'T WORK!
mySQLString = Array("SELECT PISummary.MeasureValue" & Chr(13) & "" & Chr(10) & "FROM HiPPOi_Test.dbo.PISummary" & Chr(13) & "" & Chr(10) & "WHERE PISummary.ActiveLevel ='" & activeLevelv & "'" & Chr(13) & "" & Chr(10) & "AND PISummary.MeasureID ='" & measureIDv & "'" & Chr(13) & "" & Chr(10) & "AND PISummary.MeasureDesc ='" & measureDescv & "'" & Chr(13) & "" & Chr(10) & "AND PISummary.YTDMonthFlag ='" & YTDMonthFlagv & "'" & Chr(13) & "" & Chr(10) & "AND PISummary.MonthText ='" & monthTextv & "'")
I wasn't sure if this was the right forum for this but here goes. I'm using Excel VBA to create a database cell query that connects to SQL Server and returns the result to the active cell (the result is always a single value).
The idea is that all of the WHERE clauses in the SQL are controlled by dropdown lists down column A and across the top row of maybe 20 columns of an Excel sheet. I'm a complete beginner when it comes to SQL and am not sure of the correct syntax for inserting VBA variable names into SQL.
My problem:
It just don't work! It falls over with a runetime error 13 when it tries to execute the SQL. I've pasted my SQL string below and an example of how I have dimmed and worked out my variables. But I think the problem lies with the syntax of concatenating SQL and VBA variable names (ie. a comma in the wrong place).
Any help or advice, or even a prod in the right direction would be much appreciated.
Roy
Dim measureIDv As Variant
measureIDv = Cells(iRow, iCol - iCol + 1).Value
'DON'T WORK!
mySQLString = Array("SELECT PISummary.MeasureValue" & Chr(13) & "" & Chr(10) & "FROM HiPPOi_Test.dbo.PISummary" & Chr(13) & "" & Chr(10) & "WHERE PISummary.ActiveLevel ='" & activeLevelv & "'" & Chr(13) & "" & Chr(10) & "AND PISummary.MeasureID ='" & measureIDv & "'" & Chr(13) & "" & Chr(10) & "AND PISummary.MeasureDesc ='" & measureDescv & "'" & Chr(13) & "" & Chr(10) & "AND PISummary.YTDMonthFlag ='" & YTDMonthFlagv & "'" & Chr(13) & "" & Chr(10) & "AND PISummary.MonthText ='" & monthTextv & "'")