Share via

Prevent 'the RunCommand action was canceled.' message

Anonymous
2012-04-20T13:39:57+00:00

Hi - I am getting the messagebox - 'the RunCommand action was canceled.'  After My Msgbox below is displayed.  I can click OK to procede but it seems redundant and requires an extra click.  Is there a way to prevent this extra (2nd) message box from displaying?

Private Sub Form_BeforeUpdate(Cancel As Integer)

 If Not IsNull(Me.ERS_ACT_LOC_DTE) And Me.ERS_ACT_LOC_DTE > Date Then

        MsgBox "Letter of Counseling Date Must be Current Date or Before Current Date. Hit ESCAPE Key to Undo and SAVE record Again."

        Me.ERS_ACT_LOC_DTE.ForeColor = vbRed

        Cancel = True

    ElseIf Not IsNull(Me.ERS_ACT_LOC_DTE) And Me.ERS_ACT_LOC_DTE <= Date Then

  Me.ERS_ACT_LOC_DTE.ForeColor = vbBlack

    End If

Microsoft 365 and Office | Access | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments
Answer accepted by question author
  1. Anonymous
    2012-04-24T12:37:02+00:00

    I don't see any real harm in allowing them to use the navigation buttons.  If the data is incomplete the code in the form's BeforeUpdate event procedure will kick in if they try, and prevent them leaving the current record until they've saved entered the required data.

    You might also be interested in the SaveDemo.zip file in my public databases folder at:

    https://skydrive.live.com/?cid=44CC60D7FEA42912&id=44CC60D7FEA42912!169

    You might have to copy the text of the link into your browser's address bar (not the link location).  For some reason it doesn't always seem to work as a hyperlink.

    This file includes validation on the first and last name, but also forces the user to save the record and close the form via command buttons.

    0 comments No comments

8 additional answers

Sort by: Most helpful
  1. Anonymous
    2012-04-23T12:02:34+00:00

    Ken - Thanks for the descriptive comment/notes.  It really helped follow the logic.  I was able to integrate your code w/ the Const SAVE_CANCELLED = 2501 and Case logic into the existing error handling code.  I assume the RunCommand message is prevented by no action if Case is SAVE_CANCELLED.

    0 comments No comments
  2. Anonymous
    2012-04-20T20:59:09+00:00

    You have a choice.  Either amend the existing error handler:

    Private Sub btnSaveRec1_Click()

    On Error GoTo Err_btnSaveRec_Click

           Const SAVE_CANCELLED = 2501

           DoCmd.RunCommand acCmdSaveRecord

       Exit_btnSaveRec_Click:

        Exit Sub

    Err_btnSaveRec_Click:

        Select Case Err.number

            Case SAVE_CANCELLED

            ' anticipated error, do nothing

            Case Else

            ' unknown error, inform user

            MsgBox Err.Description

        End Select

        Resume Exit_btnSaveRec_Click

    End Sub

    or change it to use the in-line error handling I posted:

    Private Sub btnSaveRec1_Click()

        Const SAVE_CANCELLED = 2501

        On Error Resume Next

        RunCommand acCmdSaveRecord

        Select Case Err.Number

            Case 0

            ' no error

            Case SAVE_CANCELLED

            ' anticipated error, do nothing

            Case Else

            ' unknown error, inform user

            MsgBox Err.Description, vbExclamation, "Error"

        End Select

    End Sub

    Both will work just the same.

    0 comments No comments
  3. Anonymous
    2012-04-20T20:32:32+00:00

    Ken - Thanks for Reply.  The RunCommand is called in the save click event.  The code has the standard error handling.  Do I eliminate the standard error handling and insert your 'On Error Resume Next' code or use both code sets?  If so how should I edit the existing code?

    Private Sub btnSaveRec1_Click()

    On Error GoTo Err_btnSaveRec_Click

           DoCmd.RunCommand acCmdSaveRecord

       Exit_btnSaveRec_Click:

        Exit Sub

    Err_btnSaveRec_Click:

        MsgBox Err.Description

        Resume Exit_btnSaveRec_Click

    End Sub

    0 comments No comments
  4. Anonymous
    2012-04-20T18:02:35+00:00

    You need to trap and ignore the error on whatever line is calling the RunCommand method.  If you have a button to save the record for instance:

        Const SAVE_CANCELLED = 2501

        On Error Resume Next

        RunCommand acCmdSaveRecord

        Select Case Err.Number

            Case 0

            ' no error

            Case SAVE_CANCELLED

            ' anicipated error, do nothing

            Case Else

            ' unknown error, inform user

            MsgBox Err.Description, vbExclamation, "Error"

        End Select

    0 comments No comments