I have a working VBA code which runs as:
wsEnd.Select
Range("A:AQ").Delete
strSQL = "Select *
strSQL = strSQL & " FROM [XXX].[ABCCustomer] As A"
strSQL = strSQL & " Left join"
strSQL = strSQL & " (Select * "
strSQL = strSQL & " From [XXX]..[ABCCustomer]"
strSQL = strSQL & " where LineageId = '123' ) B"
strSQL = strSQL & " on a.product = b.product and a.[StartDate] = b.[StartDate]"
strSQL = strSQL & " where (a.EndDate <> b.EndDate)"
strSQL = strSQL & " and a.NewEndDate is NULL AND B.NewEndDate IS NULL"
strSQL = strSQL & " and a.Id = '456"
strSQL = strSQL & " order by b.ProductType"
With ActiveSheet.ListObjects.Add(SourceType:=0, Source:=Array(Array( _
"ODBC;DRIVER=SQL Server;SERVER=XXX\SQL01;UID=;Trusted_Connection=Yes;APP=2007 Microsoft Office system;WSID=XXX;DATA" _
), Array("BASE=master")), Destination:=Range("$A$1")).QueryTable
.CommandText = strSQL
.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_XXX_C"
.Refresh BackgroundQuery:=False
End with,
I have two other scripts starting after the End With but inside the same sub all using the same VBA just different SQL, which all work perfectly fine.
Then I have this very annoying forth, which is causing my a real headache, that goes as follows:
strSQL = "Select *
strSQL = strSQL & " FROM [XXX].[ABCCustomer] As A"
strSQL = strSQL & " Left join"
strSQL = strSQL & " (Select * "
strSQL = strSQL & " From [XXX]..[ABCCustomer]"
strSQL = strSQL & " where Id = '123' ) B"
strSQL = strSQL & " on a.product = b.product and a.[StartDate] = b.[StartDate]"
strSQL = strSQL & " where (a.EndDate = b.EndDate)"
strSQL = strSQL & " and a.NewEndDate is Not NULL AND B.NewEndDate not NULL"
strSQL = strSQL & " and a.Id = '456"
strSQL = strSQL & " order by b.Product"
With ActiveSheet.ListObjects.Add(SourceType:=0, Source:=Array(Array( _
"ODBC;DRIVER=SQL Server;SERVER=XXX\SQL01;UID=;Trusted_Connection=Yes;APP=2007 Microsoft Office system;WSID=XXX;DATA" _
), Array("BASE=master")), Destination:=Range("$A$1")).QueryTable
.CommandText = strSQL
.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_XXX_D"
.Refresh BackgroundQuery:=False
End With End Sub
When running the code, the first three come back fine but the forth says "run time error 1004 general odbc error"
and stops the code at backgroundquery=false.
I have lifted the SQL code into SQL and it works perfectly fine there and have even tried running it on a seperate excel document and that doesnt give any joy.
The VBA is copy and pasted and only the list object table name is changed i.e. from C to D
I have tried to change backgroundquery:=false to background:=refresh, this works but I get a message saying "run time error 1004 This operation cannot be done because the data is refreshing in the background.
Please help!
Thanks Matt