Solved Error Handler always triggering (1 Viewer)

nashaz

Member
Local time
Today, 08:53
Joined
Mar 24, 2023
Messages
111
Hi

I have the following code in one of my forms:

Code:
Private Sub CloseBtn_Click()

    On Error GoTo Errhandler
    
    If Not Me.Dirty Then
        DoCmd.RunCommand acCmdClose
    Else
        Select Case MsgBox("Do you want to save changes before closing the form?", vbYesNoCancel + vbCritical + vbDefaultButton1)
        Case vbYes:
            DoCmd.RunCommand acCmdSaveRecord
            DoCmd.RunCommand acCmdClose
        Case vbCancel:
            Exit Sub
        Case vbNo:
            Me.Undo
            DoCmd.RunCommand acCmdClose
        End Select
    End If
    
Errhandler:
    MsgBox Err.Description
  
End Sub

The issue is, everytime I close the form, a blank vbOKOnly msg box appears with no caption or content. I am having the same issue everywhere I have used the error handler. Thanks for your guidance
 

NauticalGent

Ignore List Poster Boy
Local time
Today, 03:53
Joined
Apr 27, 2015
Messages
6,341
You need an exit handler that includes an "Exit Sub" line. As written, your error handler will ALWAYS fire.
 

nashaz

Member
Local time
Today, 08:53
Joined
Mar 24, 2023
Messages
111
You need an exit handler that includes an "Exit Sub" line. As written, your error handler will ALWAYS fire.
Can you please guide me where I have to put Exit sub line?
 

NauticalGent

Ignore List Poster Boy
Local time
Today, 03:53
Joined
Apr 27, 2015
Messages
6,341
Also, take note of the "Resume Exit_handler" line. Very important.
 

NauticalGent

Ignore List Poster Boy
Local time
Today, 03:53
Joined
Apr 27, 2015
Messages
6,341
Easy day! Just to make you feel better, I had the exact same issue back in 2017, Click this link

It's good to return the favor.
 

nashaz

Member
Local time
Today, 08:53
Joined
Mar 24, 2023
Messages
111
Easy day! Just to make you feel better, I had the exact same issue back in 2017, Click this link

It's good to return the favor.

It sure is! Its amazing even in this day and age there are people helping other strangers and expecting absolutely nothing in return. Makes you feel a lot more upbeat in the midst of chaos around us all.
 

Gasman

Enthusiastic Amateur
Local time
Today, 08:53
Joined
Sep 21, 2011
Messages
14,301
Start learning to walk through your code with F8.
That error would have then been obvious to you.
 

nashaz

Member
Local time
Today, 08:53
Joined
Mar 24, 2023
Messages
111
Start learning to walk through your code with F8.
That error would have then been obvious to you.
I do that if I have to use VBA for Excel but in Access, it asks you to save macro before you can run through the code line by line. Or am I being very daft here?
 

Gasman

Enthusiastic Amateur
Local time
Today, 08:53
Joined
Sep 21, 2011
Messages
14,301
Firstly Sub/Functions are not called macros in Access, as Access has its own version of macros. This is just VBA In Access.
I never get asked to save vba, but it is good to do so, as if Access crashes, you will have lost all your code since the last save. I tended to just key Ctrl + S often.

Access macros do ask you to save before running them, and are very limited compared to VBA. I only ever used them for batch processing.
 

nashaz

Member
Local time
Today, 08:53
Joined
Mar 24, 2023
Messages
111
Firstly Sub/Functions are not called macros in Access, as Access has its own version of macros. This is just VBA In Access.
I never get asked to save vba, but it is good to do so, as if Access crashes, you will have lost all your code since the last save. I tended to just key Ctrl + S often.

Access macros do ask you to save before running them, and are very limited compared to VBA. I only ever used them for batch processing.

When I press F8, nothing happens. If I try and run the sub, following window appears, asking to save macros:

1686239278561.png
 

Gasman

Enthusiastic Amateur
Local time
Today, 08:53
Joined
Sep 21, 2011
Messages
14,301
Set a breakpoint on If Not Me.Dirty Then and work from there.
You run the sub from your button. You have to be in the VBA window and click into a sub before you can use F8.

See the link re debugging in my signature.
 

nashaz

Member
Local time
Today, 08:53
Joined
Mar 24, 2023
Messages
111
Set a breakpoint on If Not Me.Dirty Then and work from there.
You run the sub from your button. You have to be in the VBA window and click into a sub before you can use F8.

See the link re debugging in my signature.
Thanks. will try it first thing tomorrow morning
 

nashaz

Member
Local time
Today, 08:53
Joined
Mar 24, 2023
Messages
111

Users who are viewing this thread

Top Bottom