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

Cancel looping statement

Status
Not open for further replies.

sisan

Programmer
Jan 9, 2002
39
ID
Hi...

My procedure contains For... Next statement.
How can I stop or interupt the loop by pressing the Escape key while the loop run ?

Thank you.
 
You have to set a flag for your loop and monitor for the keypress event of the currently focused object. The Ascii Code for the ESC key is 27.

Should be something like this:

Private s As Long
Private Sub Command1_Click()
s = 1
i = 0
Do While s = 1
Text1.Text = i
i = i + 1
DoEvents
Loop
End Sub

Private Sub Command1_KeyPress(KeyAscii As Integer)
If KeyAscii = 27 Then s = 0
End Sub
 
You can't interrupt any ongoing process, such as a
loop, except by placing the line DoEvents in an
iteration. DoEvents causes the program to temporarily stop,
see if there are any other commands that also need doing,
and work on those for a while.
Otherwise, your PC is so busy executing the loop that
it never has time to see your "Halt!" command.
 
DoEvents switches control to the operating-environment kernel. Control returns to your application as soon as all other applications in the environment have had a chance to respond to pending events. This doesn't cause the current application to temporarily stop or give up the focus, but it does enable background events to be processed.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top