If youre program is based on a loop (a very important one), how can i user stop the loop with the click of a button?
Printable View
If youre program is based on a loop (a very important one), how can i user stop the loop with the click of a button?
declare a public variable loopstop as boolean
public loopstop as boolean
sub Stop_click()
loopstop = true
end sub
in your loop function write this tag:
do until loopstop = false
.
..
...
loop
or
while loopstop = false
.
..
wend
hope this helps
This can be done by geting the loop to check for events at the end of each cycle.
I wipped up an app that test it.
Just start a new project put to buttons on it. Command 1 starts the loop. Command2 then stops it and displays the counter value
Good Luck
Option Explicit
Private blnstop As Boolean
Private lngCounter As Long
Private Sub Command1_Click()
blnstop = True
End Sub
Private Sub Command2_Click()
For lngCounter = 0 To 100000000
If blnstop Then
MsgBox "Stopped at " & lngCounter
blnstop = False
Exit For
End If
DoEvents
Next lngCounter
End Sub
sorry
on that first loop, it should be:
do until loopstop = TRUE
loop
:)Code:Private Sub Command1_Click()
Dim x
For x = 1 To 100000
If Me.Check1.Value = 0 Then
Exit For
Else
Print x
End If
Next
End Sub
How abt this?
Code:Dim X As Integer
Do While X < 1000
X = X + 1
DoEvents
Loop