Connecting Tech Pros Worldwide Forums | Help | Site Map

How to cancel/stop code execution

Member
 
Join Date: Mar 2007
Location: Columbus, OH
Posts: 69
#1: Oct 24 '08
Hi everyone. I'm creating an application inside Access 2007. The application will retrieve data from various locations on my company's network servers.

Depending on the time of day, alignment of the planets, other unfathomable mysteries sometimes my company's network is very, very slow.

I would like to provide the user of my application with a "Cancel" button on a form that will cancel/stop execution of the code (at whatever point it may be at) if the user clicks the button.

Is there some sort of DoCmd method or something else I can call to cancel/stop code execution in Access?

I could use the "CancelEvent" method. But to use this method, I must insert it at pre-determined points in the code, and then test to see if the user has selected the cancel button. It would work, but it's not as instantaneous as I wuld like. Plus, if execution hangs because of a slow network (which is the condition I'm most concerned about) if it doesn't happen to be a the proper spot in my code, the CancelEvents method wont be called.

I could also instruct the user to hit "Ctrl-Break" on the keyboard to stop execution, but that would produce a error dialog box which the user must learn to hit the "End" button. And besides, I don't know if users could REMEMBER the "Ctrl-Break" combination. :-)

Any thoughts or suggestions are greatly appreciated.

Thanks,
sphinney

DonRayner's Avatar
Expert
 
Join Date: Sep 2008
Location: Canada
Posts: 494
#2: Oct 24 '08

re: How to cancel/stop code execution


You could use the on timer event from a hidden form to check the state of a public variable that you set with the cancel button and then have it do the cancel event when it detects the change.
NeoPa's Avatar
Administrator
 
Join Date: Oct 2006
Location: London - UK
Posts: 15,730
#3: Oct 25 '08

re: How to cancel/stop code execution


Unfortunately, most code run in Access is synchronous.

A timer event could certainly be triggered, but even a "Stop" statement in there would, I'm pretty sure, be both unsafe and not give you the solution you want.

Remember, when queries are run from non-Access based data sources, they are also generally synchronous and they leave the Access VBA suspended, so you can't even check for the status of a variable.
Member
 
Join Date: Mar 2007
Location: Columbus, OH
Posts: 69
#4: Oct 27 '08

re: How to cancel/stop code execution


Looks like my best bet is to go with the CancelEvent method. It works well enough. I just thought there might be some other command/method that would work better.

Thanks for your time and thoughts DonRayner and NeoPa!

sphinney
NeoPa's Avatar
Administrator
 
Join Date: Oct 2006
Location: London - UK
Posts: 15,730
#5: Oct 27 '08

re: How to cancel/stop code execution


No worries :)

Just sorry we didn't have any more to add.
Reply