SQL Syntax Error? - Background Query= False

bs0d

Well-known Member
Joined
Dec 29, 2006
Messages
622
I'm using Excel (2010) to get data from a SQL database. I setup the sheet to allow user input before executing the query. But even when the query is hard-coded with static values I'm getting the following error:

Runtime Error '1004' - SQL Syntax Error

And it highlights this line in VBA:

Code:
.Refresh BackgroundQuery = False

If I comment out the line, the query doesn't execute. Anyone deal with this issue before? I'm sure it's something simple. Here's my query:

Code:
    With ActiveSheet.ListObjects.Add(SourceType:=0, Source:= _
        "ODBC;DSN=xxxx;Description=xxxxx;UID=me;APP=Microsoft Office 2010;WSID=DHVXBC1;DATABASE=database;Trusted_Connection=Yes" _
        , Destination:=Range("$B$9")).QueryTable
        .CommandText = Array( _
        "SELECT dbo.AC_PROPERTY.LEASE, dbo.AC_PROPERTY.MTR, dbo.AC_PROPERTY.SOMETHING, " _
        , _
        "avg(dbo.AC_DAILY.DATA), Avg(dbo.AC_DAILY.DATA2), avg(dbo.AC_DAILY.DATA3), Avg(dbo.AC_DAILY.DATA4), avg(dbo.AC_DAILY.DATA5), Avg(dbo.AC_DAILY.DATA6), dbo.AC_PROPERTY.LEASENO" _
        , _
        "FROM dbo.AC_DAILY INNER JOIN dbo.AC_PROPERTY ON dbo.AC_DAILY.PROPNUM = dbo.AC_PROPERTY.PROPNUM" _
        , _
        "WHERE (((dbo.AC_DAILY.D_DATE)>='1/1/2011' And (dbo.AC_DAILY.D_DATE)<'2/1/2011')) AND dbo.AC_PROPERTY.STATE = 'KS' AND dbo.AC_PROPERTY.CAT = '1'" _
        , _
        "GROUP BY dbo.AC_PROPERTY.LEASE, dbo.AC_PROPERTY.MTR, dbo.AC_PROPERTY.SOMETHING, dbo.AC_PROPERTY.LEASENO" _
        , _
        "HAVING avg(dbo.AC_DAILY.DATA) <= '50'" _
        )
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .BackgroundQuery = True
        .RefreshStyle = xlOverwriteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = False
        .RefreshPeriod = 0
        .PreserveColumnInfo = True
        .ListObject.DisplayName = "Query"
        .Refresh BackgroundQuery:=False
    End With
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
Wow, that came across in the macro record, good catch!

Now i've got some syntax errors to correct i guess. It's saying error near '.' and near 'dbo' ...
 
Upvote 0
Remove the colon.

I'm running into a problem with executing code before the query can return any results. I found a link where you can move the cursor to the end of the results (http://bytes.com/topic/access/answers/624404-how-do-you-make-vba-wait-query-finish like this:

Code:
' Open Recordset object with SQL statement. 
   Set rstOrders = dbsNorthwind.OpenRecordset(strSQL) 
  
   'This forces the query to finsh and moves the curson to the last record 
   retOrders.MoveLast 
  
   'Now I can continue execution of my code

Considering my above query, how do i set the reference to my query? so that I can use "moveLast" ?
 
Upvote 0
Does it? It must not be working; any ideas how to test to confirm? Because I'm also having a problem where the query is showing the column headings for all items in my GROUP BY clause (eventhough I tell it not to display column headings); but if I refresh the query, then they disappear.

So I tried to add a refresh to then end of the query code,
Code:
Selection.ListObject.QueryTable.Refresh BackgroundQuery:=False
and it gives an error:

object variable or with block variable not set.

But If I execute this independently after I can confirm the query has returned data, it works just fine, even wit the BackgroundQuery:= (that's how the macro records it) Hmmm.
 
Last edited:
Upvote 0
About 10 lines from the bottom you have:
Code:
.BackgroundQuery = True
Change it to False and that should fix it. Even though you have the 2nd line from the bottom set to False, change the other one to False also.

When set to True it will not wait for data to return from the query to start processing the rest of the code.
 
Upvote 0
I think it's working the way I would expect now, thanks for the help guys.
 
Upvote 0

Forum statistics

Threads
1,224,520
Messages
6,179,270
Members
452,902
Latest member
Knuddeluff

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