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

时间:2021-12-27 04:15:10

On the Form_beforeupdate() event of my form "Alias" I have this...

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

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

Anytime this cancels a runCommand code such as...

任何时候取消runCommand代码,如...

DoCmd.RunCommand acCmdSaveRecord 

a dialog appears telling me that it was canceled. Is there a way to suppress those dialogs?

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

2 个解决方案

#1


You can add an error handler to trap and ignore error 2501, which is triggered when the form's Before Update event cancels DoCmd.RunCommand acCmdSaveRecord

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

The following example is from my form which has a command button to save the current record. It suppresses that "RunCommand action was canceled" message as I think you wish.

以下示例来自我的表单,其中包含用于保存当前记录的命令按钮。它抑制了“我想你想要的”RunCommand动作被取消“的消息。

Since your form's code apparently calls DoCmd.RunCommand acCmdSaveRecord from multiple locations, replace each of those calls with SaveRecord as I did in cmdSave_Click().

由于表单的代码显然从多个位置调用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

#2


I ended up doing this on a 'Save and Open the Receipts form' button.

我最终在“保存并打开收据表单”按钮上执行此操作。

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

#1


You can add an error handler to trap and ignore error 2501, which is triggered when the form's Before Update event cancels DoCmd.RunCommand acCmdSaveRecord

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

The following example is from my form which has a command button to save the current record. It suppresses that "RunCommand action was canceled" message as I think you wish.

以下示例来自我的表单,其中包含用于保存当前记录的命令按钮。它抑制了“我想你想要的”RunCommand动作被取消“的消息。

Since your form's code apparently calls DoCmd.RunCommand acCmdSaveRecord from multiple locations, replace each of those calls with SaveRecord as I did in cmdSave_Click().

由于表单的代码显然从多个位置调用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

#2


I ended up doing this on a 'Save and Open the Receipts form' button.

我最终在“保存并打开收据表单”按钮上执行此操作。

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