在表单中,创建新记录,编辑一些数据,但在保存之前,请使用表单上的组合框选择要导航到的另一条记录。这将触发cboSalePicker_AfterUpdate
。然后在此子Form_BeforeUpdate
执行期间。用户单击消息框上的"否"以不保存新记录。然后执行cboSalePicker_AfterUpdate
其余部分,但显示以下错误消息:
错误信息
Error number -2147417848: Method ‘FindFirst’ of object ‘Recordset2’ failed.
与线路Me.Recordset.FindFirst "[SaleID] = " & Str(Nz(cboSalePicker.Value, 0))
相关联
但是,如果保存新记录,则不会产生错误,并且代码按预期执行。
Form_BeforeUpdate
Private Sub Form_BeforeUpdate(Cancel As Integer)
On Error GoTo ErrorHandler
Dim strMsg As String
Dim iResponse As Integer
'Specify the mesage to display
strMsg = "Do you wish to save the changes?" & Chr(10)
strMsg = strMsg & "Click Yes to Save or No to Discard changes."
'Display the msg box
iResponse = MsgBox(strMsg, vbQuestion + vbYesNo, "Save Record?")
'Check response
If iResponse = vbNo Then
'Undo the change.
DoCmd.RunCommand acCmdUndo
'Cancel the update
Cancel = True
End If
Exit Sub
ErrorHandler:
MsgBox "Error number " & Err.Number & ": " & Err.Description
End Sub
cboSalePicker_AfterUpdate
Private Sub cboSalePicker_AfterUpdate()
On Error GoTo ErrorHandler
Me.Recordset.FindFirst "[SaleID] = " & Str(Nz(cboSalePicker.Value, 0))
Exit Sub
ErrorHandler:
MsgBox "Error number " & Err.Number & ": " & Err.Description
End Sub
谢谢
您正在使用此将您的 SaleID 转换为字符串
Str(Nz(cboSalePicker.Value, 0))
但是您首先找到的是寻找一个数字。如果 SaleID 是一个数字,则从代码中的组合框值周围删除 Str() 函数。
要显示串联,请尝试此操作
Private Sub cboSalePicker_AfterUpdate()
On Error GoTo ErrorHandler
Dim sCriteria as String
sCriteria = "[SaleID] = " & Nz(Me.cboSalePicker, 0)
debug.print sCriteria
Me.Recordset.FindFirst sCriteria
Exit Sub
ErrorHandler:
MsgBox "Error number " & Err.Number & ": " & Err.Description
End Sub
在调试时注释掉第一个错误处理程序行。