在VBA中获取“ INSERT INTO语法错误”以进行追加查询Access 2002

问题描述

| 尝试运行追加查询时,我收到“ INSERT INTO中的语法错误”。 我正在建立一个数据库,以简化生产操作员的工作;通过手持式扫描仪捕获一些数据。 我一直在四处看看是否有人遇到过这样的问题。我已经看到了一些接近的内容,但是当我对代码进行了较小的更改时,仍然出现语法错误。 我正在使用未绑定的表单进行数据输入,但是我需要从表单捕获数据并将其添加到两个不同的表中。发生错误时,尝试运行DoCmd.Runsql。据我所讲的代码sql看起来正确。 这是我的过程的完整代码。 码:
Private Sub cmdNext_Click()

On Error GoTo Err_cmdNext_Click
\'Append table: tblScan with the Badge Number,Part #/ICS#,Lot # (if available),Press (i.e. MD01),Shift,and the current
\'                   Date/Time (captured with this code).
\'Append table: tblJob with Press,Part#/ICS#,and the current Date/Time (captured with this code).
Dim strBadge As String
Dim strPress As String
Dim dtmDate As Variant
Dim strsql As String
Dim strMessage As String
Dim strTitle As String
Dim varQuestion As Variant


strMessage = \"You have not entered a correct \"
strTitle = \"ERROR MISSING or INCORRECT informatION!\"
strsql = \"\"
dtmDate = Now

If IsNull(Me!cboBadgeNum.Column(1)) Then
    strMessage = strMessage & \"Badge Number.  Please scan the operator\'s badge before continuing.\"
    varQuestion = MsgBox(strMessage,vbOKOnly + vbCritical + vbSystemModal,strTitle)
    Me!cboBadgeNum.SetFocus
    GoTo Exit_cmdNext_Click
Else
    strBadge = Me!cboBadgeNum.Column(1)
End If


If IsNull(Me!cboPress.Column(1)) Then
    strMessage = strMessage & \"Press.  Please select the press where this job was run before continuing.\"
    varQuestion = MsgBox(strMessage,strTitle)
    Me!cboPress.SetFocus
    GoTo Exit_cmdNext_Click
Else
    strPress = Me!cboPress.Column(1)
End If

If gstrICS = \" \" Then
    strMessage = strMessage & \"ICS Number.  Please Enter a valid ICS Number before continuing.\"
    varQuestion = MsgBox(strMessage,strTitle)
    txtICS.SetFocus
    GoTo Exit_cmdNext_Click
ElseIf gstrICS = \"\" Then
    strMessage = strMessage & \"ICS Number.  Please Enter a valid ICS Number before continuing.\"
    varQuestion = MsgBox(strMessage,strTitle)
    txtICS.SetFocus
    GoTo Exit_cmdNext_Click
End If

If gstrLot = \"\" Then
    strMessage = strMessage & \"Lot Number.  Please Enter a valid Lot Number before continuing.\"
    varQuestion = MsgBox(strMessage,strTitle)
    txtLot.SetFocus
    GoTo Exit_cmdNext_Click
End If

\'Use an Apend query to update Scan table.   
**strsql = \"INSERT INTO tblScan (BadgeNum,PartNum,LotNum,Press,ScanDate) \" & vbCrLf
strsql = strsql & \"VALUES  (\" & Chr(34) & strBadge & Chr(34) & \",\"
strsql = strsql & Chr(34) & gstrICS & Chr(34) & \",\"
strsql = strsql & Chr(34) & gstrLot & Chr(34) & \",\"
strsql = strsql & Chr(34) & strPress & Chr(34) & \",\"
strsql = strsql & gShift & \",\"
strsql = strsql & \"#\" & dtmDate & \"#);\"**


DoCmd.SetWarnings False
DoCmd.Runsql strsql
DoCmd.SetWarnings True

\'Use an Apend query to update Job table.
**strsql = \"INSERT INTO Job (Press,StartDate) \"
strsql = strsql & \"VALUES (\" & Chr(34) & strPress & Chr(34) & \",\"
strsql = strsql & \"Job.JobDate = #\" & dtmDate & \"#);\"**


\'DoCmd.SetWarnings False
DoCmd.Runsql strsql
DoCmd.SetWarnings True

lblFirstName.Caption = \" \"
lblLastName.Caption = \" \"
txtICS.SetFocus
txtICS.Text = \" \"
txtLot.SetFocus
txtLot.Text = \" \"
cboBadgeNum.SetFocus
cboBadgeNum.Value = 0

DoCmd.Close


Exit_cmdNext_Click:
Exit Sub

Err_cmdNext_Click:
MsgBox Err.Description
Resume Exit_cmdNext_Click

End Sub
我暂时禁用了“ DoCmd.SetWarnings”,直到获得更新表的查询为止。 在此先感谢您的协助/帮助。这将非常有帮助。     

解决方法

        发生错误时,尝试运行DoCmd.RunSQL。据我所讲的代码,SQL看起来正确。 您的代码两次调用DoCmd.RunSQL。您是从这两者还是第二次得到错误? 我建议您放弃DoCmd.RunSQL以便:
Dim db As DAO.Database
Set db = Currentdb
db.Execute strSQL,dbFailonerror
这样,您就不必摆弄SetWarnings。您的错误处理程序不会重新打开SetWarnings,这可能会使Access处于无法获取重要信息的状态。因此,如果您觉得自己绝对肯定必须扭动SetWarnings,请确保无论您的子程序是否结束(错误与否),SetWarnings都会重新打开。但是更好的IMO首先不要搞砸SetWarnings。 @cularis试图帮助您看到您正在构造第二个INSERT语句,例如带有虚假名称的此示例。
INSERT INTO tblFoo (fieldA,fieldB) VALUES (fieldA = 7,fieldB = 2)
正如他所说,您不能在VALUES列表中包含=符号。它必须是这样的:
INSERT INTO tblFoo (fieldA,fieldB) VALUES (7,2)
我要添加到他的建议中的要点是使用Debug.Print,以便您可以在执行strSQL之前将其显示在立即窗口中。如果存在问题,则可以查看您要数据库引擎执行的语句...而不是试图想象您的代码构造了什么。如果无法立即发现问题,请从“立即窗口”中复制该语句,然后将其粘贴到新查询的SQL视图中。如果那仍然不能让您发现问题,则可以将该语句粘贴到关于stackoverflow的问题中。     ,        您的SQL无效。 它应该是:
 INSERT INTO tblJob  (Press,PartNum,StartDate) VALUES (value1,value2,value3)
您的似乎是
INSERT INTO
UPDATE ... SET
SELECT ... FROM
的混合物     ,        只是调试技巧,而不是真正的解决方案: debug.print strSQL字符串 复制\\粘贴到访问查询(SQL 查看,传递查询),然后尝试 执行它(实际上,您可以在可以连接到数据库的任何查询设计器中执行此操作) 然后,您可以进行一些试验, 您的文字字串上的错误,直到您 使它工作 您可以例如尝试插入一个 字段及其值,然后添加 其他领域,等等...     

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...