Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Disable Access's close box 4

Status
Not open for further replies.

freefour

MIS
Aug 26, 2004
33
US
Hey all,

I want my users to exit out Access using my exit button (so I can run some vb script I wrote). I know how to disble the form's close button, but how can I disable Access's close X button at the very top?

Thanks so much!

--freefour
 
You can't.
The standard technique is to open a hidden form and use that form's close event to detect if the user has closed Access directly. (If the close event fires then you assume that the user is closing incorrectly and you do whatever you need to - run housekeeping/cancel the close/whatever- at that point.)
 
There is a way to disable the main access "X" so the user can't close down access that way. Do a search here and/or google to find the post. I remember seeing it not that long ago.
 
Create a 'background' form and open it hidden when your application starts up:
Code:
DoCmd.OpenForm "Background", , , , , acHidden

In the form's Unload event procedure, put the following code:
Code:
Cancel = True
MsgBox "Please use the Exit button to close the application"
This works because Access begins its shutdown by closing all forms. This Unload event gets raised when the Background form is being closed. By setting the Cancel parameter to True, it aborts the form close, which aborts the shutdown.

Let me suggest, however, that you go ahead and let them use the Access X button. All you have to do is call the same code from the background form's Close event that you currently call from your Exit button. If you do this, you should either eliminate the Exit button or you should change its Click procedure to:
Code:
RunCommand acCmdExit
The reason is that, if your Exit button calls your VBA code and then shuts down the application, Access will try to close the Background form, which will raise its Close event, and the form will try to call your VBA code again.

Rick Sprague
Want the best answers? See faq181-2886
To write a program from scratch, first create the universe. - Paraphrased from Albert Einstein
 
Thanks Rick,

Your code works great, but when I want them to exit, it still pops that msgbox up, even when I try to close the background form first.

How can I stop that from happening?

--freefour
 
I take it you still mean to force them to use your Exit button, rather than using my suggestion, right? My answer wasn't complete enough for that situation. Sorry.

In the Background form module, add this code:
Code:
(In the Declarations section:)
Public AllowShutdown As Boolean

(At the start of the Form_Unload event procedure:)
    If AllowShutdown Then Exit Sub

In your code, just before you execute the RunCommand acCmdExit (or DoCmd.Quit or Application.Quit):
Code:
    Form_Background.AllowShutdown = True

Rick Sprague
Want the best answers? See faq181-2886
To write a program from scratch, first create the universe. - Paraphrased from Albert Einstein
 
Hey Rick,

Thanks again. Got one more quick question. Instead of the line:

DoCmd.OpenForm "Background", , , , , acHidden

I want the background form to be the one that Access uses when it starts. How can I hide the form using code?

I tried the form_background.Visible = False but that did not work.

Thanks again,

--freefour
 
You mean you want to set the Startup Form property to "Background"? Personally, I don't like to use a startup form, but if that's what you want:
In the Background form's Form_Open (or Form_Load) event procedure, add the line:
Me.Visible = False

You could also try changing your statement to Forms!Background.Visible = False

By the way, the predefined variable Form_Background refers to the "default instance" of the form. Although you can usually use these predefined variables to refer to the only instance of an open form, it may not always work, and the documentation isn't clear on when it would or wouldn't. The alternate syntax Forms!Background will always work, though, so you may prefer to change the syntax I gave for setting the AllowShutdown property.

Rick Sprague
Want the best answers? See faq181-2886
To write a program from scratch, first create the universe. - Paraphrased from Albert Einstein
 
PaulF,

Where do I put that code? Sorry I am kinda a newbie.

Thanks!

--freefour
 
just place it in a new module, then call the function from the first form that opens during its On Open event.

PaulF
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top