当我尝试运行追加查询时,我收到“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, vbOKOnly + vbCritical + vbSystemModal, 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, vbOKOnly + vbCritical + vbSystemModal, 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, vbOKOnly + vbCritical + vbSystemModal, 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, vbOKOnly + vbCritical + vbSystemModal, 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, Shift, 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, PartNum, StartDate) "
strSQL = strSQL & "VALUES (" & Chr(34) & strPress & Chr(34) & ", "
strSQL = strSQL & Chr(34) & gstrICS & 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”,直到我得到更新表的查询。
提前感谢您的任何帮助/帮助。这将非常有帮助。
答案 0 :(得分:2)
您的SQL无效。 它应该是:
INSERT INTO tblJob (Press, PartNum, StartDate) VALUES (value1, value2, value3)
您的似乎是INSERT INTO
,UPDATE ... SET
和SELECT ... FROM
答案 1 :(得分:2)
尝试运行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上的问题中。
答案 2 :(得分:0)
只是一个调试技巧,而不仅仅是一个真正的解决方案: