在 VBA SQL 语句中连接字段时出现错误 424

Error 424 when concatenating fields in a VBA SQL statement

我在 VBA 中编写了以下 SQL 语句,但是当我在字符串变量 [=13= 中执行此语句时出现错误 424(“需要对象”) ](VBA 编辑器只突出显示整个 SQL 字符串)。下面的语句是一系列语句的一部分,这些语句在执行时将 Excel 电子表格作为临时 table 导入我的 Access 数据库,更新状态,添加新记录,然后删除临时 table.

     Private Sub btnImport_Click()

'create a new file system object that will check for conditions and import the file as a new table if a valid name is chosen

    Dim FSO As New FileSystemObject
    Dim strSQL, strSQL2, strSQL3, strSQL4 As String
    Dim dateToday As String
    Dim tableTest As Object
    Dim fieldNew As Object
    Dim db As DAO.Database
    Dim Iss As DAO.Recordset
    Dim Temp As DAO.Recordset
    Dim reccordsAdded As Long
    
    dateToday = Date
    Set db = CurrentDb
    'Set Iss = db.OpenRecordset("IssuesDemo")
    
    'If no file name in box
    If Nz(Me.txtFileName, "") = "" Then
        MsgBox "Please choose a file."
        Exit Sub
    End If
    
    'If a file name is in box and the file can be located
    If FSO.FileExists(Me.txtFileName) Then
        
        ImportExcel.ImportExcel Me.txtFileName, "TempTable"
       
    'once it imports the table, it then adds today's date (the upload date)
        
        strSQL = "ALTER TABLE TempTable ADD COLUMN Upload_Date DATE;"
        strSQL2 = "UPDATE TempTable SET Upload_Date = '" & Date & "'"
       
       
       'This SQL Statement inserts the data from TempTable into the Issues table that does not match anything currently in the issues table.
       
       
        strSQL3 = "UPDATE IssuesDemo SET IssuesDemo.Status = 'Closed' " & _
                  " WHERE NOT EXISTS " & _
                  " (SELECT 1 FROM TempTable WHERE" & _
                  "'" & IssuesDemo.[Provider Identifier] & IssuesDemo.[Caresite] & IssuesDemo.[Care Address] & "'=  " & _
                  "'" & TempTable.[Provider Identifier] & TempTable.[Caresite] & TempTable.[Address Composite (Caresite) (Care Site)] & "')"

                  
       
       
        strSQL4 = "INSERT INTO IssuesDemo " & _
                " ([Provider Identifier], [Provider], " & _
                " [Caresite], [Care Address], " & _
                " [Bing Address], [Upload Date]) " & _
                " SELECT " & _
                " TempTable.[Provider Identifier], TempTable.[Provider], TempTable.[Caresite], " & _
                " TempTable.[Address Composite (Caresite) (Care Site)] as [Care Address], " & _
                " TempTable.[Bing Suggested Postal Address (Caresite) (Care Site)] as [Bing Address], " & _
                " TempTable.[Upload_Date] as [Upload Date] " & _
                " FROM TempTable " & _
                " WHERE NOT EXISTS " & _
                " (SELECT 1 FROM IssuesDemo WHERE " & _
                    " IssuesDemo.[Provider Identifier] = TempTable.[Provider Identifier] " & _
                    " AND " & _
                    " IssuesDemo.[Care Address] = TempTable.[Address Composite (Caresite) (Care Site)] " & _
                    " AND " & _
                    " IssuesDemo.[Caresite] = TempTable.[Caresite]) "
''                    " AND " & _
''                    " IssuesDemo.[Upload Date] = TempTable.[Upload_Date]) "

 '       strSQL4 =
              
        db.Execute strSQL, dbFailOnError
        db.Execute strSQL2, dbFailOnError
        db.Execute strSQL3, dbFailOnError
        db.Execute strSQL4, dbFailOnError
        
        'This displays a message box that confirms to the user that the operation succeeded and that X records were added.
        recordsAdded = db.RecordsAffected
        MsgBox "Operation Successful!  You added " & recordsAdded & " records to the database."
        
        'This deletes the temporary table.
        DoCmd.RunSQL ("DROP TABLE TempTable;")
        
    Else
    'Error message if file can't be found
        MsgBox "File not found."
    End If
    

End Sub

我的目标是将两组连接在一起的字段进行比较。我想这样做是因为许多记录从一个 table 到另一个是相似的,在这三个字段中的一个字段中存在细微差别;这里的两个 table 之间没有通用的唯一标识符。

我的 VBA 代码中的其他所有内容都执行得很好。感谢您提供的任何帮助。

当引用实际的 table 列而不是外部值(如表单上的文本框)时,table.field 名称必须是 SQL 字符串的一部分。

strSQL3 = "UPDATE IssuesDemo SET IssuesDemo.Status = 'Closed' " & _
          " WHERE NOT EXISTS " & _
          " (SELECT 1 FROM TempTable WHERE " & _
          " IssuesDemo.[Provider Identifier] & IssuesDemo.[Caresite] & IssuesDemo.[Care Address]  = " & _
          " TempTable.[Provider Identifier] & TempTable.[Caresite] & TempTable.[Address Composite (Caresite) (Care Site)] )"

您的代码中发生的情况是:VBA 将 IssuesDemo.[Provider Identifier] 视为一个变量,并假设存在一个对象 IssuesDemo 和 属性 [Provider Identifier] .