我正在尝试通过内部联接查询访问vba更新另一个表。要更新的字段名称将以表格形式进行。
我写了这个查询,但它给了我错误。
Dim dbs As Database
Dim qdf As QueryDef
Set dbs = OpenDatabase("Dba_Savings.mdb")
dbs.Execute " UPDATE TblClient INNER JOIN TblImport ON TblClient.Account_Id = TblImport.Account_Id" _
& " SET TblClient." & Me.txtNewFieldName & " = TblImport." & Me.txtNewFieldName2 & ";"
dbs.Close
任何人都可以帮助我。
Dim S As String
S = "UPDATE TblClient INNER JOIN TblImport ON TblClient.Account_Id = TblImport.Account_Id" _
& " SET TblClient." & Me.txtNewFieldName & " = TblImport." & Me.txtNewFieldName2 & ";"
Debug.Print S
dbs.Execute S
UPDATE TblClient INNER JOIN TblImport ON TblClient.Account_Id = TblImport.Account_Id SET TblClient.S_B_Mar-14 = TblImport.S_B_Mar-14;
SQL工作完美并更新表,没有错误。
答案 0 :(得分:1)
你错过了这两者之间的空格:
ON TblClient.Account_Id = TblImport.Account_Id" _
& "SET TblClient.
在VBA中构建SQL时,始终将SQL分配给字符串变量,然后Debug.Print
此变量。在这种情况下,错误很明显。
此部分也是不必要的,因为它已经在INNER JOIN中了:
WHERE TblClient.Account_Id = TblImport.Account_Id
修改强>
而不是
dbs.Execute " UPDATE TblClient INNER JOIN TblImport ON TblClient.Account_Id = TblImport.Account_Id" _
& " SET TblClient." & Me.txtNewFieldName & " = TblImport." & Me.txtNewFieldName2 & ";"
你这样做:
Dim S As String
S = "UPDATE TblClient INNER JOIN TblImport ON TblClient.Account_Id = TblImport.Account_Id" _
& " SET TblClient." & Me.txtNewFieldName & " = TblImport." & Me.txtNewFieldName2 & ";"
Debug.Print S
dbs.Execute S
Ctrl + G打开立即窗口,这将显示Debug.Print
的输出。请将此添加到您的问题中。
然后,如果它仍然出错,您还可以创建一个新查询,切换到SQL视图,复制&粘贴输出,并尝试运行查询。您可能会收到更好的错误消息。或者至少强调有问题的部分。