adanthuis
adanthuis

Reputation: 55

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

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

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...

DoCmd.RunCommand acCmdSaveRecord 

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

Upvotes: 3

Views: 4445

Answers (2)

adanthuis
adanthuis

Reputation: 55

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

Upvotes: 1

HansUp
HansUp

Reputation: 97131

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

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.

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().

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

Upvotes: 4

Related Questions