I've been writing VBA in Excel 2003 for years, but my first attempt in 2007 has been frustrating. I'm trying to query Access, so I recorded the query. When I went to edit it and insert my variables for 2 fields, I found the syntax to be different than I was accustomed to. I can refresh the query from the menu, but when I try to rerun the code I get a message that "A table cannot overlap a range that contains a PivotTable report, query results, protected cells or another table." The recorded query is below.
With ActiveSheet.ListObjects.Add(SourceType:=0, Source:=Array(Array( _
"ODBC;DSN=MS Access Database;DBQ=C:\Users\awinterstein\Documents\GL_Data.accdb;DefaultDir=C:\Users\awinterstein\Documents;DriverId=25" _
), Array(";FIL=MS Access;MaxBufferSize=2048;PageTimeout=5;")), Destination:= _
Range("$A$1")).QueryTable
.CommandText = Array( _
"SELECT `Account Data 1) Pull Account Totals`.Account, `Account Data 1) Pull Account Totals`.FiscalYear, `Account Data 1) Pull Account Totals`.FiscalPeriod, `Account Data 1) Pull Account Totals`.Beginn" _
, _
"ingBalance, `Account Data 1) Pull Account Totals`.DebitAmount, `Account Data 1) Pull Account Totals`.CreditAmount" & Chr(13) & "" & Chr(10) & "FROM `C:\Users\awinterstein\Documents\GL_Data.accdb`.`Account Data 1) Pull Account To" _
, _
"tals` `Account Data 1) Pull Account Totals`" & Chr(13) & "" & Chr(10) & "WHERE (`Account Data 1) Pull Account Totals`.Account='1300-0028') AND (`Account Data 1) Pull Account Totals`.FiscalYear='2011')" _
)
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
.ListObject.DisplayName = "Table_Query_from_MS_Access_Database_1"
.Refresh BackgroundQuery:=False
End With
Bookmarks