-
I'd like to add a Cancel button to a form whereby I could interrupt a certain program task, typically a time-consuming one, for example:
for i1=1 to largeNumber1
for i2=1 to LargeNumber2
WorkOnSomethingCumbersome
next
next
How can I have the cancel button take me out of the loops and to the exit point of the procedure?
-
Couldn't you have something like this? There is probably a more efficient way but:
Code:
Dim Canceled as Boolean
Private Sub cmdCancel_Click()
Canceled = True
End Sub
THEN
Code:
for i1=1 to largeNumber1
for i2=1 to LargeNumber2
If Canceled = True Then Exit For ' or exit sub/function
WorkOnSomethingCumbersome
next i2
-
Dont forget the DoEvents
That code looks good, but don't forget to put DoEvents in your code to give the user the opportunity to click on the cancel button. Also disable all other things he may not use during the proces. But you probably thought about this anyway.