错误3075语法缺少逗号

时间:2014-09-15 15:20:29

标签: sql vba ms-access access-vba

我在互联网上的许多地方试图解决这个问题,但我似乎无法弄明白。有人可以提供第二组眼睛,只检查我的语法错误吗?

错误说RunTime错误3075, “查询表达式中的语法错误(逗号) '(' 试验 '12 /二千零十四分之十五' , '主')'

最初,当我把它分成四个不同的INSERT语句时,它工作了,但是当我把它组合起来时,它停止了工作

    SQL = "INSERT INTO WeeksonCall([Employee], [Week], [Prim/Backup]) VALUES (('" & _ cboName.Column(1) & "','" & cboWeek.Column(1) & "','" & cboPrimBack.Value & _
"'),('" & cboName2.Column(1) & "','" & cboWeek.Column(1) & "','" & cboPrimBack2.Value & _
"'),('" & cboName.Column(1) & "','" & cboWeek2.Column(1) & "','" & cboPrimBack2.Value & _
"'),('" & cboName2.Column(1) & "','" & cboWeek2.Column(1) & "','" & cboPrimBack.Value & "'))"

2 个答案:

答案 0 :(得分:2)

这不是MS Access中的正确语法。您必须单独插入记录,如:

DoCmd.SetWarnings False

SQL = "INSERT INTO WeeksonCall(Employee, Week, [Prim/Backup]) " & _
"VALUES ('" & _ cboName.Column(1) & "','" & cboWeek.Column(1) & "','" & cboPrimBack.Value & "')"

SQL = "INSERT INTO WeeksonCall(Employee, Week, [Prim/Backup]) " & _
"VALUES ('" & cboName2.Column(1) & "','" & cboWeek.Column(1) & "','" & cboPrimBack2.Value & "')"

SQL = "INSERT INTO WeeksonCall(Employee, Week, [Prim/Backup]) " & _
"VALUES ('" & cboName.Column(1) & "','" & cboWeek2.Column(1) & "','" & cboPrimBack2.Value & "')"

SQL = "INSERT INTO WeeksonCall(Employee, Week, [Prim/Backup]) " & _
"VALUES ('" & cboName2.Column(1) & "','" & cboWeek2.Column(1) & "','" & cboPrimBack.Value & "')"

DoCmd.SetWarnings True

编辑:您可以在插入前使用SetWarnings = False停止用户提示。然后在插入后通过DoCmd.SetWarnings True重新打开警告。

答案 1 :(得分:1)

您能否提供您正在使用的VBA的剩余部分,以便询问用户输入?据我所知,你只是为SQL变量分配一个字符串。你的其余代码在哪里?

顺便说一句,我从不喜欢使用:

   DoCmd.SetWarnings False

而是使用下面的模块,如果你遇到错误就可以处理它而不是隐藏它:

Option Compare Database
Option Explicit

Public Function Run_Safe_SQL(strSQL)
On Error GoTo Error_Handler
Dim db As DAO.Database

   Set db = CurrentDb()
   db.Execute strSQL, dbFailOnError
   DBEngine.Idle dbRefreshCache
'   DoEvents

Exit_Here:
   'Cleanup
   Set db = Nothing
   strSQL = ""
   Exit Function

Error_Handler:
    MsgBox Err.Description & " " & Err.Number

End Function