Click to See Complete Forum and Search --> : Cancel while in process


rlecjr
August 14th, 2001, 10:40 AM
I have a section of code that retrieves a number (variable amount) of records from a database. Upon retrieval, the code loops through the records one by one and performs a number of functions with these records. This can sometimes take too long for an impatient user and they want to cancel out of the operation. What is the best to allow the user to press a cancel button left on the screen? Usually, when I am executing code, there is no interaction by the user allowed... Any suggestions would be greatly appreciated!!

Thank you.

d.paulson
August 14th, 2001, 01:35 PM
Use of the keyword DoEvents in your loop will allow execution of your 'Cancel' button.

David Paulson

John G Duffy
August 14th, 2001, 08:29 PM
Small example of Davids suggestion. Start a new project. Add two command buttons. Paste this code into general declarations section. Run it. Click Command1 which starts a long loop.
Click command2 to stop it.

option Explicit
'
Dim blCancel as Boolean
'
private Sub Command1_Click()
'
LongLoopSub ' start a long loop
MsgBox "Long Loop ended"
'
End Sub

public Sub LongLoopSub()
'
Dim Something as string
blCancel = false
Do Until Something = "Hello there"
'
DoEvents
If blCancel then Exit Sub
'
Loop
'
End Sub

private Sub Command2_Click()
'
blCancel = true
'
End Sub




John G