Form unload event - did user close form or application?

KeithWilliams

Registered User.
Local time
Today, 13:39
Joined
Feb 9, 2004
Messages
137
Hi,

I have scoured the forum but not found a satisfactory answer to my query. I hope someone can advise.

I have a form I want to open first when the database is opened, and not allow to close until the user closes the database.

I can disable the close button using Close Button property = No for the form. But the user can still close it using ALT-F4.

To prevent this I coded:
Code:
Private Sub Form_Unload(Cancel As Integer)
    Cancel = True
End Sub

But this also prevents the user from closing the database, which I don't want.

I need to enhance the above code to allow the form to close if the user is closing the database, but not if they are closing just the form. Something like:
Code:
Private Sub Form_Unload(Cancel As Integer)
    If Not <application is closing> Then
        Cancel = True
    End If
End Sub

Can anyone suggest how to do this? I don't want to disable the File>Exit or application close buttons as this would confuse (or even enrage?) my technophobe users.

Many thanks,
Keith.
 
Keith,

The simplest way to do this might be to create a global scope variable in a standard module.

Roughly...

Code:
Public CloseDB as Boolean

When the user closes and exits properly, change the global to True...

Code:
CloseDB = True

In your form_unload event, evaluate the global variable...

Code:
   If CloseDB = False Then
        Cancel = True
    End If

Regards,
Tim
 
Hi Tim,

I wondered about that, but where would I code "CloseDB = True"? I think I wouold need to code it in an event where the application is closing but not yet closed. I am not aware of an Application Unload event analogous to the Form Unload event, otherwise I could use that.

Thanks,
Keith.
 
Rich,

Are you suggesting I open a hidden form after the form I want to keep open, then check whether it is still open when my form_unload event fires? Can I rely on Access closing the hidden form before my always-open form, even when I compile to an MDE file?

Thanks,
Keith.
 
This is one of the benefits of using a switchboard to control the application, but you can use the IsLoaded function on the hidden form to determine if your main form is open
 
Thanks Rich, I'll give that a try. Shame there isn't an "application closing" status you can check in Access...

Keith.
 
Keith,

Do I have this right? Alt-F4 closes Access (and, concomitantly, your file and form). Ctrl-F4 closes just the active form in your open Access file.

You want to allow your users to continue to shut down your Access file (and the Access application "shell") by clicking the X in the upper right hand corner? And yet you want to stop them from closing things down by pressing Alt-F4?

And you do not want your Access file to close if a user closes your "starting" form by pressing Ctrl-F4? (You have already dealt with the form's X.)

If this is correct, set the form's key preview property to true. And create a module scope variable at the top of the form...

Code:
Private F4Close as Boolean
Option Compare Database
Option explicit

In the form's Keydown event, put code similar to this...
Code:
'Respond to Ctrl-F4
      If Shift And acCtrlMask Then
          If KeyCode = vbKeyF4 Then blnF4 = True
      End If

'Respond to Alt-F4
      If Shift And acaltMask Then
          If KeyCode = vbKeyF4 Then blnF4 = True
      End If

In the form's unload event...
Code:
If F4Close = True Then
   Cancel = True
   F4Close = False
End If

Regards,
Tim
 
Hi Tim,

Sorry for not replying sooner, I don't seem to have got the email notifying that you had replied. Your solution does what I need, thank you!

I got the original description wrong, I want to allow the application to close (e.g. ALT-F4) but not close the form leaving the application running (e.g. CTRL-F4). This achieves the result of ensuring the form is open for as long as the application is, which is what I need.

Now I have a related problem. The form contains an activeX treeview as the only control. When I click CTRL-F4, the form stays open, but the current selection in the form is lost. I'm now looking for a workaround for that!

Thanks again,
Keith.
 

Users who are viewing this thread

Back
Top Bottom