How to Cancel Execution with Cancel Button? 
Author Message
 How to Cancel Execution with Cancel Button?

Form1 = your main form with a long processing routine
Form2 = progress/cancel dialogue.

Create a public property in Form2 called Cancel.

Public Property Get Cancel() As Boolean
    Cancel = fblnCancel
End Property

In your cancel button Click event, set the variable to true.

Private Sub cmdCancel_Click()
    fblnCancel = True
End Sub

In Form1, use this code in your processing routine:

Private Sub LongProcess()

    ' The combination of these two commands will simulate a modal dialogue
    Form1.Enabled = False    ' disable the main form
    Form2.Show , Form1    ' show the progress form

    ' The main routine
    Do While Form2.Cancel = False
        ' Do your processing here

    ' Always put things back the way you found them
    Unload Form2
    Set Form2 = Nothing
    Form1.Enabled = True
End Sub

John Tabor

http://www.*-*-*.com/ ~jftabor

Note:  Anti-spamming suffix of ".nospam" added to header.

>This method works OK, but it seems like there would be an easier way.  Any
>suggestion is much appreciated.

Sun, 21 Jan 2001 03:00:00 GMT  
 How to Cancel Execution with Cancel Button?

Re your problem.  I don't know if what I am suggesting is easier, but it is much
more standard.

Have the process run on a seperate thread from the calling process.  This will
allow you the flexibility to continue other operations in the program and allow
you to kill the thread if the user chooses cancel.

Not real easy to do and is definitely a mouthful for the uninitiated, but
multi-threaded applications are standard and have been used for a while to
accomplish just what you want.

Best of luck to you!

Ron Sparks

> I have a situation where I am running a routine and I have set up a cancel
> button to stop the execution of this routine.  Simple enough right? Wrong.
> The cancel button I am using is on a different form from the routine that
> is running.
> This method works OK, but it seems like there would be an easier way.  Any
> suggestion is much appreciated.

Sun, 28 Jan 2001 03:00:00 GMT  
 [ 3 post ] 

 Relevant Pages 

1. Q: When does a Cancel button *mean* Cancel?

2. cancel subform=cancel mainform

3. Error code 40087 - Execution canceled

4. Cancel Button on InputBox()

5. status bar and cancel button

6. Cancel button on Calendar Dialog form

7. Cancel button doesn't end macro

8. Detecting Cancel button in Insert Cross Reference dialog

9. "Cancel" Button

10. Cancel button gets Illegal operation error--Word 97

11. Multiple cancel buttons on multipage

12. No validation with Cancel-Button


Powered by phpBB® Forum Software