Run Code and then quit

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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
 
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
 
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.
 
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top