Home Forums SQL Server 7,2000 T-SQL Incorrect syntax near the keyword ''IN'' error RE: Incorrect syntax near the keyword ''''IN'''' error

  • That's not a query table though... It can be cumbersome going through a recordset record by record. This is the VBA to add a query table. You need to put your DSN name in and obviously change the command text to the sql you want to use.

    Public Sub CreateQueryTable()

        With ActiveWorkbook.ActiveSheet.QueryTables.Add(Connection:= _

            "ODBC;DSN=YOURDSNNAME" _

            , Destination:=Range("A1"))

            .CommandText = Array("SELECT * FROM TESTTABLE")

            .Name = "WHATEVERQUERYNAMEYOULIKE"

            .FieldNames = True

            .RowNumbers = False

            .FillAdjacentFormulas = False

            .PreserveFormatting = True

            .RefreshOnFileOpen = False

            .BackgroundQuery = True

            .RefreshStyle = xlInsertDeleteCells

            .SavePassword = True

            .SaveData = True

            .AdjustColumnWidth = True

            .RefreshPeriod = 0

            .PreserveColumnInfo = True

            .Refresh BackgroundQuery:=False

        End With

    End Sub