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:
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:
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