Avatar of Lambel
Lambel
 asked on

MSAccess Error Function Runs without an error

I have a procedure that I've built in error coding.  For some reason, the error process runs even when there are no errors.  Why is this?
Private Sub cmdCancel_Click()
    Dim strMessage As String
    
   On Error GoTo cmdCancel_Click_Error

   Forms!frmReportGenerator.[Report Parameters].SetFocus
Forms!frmReportGenerator.[Report Parameters].Controls.txtSaveAs.SetFocus

   On Error GoTo 0
   Exit Sub

cmdCancel_Click_Error:

    strMessage = "Error " & Err.number & " (" & Err.Description & ") in procedure cmdCancel_Click of VBA Document Form_frmSubRecipients-Add."
    strMessage = strMessage & " Application will stop processing now." & vbNewLine
    strMessage = strMessage & "Please note or copy this error message and contact application developer for assistance."
    MsgBox strMessage, vbCritical + vbOKOnly, "Error"
    End
End Sub

Open in new window

Microsoft Access

Avatar of undefined
Last Comment
Dale Fye

8/22/2022 - Mon
danishani

That happens because you are using:

On Error Goto 0

So basicly when the error occurs it turns off error handling and sets it back to the Access default.

So removing the line and then the error will be trapped by the cmdCancel_Click_Error.

HTH,
Daniel
ASKER CERTIFIED SOLUTION
Dale Fye

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck