我是VBA的新手,我正尝试使用VBA在具有选定字段的2个表中运行插入查询。但是,当我运行代码时,我得到了错误=运行时错误3134“ INSERT INTO语句中的语法错误
不确定问题出在哪里。有人可以查看代码并提出建议吗?
代码-
Public Sub sub_import_IDP_Data()
Dim str_query As String
Dim rs_filelist As Recordset
Set rs_filelist = CurrentDb.OpenRecordset("tbl_IDP_Filenames")
str_query = "INSERT INTO tbl_IDP_Import"
str_query = str_query & "SELECT "
str_query = str_query & ", tbl_IDP_Import_Temp.PRI_INDEX AS [PRI_INDEX]"
str_query = str_query & ", tbl_IDP_Import_Temp.ROUTING_SET AS [ROUTING_SET]"
str_query = str_query & ", tbl_IDP_Import_Iemp.START_TIME AS [START_TIME]"
str_query = str_query & ", tbl_IDP_Import_Temp.STOP_TIME AS [STOP_TIME]"
str_query = str_query & ", CBYTE(" & tbl_IDP_Import_Temp & ".[HOUR]) AS [HOUR]"
str_query = str_query & ", CBYTE(" & tbl_IDP_Import_Temp & ".[MINUTE]) AS [MINUTE]"
str_query = str_query & ", CSNG(IIF(ISNULL(" & tbl_IDP_Import_Temp & ".[F CALLS]), '0', " & tbl_IDP_Import_Temp & ".[F CALLS])) AS [F CALLS]"
str_query = str_query & ", CSNG(IIF(ISNULL(" & tbl_IDP_Import_Temp & ".[A CALLS]), '0', " & tbl_IDP_Import_Temp & ".[A CALLS])) AS [A CALLS]"
str_query = str_query & ", " & tbl_IDP_Import_Temp & ".[CALLS DEV%]"
str_query = str_query & ", CSNG(IIF(ISNULL(" & tbl_IDP_Import_Temp & ".[F AHT]), '0', " & tbl_IDP_Import_Temp & ".[F AHT])) AS [F AHT]"
str_query = str_query & ", CSNG(IIF(ISNULL(" & tbl_IDP_Import_Temp & ".[A AHT]), '0', " & tbl_IDP_Import_Temp & ".[A AHT])) AS [A AHT]"
str_query = str_query & ", CDBL(IIF(ISNULL(" & tbl_IDP_Import_Temp & ".[F REQ]), '0', " & tbl_IDP_Import_Temp & ".[F REQ])) AS [F REQ]"
str_query = str_query & ", CDBL(IIF(ISNULL(" & tbl_IDP_Import_Temp & ".[SCH R]), '0', " & tbl_IDP_Import_Temp & ".[SCH R])) AS [SCH R]"
str_query = str_query & ", CDBL(IIF(ISNULL(" & tbl_IDP_Import_Temp & ".[SCH O]), '0', " & tbl_IDP_Import_Temp & ".[SCH O])) AS [SCH O]"
str_query = str_query & ", CDBL(IIF(ISNULL(" & tbl_IDP_Import_Temp & ".[A REQ]), '0', " & tbl_IDP_Import_Temp & ".[A REQ])) AS [A REQ]"
str_query = str_query & ", CDBL(IIF(ISNULL(" & tbl_IDP_Import_Temp & ".[APS]), '0', " & tbl_IDP_Import_Temp & ".[APS])) AS [APS]"
str_query = str_query & ", '" & rs_filelist.Fields("fileinout_idp").Value & "' AS [InOut]"
str_query = str_query & ", #" & CDate(Format(rs_filelist.Fields("datecreated_idp").Value, "m/d/yyyy")) & "# AS [FileDate]"
str_query = str_query & ", '" & rs_filelist.Fields("filegroup_idp").Value & "' AS [FileGroup]"
str_query = str_query & "FROM tbl_IDP_Import_Temp"
CurrentDb.Execute str_query
End Sub
答案 0 :(得分:0)
您在插入到tbl和选择之间缺少空格。最后一行– @chris neilsen