Sub RunQuery() Syntax Error using VBA

kkoruni

New Member
Joined
Jul 18, 2017
Messages
23
Good morning all,
I am running into a syntax error ( [datasource.error] microsoft sql: incorrect syntax near '>'. ) I tried multiple ways to fix this error before posting but I am at dead end.


Sub RunQuery()
Dim QueryText As String
QueryText = " "
QueryText = QueryText + "USE DataBaseName;"
QueryText = QueryText + "GO"
QueryText = QueryText + " "
QueryText = QueryText + "EXEC dbo.StoredProcedure"
QueryText = QueryText + "@BeginDate >= '" & ActiveSheet.Range("B3") & "' "
QueryText = QueryText + "@EndDate <= '" & ActiveSheet.Range("B4") & "' "
QueryText = QueryText + "GO"
QueryText = QueryText + " "
QueryText = QueryText + "select * from ##TempTableFromStoredProcedure"
ActiveWorkbook.Queries("Query1").Formula = _
"let" & Chr(13) & "" & Chr(10) & " Source = Sql.Database(""ServerName"", "" DataBaseName "", [Query=""" & QueryText & """, CreateNavigationProperties=false])" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " Source"

ActiveWorkbook.RefreshAll

End Sub
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
It looks like you are missing some spaces in there. Try outputting the QUeryText variable and you should see what I mean.
 
Upvote 0
It looks like you are missing some spaces in there. Try outputting the QUeryText variable and you should see what I mean.
Hello,
I am so sorry but the way i understand querytext variable is by using brackets {}. But, knowing where to use them I tried google and i read in microsoft's query-variable some way to use them but still failed.

QueryText = QueryText + "{@BeginDate >=} '" & ActiveSheet.Range("B3") & "' "
QueryText = QueryText + "{@EndDate <=} '" & ActiveSheet.Range("B4") & "' "

Any help RoryA is greatly appreciated.

Thank you.
 
Upvote 0
With these lines:

Code:
QueryText = QueryText + "EXEC dbo.StoredProcedure"
QueryText = QueryText + "@BeginDate >= '" & ActiveSheet.Range("B3") & "' "

there is no space between the end of the word StoredProcedure and the start of the parameter name. Also note that it is safer to use & for concatenation rather than +
 
Upvote 0
With these lines:

Code:
QueryText = QueryText + "EXEC dbo.StoredProcedure"
QueryText = QueryText + "@BeginDate >= '" & ActiveSheet.Range("B3") & "' "

there is no space between the end of the word StoredProcedure and the start of the parameter name. Also note that it is safer to use & for concatenation rather than +
Hi RoryA,
I am not seeing where I am wrong. I am very sorry I am new to vba and I am failing miserly. I am completely lost.
I still get error even if I remove the GO. Can you please help me fix this syntax issue?

Sub RunQuery()
Dim QueryText As String
QueryText = " "
QueryText = QueryText & "USE DataBaseName;" & " "
QueryText = QueryText & " "
QueryText = QueryText & "EXEC dbo.StoredProcedure"
QueryText = QueryText & " "
QueryText = QueryText & " @BeginDate >= '" & ActiveSheet.Range("B3") & "' "
QueryText = QueryText & " @EndDate <= '" & ActiveSheet.Range("B4") & "' "
QueryText = QueryText & " "
QueryText = QueryText 7 "select * from ##TempTableFromStoredProcedure"
 
Upvote 0
If you hardcode the parameters in your query, does it actually run in power query?
 
Upvote 0

Forum statistics

Threads
1,224,825
Messages
6,181,189
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top