|
-
September 8th, 2001, 03:35 PM
#1
Exiting from loops
I'm a complete beginner to VB programming, and I've enconutered this problem for which I need the solution urgently. A critical part of my application involves a lengthy for-next loop. I want a method by which the user can click on a command button and exit the loop at any time during which the the loop is running. Right now what happens is, when the user clicks a command button during the running of the loop, the loop continues to run, and only when it finishes does the subroutine associated with the command button take place.
Thanx in advance for any help received
Abhik Majumdar
-
September 8th, 2001, 04:12 PM
#2
Re: Exiting from loops
You need to add the DOEVENTS statement sporadically in your loop. This allows other events to take place outside your loop. THe outside procedure could set a switch telling the loop to abort if set.
A small example
option Explicit
Dim blAbort as Boolean
private Sub cmdStartLoop_Click()
Dim x
Do Until x = 1000
DoEvents
If blAbort then Exit Do
Loop
Unload me
End Sub
private Sub cmdStopLoop_Click()
blAbort = true
End Sub
John G
-
September 8th, 2001, 10:09 PM
#3
Re: Exiting from loops
It might be helpful to change it to a Do...Loop, if you know how, and then the Sentinel would be easy to set, plus a global variable could easily be checked every so often (at the beginning, middle and end) during the loop to see if you want to break it... in which case, you could execute the sentinel.
For...Loops are much better in C++.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|