Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here


Microsoft: Access Forms FAQ

How to

KISS way to force users to click a command button to exit a form regardless of menu or toolbar options by SBendBuckeye
Posted: 21 Jun 02 (Edited 22 Jun 02)

I wish I could take credit for thinking of a simple solution like this, but I can't since I came across this in my reading (Access Developers Handbook, Volume 1). I noticed a couple threads on the forums regarding user's closing down forms by exiting Access via the menu or tool bars, etc. so I thought I would write a quick FAQ for it.

Comments, suggestions, feedback, etc. gladly accepted.

Way to only allow form exit if a command button is clicked:

1. Declare a public form module variable such as:
   Public AllowClose As Boolean

2. In the On Load or On Open event on your form, add this:
   AllowClose = False

3. In the Click event on the Exit command button, add this:
   AllowClose = True

4. In the On Unload event on your form, add this:
   Cancel = Not AllowClose
5. Test and enjoy

The reason this works is because the Unload event (as opposed to the On Close event) passes a Cancel parameter. If cancel is set to true (which it will be unless your command click event sets AllowClose to true), then the form cannot be unloaded no matter what keys are pressed or buttons are clicked on the form or elsewhere on a menu or toolbar.

This why you don't have to worry about disabling close or exit actions anywhere outside the form because it just doesn't matter. If AllowClose = False then Cancel will be true and the form will NOT unload.

Events that pass the cancel parameter come in handy. Other examples of form controls that pass a cancel parameter include Before Update and On Exit. If you set cancel to true, the control cannot lose focus no matter what the user did. If you want to restore the original value, put controlname.undo somewhere in the validation code before you cancel.

Have a great night all, it's been a long day!

Sample code for the above:

Option Compare Database
Option Explicit

Public AllowClose As Boolean

Private Sub Form_Load()
    AllowClose = False
End Sub

Private Sub Form_Unload(Cancel As Integer)
    Cancel = Not AllowClose
End Sub
Private Sub cmdClose_Click()
On Error GoTo Err_cmdClose_Click

    AllowClose = True

    Exit Sub

    MsgBox Err.Description
    Resume Exit_cmdClose_Click
End Sub

Back to Microsoft: Access Forms FAQ Index
Back to Microsoft: Access Forms Forum

My Archive

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close