Well I can say for certain the syntax is correct because
1) the error I receive is "Run-time error '1004': Application-
defined or object-defined error" not a sql syntax error and
2) the way the process works is that I create the queries using
Query Analyzer (a SQL Server query tool), test it in that
environment and then just paste the query into the textbox.
3) I use the msgbox approach to edit errors and have already checked
that.
That said, I'm happy to hear you've used SQL variable naming
conventions in a VBA procedure. It gives me hope that this problem
can be corrected.
This is my code below. I'm using a QueryTable so maybe that is the
problem. What method do you use?
FYI - the error is on the 'oQT.Refresh' and like I've said, if I
just take out the '@'+variable name and replace the variables with
constants the process works as intended.
Sub CreateQT3()
Dim sConn As String
Dim sSql As String
Dim oQt As QueryTable
Dim intdate As Long
Dim strOut As String
Dim shtOut As String
Dim wkbkOut As String
Dim wkbk As Workbook
Set wkbk = ActiveWorkbook
wkbkOut = Sheet3.Range("AJ27").Value
shtOut = Sheet3.Range("AJ28").Value
strOut = Sheet3.Range("AJ29").Value
sConn = "ODBC;DRIVER=SQL
Server;SERVER=xxxxxxxx;UID=xxxxxxx;Trusted_Connection=Yes"
sSql = Sheets("Queries").AccessQry.Text
Set oQt = Workbooks(wkbkOut).Sheets(shtOut).QueryTables.Add
(Connection:=sConn, _
Destination:=Sheets(shtOut).Range(strOut), Sql:=sSql)
With oQt
.Name = "QueryResults"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = False
.RefreshStyle = xlOverwriteCells
.SavePassword = True
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 1
.PreserveColumnInfo = False
End With
oQt.Refresh
oQt.Delete
End Sub