Distinguishing root cause of Form_Close event

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Is there any way to distinguish from Form_Close events what caused the form
to close? Specifically, I need a different behavior if the form was closed
because the form was closed by the corner button or if it was closed because
the application is closing.
 
AFAIK, no, at least not directly.

However, you can set things up so that you know if a "Close App" button has
been pressed. Maybe that would help?
- Create a global boolean variable named bolOKToClose (initializes as
False).
- In the CloseApp button Click event, set that variable to True.
- In the Form_Unload event, check the value of bolOkToClose: If True,
continue, and close app. If False, Cancel the Unload and optionally prompt
the user that they can only Close the app by using the proscribed button.
(This assumes you have a "Main" form that is always open).

Once set up, you can incorporate the same logic in the Close events of
non-Main forms as well. If bolOkToClose has been set to True, the app is
closing. In any other case you could assume the user has pressed the corner
X and have your code respond appropriately.

HTH,
 
Thank you for the quick reply.

I've already done that with regard to the two command buttons that close the
menu system in question (one to exit the menu, one to exit Access entirely).
Both work well.

The problem is that it's also possible close Access directly, which as far
as I can tell, cannot be distinguished programatically from closing the form
manually.

I guess the easiest solution is to just keep the turn the corner close
button off, that way closing Access will be the only obvious way for the user
to unload the form without going through one of my command buttons (though
they could still cause me trouble by closing it through the menu or hotkey).
Alternately, I might have Form_Unload return cancel if the unload event
wasn't triggered through one of my command buttons.

But the reason I don't like either of those solutions is because both
involve restricting functionality rather than *using* functionality. The
user should have access to the corner close button and should be able to exit
access without exiting my menu first, but I need to have my system tell which
way they've exited so that it can handle some close down routines properly.
Oh, well.

Thank you again.

George Nicholson said:
AFAIK, no, at least not directly.

However, you can set things up so that you know if a "Close App" button has
been pressed. Maybe that would help?
{snip}
 
Michael Suttkus said:
Is there any way to distinguish from Form_Close events what caused
the form to close? Specifically, I need a different behavior if the
form was closed because the form was closed by the corner button or
if it was closed because the application is closing.

There's no good way to do this. If you need to isolate
application-close logic from form-close logic, the only solution I know
of is to open a hidden form at startup and keep it open all the time the
application is open. Then you can use that form's Unload or Close event
to hold your application-close logic, since that form will be closed (by
Access) only when the application is closed.
 
Is there any way to guarantee that the hidden status-tracking form would be
unloaded before the Menu form in question upon application closing?
 
Michael Suttkus said:
Is there any way to guarantee that the hidden status-tracking form
would be unloaded before the Menu form in question upon application
closing?

A quick experiment run in Access 2002 suggests that Access unloads forms
at shutdown in the same order in which they were loaded. In other
words, first loaded = first unloaded. I don't know if this is
guaranteed anywhere, and I haven't made exhaustive tests. If it does
hold true, then to have your hidden status-tracking form unloaded before
your Menu form, you'd want to make sure that it's opened first.
 
Back
Top