如何在 "cancel = true" 语句后删除讨厌的 "The runCommand action was canceled" 对话框

How can I remove the pesky "The runCommand action was canceled" dialog box after a "cancel = true" statement

在我表单的 Form_beforeupdate() 事件中 "Alias" 我有这个...

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,该错误在表单的 Before Update 事件取消时触发 DoCmd.RunCommand acCmdSaveRecord

以下示例来自我的表单,该表单具有用于保存当前记录的命令按钮。它抑制了 "RunCommand action was canceled" 消息,正如我认为你希望的那样。

由于您的表单代码显然从多个位置调用 DoCmd.RunCommand acCmdSaveRecord,请将每个调用替换为 SaveRecord,就像我在 cmdSave_Click()[=23 中所做的那样=].

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

我最终在 'Save and Open the Receipts form' 按钮上完成了此操作。

Private Sub btnSaveOpenReceipts_Click()

'''''SaveRecord'''''
If (Me.Dirty) Then
    SaveRecord
End If

'''''OpenReciepts'''''
If (Me.Dirty) Then 'unsure it saved
Exit Sub
Else
'blah blah blah
End If
End Sub