在我的表单“别名”的Form_beforeupdate()事件上,我有这个...

If IsNull(Me.txtFName) And IsNull(Me.txtLName) Then
MsgBox "You must enter a contact!", vbokayonly, "Contact"
Cancel = True
End If

每当这取消了runCommand代码,例如...
DoCmd.RunCommand acCmdSaveRecord

出现一个对话框,告诉我它已被取消。有没有办法抑制这些对话框?

最佳答案

您可以添加错误处理程序以捕获并忽略错误2501,该错误2501在表单的更新前事件取消DoCmd.RunCommand acCmdSaveRecord时触发

下面的示例来自我的表单,该表单具有用于保存当前记录的命令按钮。如我所愿,它禁止显示“RunCommand操作被取消”消息。

由于您的表单的代码显然是从多个位置调用DoCmd.RunCommand acCmdSaveRecord,因此像在cmdSave_Click()中一样,将每个调用替换为SaveRecord

Option Compare Database
Option Explicit ' <-- NEVER troubleshoot VBA code without this!!!

Private Sub cmdSave_Click()
    'DoCmd.RunCommand acCmdSaveRecord
    SaveRecord
End Sub

Private Sub SaveRecord()
    Dim strMsg As String

On Error GoTo ErrorHandler

    DoCmd.RunCommand acCmdSaveRecord

ExitHere:
    Exit Sub

ErrorHandler:
    Select Case Err.Number
    Case 2501 ' The RunCommand action was canceled.
        ' do nothing --> just ignore the error
    Case Else
        ' notify user about any other error
        strMsg = "Error " & Err.Number & " (" & Err.Description _
            & ") in procedure SaveRecord"
        MsgBox strMsg
    End Select
    Resume ExitHere
End Sub

Private Sub Form_BeforeUpdate(Cancel As Integer)
    If IsNull(Me.txtBlock_start.Value) Then
        MsgBox "You must enter a start time!", vbOKOnly, "Start Time"
        Cancel = True
    End If
End Sub

09-26 06:14