连接 VBA SQL 语句中的字段时出现错误 424
Posted
技术标签:
【中文标题】连接 VBA SQL 语句中的字段时出现错误 424【英文标题】:Error 424 when concatenating fields in a VBA SQL statement 【发布时间】:2020-08-17 21:49:11 【问题描述】:我在 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 代码中的其他所有内容都可以正常执行。感谢您提供的任何帮助。
【问题讨论】:
如果您发布整个代码会有所帮助Debug.Print strSQL3
- 输出看起来像你期望的那样吗?
@RicardoDiaz 我添加了整个代码
@TimWilliams 当我将该命令输入到中间窗口时,我没有得到任何输出。我是一个相对初学者,所以我可能只是做错了。
您将 debug.print 放入您的代码中,输出会显示在即时窗格中
【参考方案1】:
当引用实际的表列而不是来自外部的值(如表单上的文本框)时,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]
。
【讨论】:
谢谢!我很欣赏这种洞察力。以上是关于连接 VBA SQL 语句中的字段时出现错误 424的主要内容,如果未能解决你的问题,请参考以下文章
在 vba 中执行 if 和 select 语句时出现语法错误
查询带有标题的制表符分隔的文本文件时出现 VBA 错误 - “没有为一个或多个必需参数提供值”
在与 SQL Server 建立连接时出现与网络相关的或特定于实例的错误。 错误号: 2 严重性: 20 状态: 0