Search code examples
sqlvbajet

Syntax Error in SQL statement (2)


I am trying to run this SQL statement in a VBA macro but for some reason it won't run due to a syntax error that I cannot seem to pinpoint. I am rather new to VBA so any help in pinpointing this error would be greatly appreciated.

Dim dte As String, db As Database, tableName As String, DataDump As Recordset, clientTable As Recordset
Set db = CurrentDb
dte = InputBox("What date was the Data Dump run?", "Please Input a date")
tableName = "FN_DataDump_ALL_" & dte
Set DataDump = db.OpenRecordset(tableName, dbOpenDynaset, dbEditAdd)
Set clientTable = db.OpenRecordset("Clients", dbOpenDynaset, dbEditAdd)

MySQL = "SELECT DISTINCT DD.[Client ID] " & _
        "INTO Clients " & _
        "FROM " & tableName & "as DD " & _
        "Where CL.[Client ID] NOT IN (SELECT DD.[Client ID] ""FROM"" &  tableName & ""as DD"") "

db.Execute MySQL

Solution

  • Misplaced quotes around FROM and a missing space produced TABLENAMEAS, instead:

    = "SELECT DISTINCT DD.[Client ID] " & _
            "INTO Clients " & _
            "FROM " & tableName & " as DD " & _
            "Where CL.[Client ID] NOT IN (SELECT DD.[Client ID] FROM " & tableName & " as DD)"