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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Stopping Code ? 1

Status
Not open for further replies.

grobermatic

Technical User
Dec 21, 2002
153
GB
Hi,

How would I safely stop a procedure from being executed?

I have quite a long For...Next loop which I would like the user to be able to "Abort" by pressing a command button on the form.

Is there a simple command which would stop all code which is currently running?

Thanks

Craig

--------------------------------------------------------------------------------------------------------
"Time-traveling is just too dangerous. Better that I devote myself to study the other great mystery of the universe: Women!" .. Dr E. Brown (1985)
 
[blue]Exit For[/blue] will get you out of the loop.

[blue]Exit Sub[/blue] will get you out of the current procedure

[blue]End[/blue] will shut down the application

... but [blue]End[/blue] is fraught with peril. Avoid it if at all possible.
 
Hi,

Sorry if I wasn't clear before...

I have a For...Next loop running in a sub procedure, if I simply put "Exit For" in to the "On Click" Event of the command button will this work???

I can't test it at the moment as its at work.

Thanks


Craig

--------------------------------------------------------------------------------------------------------
"Time-traveling is just too dangerous. Better that I devote myself to study the other great mystery of the universe: Women!" .. Dr E. Brown (1985)
 
How are ya grobermatic . . . . .

I don't believe you can do that directly from an alternate routine, however, why not use the old manual [blue]Ctrl + Break[/blue] key combination? You'll have to close a dialog box, but it works just fine . . . . .

cal.gif
See Ya! . . . . . .
 
Perhaps you could use your "stop code" button to set a flag that the loop checcks for each time through the loop... perhaps the tag on the button control?

C
 
Hi!

Not sure I understand completely. But to have the opportunity to stop prior to executing, then do a message box telling "Either go fetch a coupla cups of coffe, or press cancel..." and act upon the users answer.

Ctrl+Break does work, sometimes too well, if you depend on variable values after the abort.

I haven't tried this, but in this thread (thread705-752094) Steve101 demonstrates a technique using ESC in a way that should give you a more controlled abort of the routine.

Roy-Vidar
 
Anyway, if you want some events while your sub is running, you have to call the DoEvents function inside your loop.

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884
 
Usually something like this

Declarations Section[tt]
Dim StopTheLoop As Boolean[/tt]

In the Loop[tt]
StopTheLoop = False
For n = 1 To 999
' ... Processing Code ...
DoEvents
If StopTheLoop Then Exit For
Next n[/tt]

Under a Click Event[tt]
Sub cmdStopIt_Click()
StopTheLoop = True
End Sub[/tt]
 
I took cgarts's advice here and programatically set the TAG property of the form to "STOP", then checked for this on each iteration of the for..next loop.

Thanks everyone for your input

Craig

--------------------------------------------------------------------------------------------------------
"Time-traveling is just too dangerous. Better that I devote myself to study the other great mystery of the universe: Women!" .. Dr E. Brown (1985)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top