问题描述
我在VBA中编写了以下SQL语句,但是当我在字符串变量strSQL3
上执行该语句时,出现了错误424(“对象必需”)(VBA编辑器仅突出显示了整个SQL串)。以下语句是一系列语句的一部分,这些语句在执行时将Excel电子表格作为临时表导入到我的Access数据库中,更新状态,添加新记录,然后删除该临时表。
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
我在这里的目标是将两组连接字段进行比较。我之所以要这样做,是因为许多记录从一张表到另一张表是相似的,在这三个字段之一中有细微的差别。这两个表之间没有唯一的唯一标识符。
我的VBA代码中的所有其他内容都可以正常执行。感谢您能提供的任何帮助。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)