How to allow user to stop code running

PNGBill

Win10 Office Pro 2016
Local time
Tomorrow, 01:28
Joined
Jul 15, 2008
Messages
2,271
Access 2010 accdb.

I have some coe that loops through records and performs tasks. Sometimes this process can take 2 hours or more.

How can I have an option for the operator to stop this process if they need to do some other task ?

I imagine each loop could look for a key being depressed but how do you get the message to the operator and look for the key each time ?

Another way could be for a question to be asked and the operator has 3 seconds to click to halt or the code continues but how to do this without the need for the operator to click when there is no need to halt the code ?

Appreciate some advice.:)
 
Thanks. I have a little research to do on some issues the link brings up.

What a shame there isn't a Time Out version of MsgBox that will say be on the screen for 3 seconds and if not clicked, then clear from the screen and the code continues.

This would then allow the operator to make a cup of tea (or coffee) and at any time click to halt the code.

I have used a TempVar which will allow the code to resume where it was stopped, unless the database is closed.

I suppose I could create a form to be opened, count and then close the form and between each count, check for a button to be clicked ?
or am I dreaming :confused:
 
I suppose I could create a form to be opened, count and then close the form and between each count, check for a button to be clicked ?
or am I dreaming :confused:

You certainly could pop up a form with a Timer on it set to close the form after a set time.

Howver this would halt the code each time it popped up. The solution with a flag checked during the cycle is better.
 
what you could do is this, using an autokeys macro

wrap your process with a little block of code

Code:
'create a public boolean called stopprocess

stopprocess = false
while not stopprocess
    doevents  'very important!
    if stopprocess then
         msgbox("Userhalted job")
         exit sub
    end if

    {rest of your normal code goes here}

wend

now, add an autokeys macro, keyed to say ctrl+H, which runs code to open a msgbox, asking whether the process should be aborted. If so, set stoppprocess to true.

Autokeys are very useful macros, which cannot be replicated in code. They are single character interrupts. You still need the doevents though, otherwise the process doesn't let the interrupt execute.

Here's a demo.

Open the form, and press the button. A process will now start, which will continue forever. Now press ctrl-H, to get a msgbox asking whether to abort or not. (note that in this example the process eternal continues to run, even if you close the form. it will only end when you uise the ctrl-H option)

note that with any long manual process, you can add a progress indicator of some sort to show the user that something is happening - but you need the doevents to let access "share" the process, and display the indicator

View attachment autokeys.zip


I started messing around with this code to try out a few other things, so can anyone throw any light on this point ... when the sub eternal is running ctrl-break seems not be active. Anyone know why?
 
Last edited:
Autokeys are very useful macros, which cannot be replicated in code.

I thought at first of the OnKey Events of the form but then realised they won't function while the loop is running. Good thing to know. Thanks Dave.

Couple of other thoughts.

It might also be useful to store any information about the progress of the job when it is interrupted so it knows where to start again.

This might even include saving temporary recordsets if they were complex to reproduce when the job was to be continued. One of the cool things about ADODB recordsets is they can be saved to a file and restored.
 
what you could do is this, using an autokeys macro

wrap your process with a little block of code.....QUOTE]

Thanks Dave, very simple and smart.:)

Could the Public stopprocess As Boolean by used in more then one set of code ?
or would the remote possibility of two different sets of code running at the same time, both using stopprocess be an issue ?
 
using the same control flag would stop both processes obviously, but you could easily add other flags/variables to set the active process, and maybe only start a new long process under certain circumstances. you could also easily have other public booleans anyway.

the other thing in general is that you have to be careful about aborting some processes, depending on what you are doing, although I am sure you are aware of any tidying up you need.

note that using autokeys macros changes the action of the keypresses. eg ctrl+H is normally find/replace, but using an autokeys macro will supersede this behaviour, so you may need to be careful which letter you use.
 

Users who are viewing this thread

Back
Top Bottom