我正在尝试使用SQL为我的访问数据库编写一个简单的追加查询。在尝试执行代码时,我得到的消息是:
通奸错误。已执行的函数或变量
查询是一个连接4个表并将字段粘贴到另一个表中的查询。使用标准MS Access查询时,它工作正常。然后我生成并复制了SQL代码(下面),但遗憾的是无法使查询生效。
关于奇怪事情的最后一点说明。与我成功编写的所有其他SQL查询不同,这一次,在将Application.DoCmd.RunSQL(st_sql)写入VBA时,“L”和“(st_sql)之间的空格由于某种原因被截断..奇怪,对于在我完成其他追加查询的整个例程中的任何其他字符串,都不会发生这种情况。
以下是代码:
st_sql = "INSERT INTO[tblContactReporting03]([ID Project],[tblProjManagementPhaseHierarchy],[tblProjManagementSubPhaseHierarchy],[ID_Event],[SubTask_Hierarchy],[Project],[Sub project],[Project_Phase],[Project_Sub_Phase],[ContactFullName],[Role_Type],[type],[Event],[Effective_date],[Commitment],[Sub_task_name],[Status],[Notes])" & _
"SELECT[tblProjectMasterList].[ID Project],[tblProjManagementPhase].[Hierarchy],[tblProjManagementSubPhase].[Hierarchy],[tblContactReporting02].[ID_Event],[tblContactReporting02].[SubTask_Hierarchy],[tblProjectMasterList].[Project],[tblProjectMasterList].[Sub project],[tblProjManagementPhase].[Project_Phase],[tblProjManagementSubPhase].[Project_Sub_Phase],[tblContactReporting02].[ContactFullName],[tblContactReporting02].[Role_Type],[tblContactReporting02].[type]," & _
"[tblContactReporting02].[Event], [tblContactReporting02].[Effective_date],[tblContactReporting02].[Commitment],[tblContactReporting02].[Sub_task_name],[tblContactReporting02].[Status],[tblContactReporting02].[Notes]" & _
"FROM[tblProjectMasterListINNER JOIN ([tblProjManagementPhase] INNER JOIN ([tblContactReporting02] INNER JOIN [tblProjManagementSubPhase] ON [tblContactReporting02].[ID_Project_Sub_Phase] = [tblProjManagementSubPhase].[ID_Project_Sub_Phase]) ON ([tblContactReporting02].[ID_Project_Phase] = [tblProjManagementPhase].[ID_Project_Phase]) AND ([tblProjManagementPhase].[ID_Project_Phase] = [tblProjManagementSubPhase].[ID_Project_Phase])) ON [tblProjectMasterList].[ID Project] = [tblProjManagementPhase].[ID_Project]" & _
"ORDER BY [tblProjectMasterList].[ID Project], [tblProjManagementPhase].[Hierarchy], [tblProjManagementSubPhase].[Hierarchy], [tblContactReporting02].[ID_Event], [tblContactReporting02].[SubTask_Hierarchy];" & _
Application.DoCmd.RunSQL(st_sql)
答案 0 :(得分:1)
在运行之前我建议使用Debug.Print st_sql
,以便您能够调试构造的SQL。
你得到的错误是因为RunSQL
是一个sub而不是一个函数,所以你需要调用它1)没有括号:
Application.DoCmd.RunSQL st_sql
或2)之前使用Call并使用括号:
Call Application.DoCmd.RunSQL(st_sql)
您可以将语法2用于不需要使用其返回值的函数。