Run Code and then quit

D

David

How can I run VBA code and then quit the program. I know I can add a command
button with on click expression to run the code and then quit. The problem
would be if the user clicks on the X rather then clicking on the command
button to quit.

Thank You
David Ehrenreich
 
R

Rick B

If you have a form open (maybe open ine hidden with the file and leave it
running in the background at all times) you can add code to the form's
"close" event.

I think that will run when you click the "X". It won't run if they power
off the computer or if they CTRL-ALT-DEL and cancel it.

Rick B
 
E

elwin

You're trying to prevent the user from exiting without running your code.
Declare a boolean variable at the top of your MODULE, not procedure, so it
can be shared between event procedures.

Dim bolOkToExit as Boolean

Set the variable equal to 'True' at the top of your command button click
event procedure

bolOkToExit = True

Check the value of the variable with the form's unload event. If it hasn't
been set to 'True' then you cancel the unload event. This prevents the form
from closing, hence the application also can't close.

If Not bolOkToExit Then Cancel = True


Good luck.
 
L

Larry Daugherty

Hi David,

Put the code to Quit in the form's OnClose event. That way it doesn't
matter how you got there, the event always occurs - short of their hitting
the power switch.

HTH
 
Top