I have an Excel 2000 file set up to query an Acess 2000 database. The code works fine, but now I need the ability to query the database table based on a variable. Below is my current code and where I set the variable and think the variable should be in the query string(highlighted in red):
Dim LastRow As Long
Worksheets("Sheet2").Select
ActiveSheet.Range("A1").Select
Selection.End(xlDown).Select
LastRow = ActiveCell.Row
For Each c In Worksheets("Sheet2").Range(Cells(1, 1), Cells(LastRow, 1))
strTerr = c.Value
Worksheets("Sheet1").Select
ActiveSheet.Range("A1").Select
With Selection.QueryTable
.Connection = Array(Array( _
"ODBC;DBQ=\\uscles015\docs\finance\RFS\SBT\SBT2007MastInvSched.mdb;DefaultDir=\\uscles015\docs\finance\RFS\SBT;Driver={Microsoft Acce" _
), Array( _
"ss Driver (*.mdb)};DriverId=25;FIL=MS Access;MaxBufferSize=2048;MaxScanRows=8;PageTimeout=5;SafeTransactions=0;Threads=3;UserCo" _
), Array("mmitSync=Yes;"))
.CommandText = Array( _
"SELECT tbl2007MastSchedFinal.Terr, tbl2007MastSchedFinal.TerrLeadNbr, tbl2007MastSchedFinal.Chain, tbl2007MastSchedFinal.Store, tbl2007MastSchedFinal.Date, tbl2007MastSchedFinal.`Inv Time`, tbl2007Mas" _
, _
"tSchedFinal.`AG or CFH`, tbl2007MastSchedFinal.`Inv Service`, tbl2007MastSchedFinal.Street, tbl2007MastSchedFinal.City, tbl2007MastSchedFinal.State, tbl2007MastSchedFinal.Zip, tbl2007MastSchedFinal.RE" _
, _
"GIONNAME, tbl2007MastSchedFinal.DSMNbr, tbl2007MastSchedFinal.FMNbr, tbl2007MastSchedFinal.RVPNbr, tbl2007MastSchedFinal.RLDNbr, tbl2007MastSchedFinal.RDDNbr, tbl2007MastSchedFinal.AE" & Chr(13) & "" & Chr(10) & "FROM `\\uscles0" _
, _
"15\docs\finance\RFS\SBT\SBT2007MastInvSched`.tbl2007MastSchedFinal tbl2007MastSchedFinal" & Chr(13) & "" & Chr(10) & "WHERE (tbl2007MastSchedFinal.Terr=& strTerr)" & Chr(13) & "" & Chr(10) & "ORDER BY tbl2007MastSchedFinal.Date, tbl2007MastSchedFinal.Chain, t" _
, "bl2007MastSchedFinal.Store")
.Refresh BackgroundQuery:=False
End With
Next c
strTerr is where my variable is stored and is a 4 digit code that is formatted as Text because strTerr could begin with a 0, ie 0214. When I run the code as given above, I get "SQL Syntax Error." I think I'm on the right track, but cannot figure out the correct syntax, any ideas?
Your help is greatly appreciated. Thanks
Bookmarks