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

Security Permissions

Control Closing Of Form Or Database by SgtJarrow
Posted: 3 May 02 (Edited 27 Jun 02)

Most of the code for this FAQ came from the Access 2000 Developer's Handbook Volume 1: Desktop Edition by Fetz, Litwin, and Gilbert, Published by Sybex.  I cannot take credit for the code.  Although this is publsihed in an Access 2000 manual, I have successfully used this in Access 97 as well and believe it will work in Access 20002.

Access allows you to remove the little 'x' in the right hand corner of your form, and there is even a way to remove the 'x' for the application itself (but this is much more detailed, involving API calls and the like and is not discussed in this FAQ).  But your users can still close the apllication by using Ctrl+F4 or Alt+F4 to close you forms.  You could use the AutoKeys option and remap these keys to nothing or a message saying it is disabled, but this method simply cancels the closing of the form.  If you are using some main screen for your application, by placing this procedure on that form, you will effectively prevent your users from closing your database except by the method you provide.

1.  Set the CloseButton propert of the form to No.  You can alternately set the ControlBox property to No, which removes both the 'x' and the min max buttons.

2.  In the declaration's area of the form's VBA code, define the followinf variable:

    Public OK2Close As Boolean

3.  In the form's OnLoad event, place the following code:

    OK2Close = False

4.  On your form should be a button to close the form (or Quit the application if on the Main screen).  In the OnClick event for this button, place:

    OK2Close = True
    DoCmd.Close     '(or DoCmd.Quit for Main screen)

5.  In the form's Unload event, place:

    Cancel = Not OK2Close

That's it....the above 5 steps and you now have a form (or application) that cannot be closed except by the method you have provided.

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