Determining what's caused the form to be closed (Access 2013) (1 Viewer)

Simon_C

Registered User.
Local time
Today, 14:39
Joined
Jun 7, 2019
Messages
36
Is it possible to determine in code what's caused a form to be unloaded / closed?

If I've got a form open I can close the form by clicking on the close button in the top right-hand corner. But the form will also be closed if I click on the close button on the outer Access window and close down Access itself.

Is there a way within the form itself of determining which of those two methods - or indeed any other - is causing the form to unload / close?

Or a way within Access to pick up the fact that the user has clicked to close the main Access window?
 

theDBguy

I’m here to help
Staff member
Local time
Today, 07:39
Joined
Oct 29, 2018
Messages
21,357
Hi. To directly answer your question, I don't think there is a way to find out "how come" the form is closing. But if your real question is "how to prevent it from closing unless it's the proper way," then there is a method to do that.
 

Simon_C

Registered User.
Local time
Today, 14:39
Joined
Jun 7, 2019
Messages
36
There isn't a "proper way" in this system e.g. a "close" button on the body of the form itself, if that's what you mean. I just want to be able to differentiate between the two and do slightly different things depending on the circumstance.
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 10:39
Joined
May 21, 2018
Messages
8,463
This may help. Put this code in a form with a close button.
Code:
Dim allowClose As Boolean

Private Sub Command1_Click()
  allowClose = True
  DoCmd.Close acForm, Me.Name
End Sub

Private Sub Form_Unload(Cancel As Integer)
  If Not allowClose Then
   Cancel = True
    MsgBox "Must use the close Button to close form"
 End If
End Sub

The only way to close the form is through the button
 

theDBguy

I’m here to help
Staff member
Local time
Today, 07:39
Joined
Oct 29, 2018
Messages
21,357
There isn't a "proper way" in this system e.g. a "close" button on the body of the form itself, if that's what you mean. I just want to be able to differentiate between the two and do slightly different things depending on the circumstance.
Right. What I was attempting to guess from your original post is whether you didn't want the form to actually close unless the user does a specific action. That could mean click on a specific "Close" button instead of the Access red "X" button. This is usually what this type of question boils down to in previous discussions. However, if you're requirement is more complex than that, then please give us more details on what you need to help us understand your specific situation. But as I said earlier, just trying to find out "how" a form was closed is non-trivial.
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 10:39
Joined
May 21, 2018
Messages
8,463
I would slightly caveat what DBguy said. Finding if it was closed by a button or closed by any X is easy.
Code:
Dim ButtonClose As Boolean

Private Sub Command1_Click()
   ButtonClose = True
   DoCmd.Close acForm, Me.Name
End Sub

Private Sub Form_Close()
  If ButtonClose Then
    MsgBox "Closed by button"
  Else
    MsgBox "closed by X (either form or application)"
  End If
End Sub

However to differentiate between between the Form's X and the Application X would definitely be non-trivial. The problem is there is no application level events that can be trapped. Maybe you could do this with API, but beyond my ability.
 

theDBguy

I’m here to help
Staff member
Local time
Today, 07:39
Joined
Oct 29, 2018
Messages
21,357
I would slightly caveat what DBguy said. Finding if it was closed by a button or closed by any X is easy.
Code:
Dim ButtonClose As Boolean

Private Sub Command1_Click()
   ButtonClose = True
   DoCmd.Close acForm, Me.Name
End Sub

Private Sub Form_Close()
  If ButtonClose Then
    MsgBox "Closed by button"
  Else
    MsgBox "closed by X (either form or application)"
  End If
End Sub

However to differentiate between between the Form's X and the Application X would definitely be non-trivial. The problem is there is no application level events that can be trapped. Maybe you could do this with API, but beyond my ability.
I stand corrected. However, all I can see that proves is you can tell how the form was closed only if you set it up beforehand. In other words:
Code:
If ButtonClose Then
    'form is being closed by a button we created
Else
    'we actually have no idea here why or how the form was closed
    'it could be the form X button, the Access application X button, a DoCmd.Close method, 3-figer salute, etc.
End If
PS. Rereading the above post, I think I am basically saying the same thing. Sorry.
 

Simon_C

Registered User.
Local time
Today, 14:39
Joined
Jun 7, 2019
Messages
36
I guess the simplest way to describe this is that I want a message box to be displayed (and to hold up proceedings until the user responds) when the user clicks on the Form's [X] but not when they click on the Application's [X].

The user's response could - but will not always - prevent the form from being closed. But as I've said only if the user has clicked on the Form's [X]. If Access itself is being closed I want none of this to happen and the closure to continue as normal.
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 10:39
Joined
May 21, 2018
Messages
8,463
like I said, I know of no way to do that. My recommendation would be to get rid of the x on the form and only be able to close the form from a button. The other way is to make the form modal so that you cannot close it from the application since you cannot get focus off the form.
 

Isaac

Lifelong Learner
Local time
Today, 07:39
Joined
Mar 14, 2017
Messages
8,738
Maybe Microsoft will read this thread and provide Access with a functionality similar to Excel userform's QueryClose event.
 

Users who are viewing this thread

Top Bottom