I want to do a
Do Until Loop
stopping when a command button is clicked.
Printable View
I want to do a
Do Until Loop
stopping when a command button is clicked.
Code:whoops: you asked for a do until...
well, easy:
'on top of form
dim blnStop as boolean
'--------------------------------
'where you need the loop
Do Until blnStop = True
DoEvents
Loop
blnStop= False
'--------------------------------
'on click of that button:
Private Sub Command1_Click()
blnStop = true
End Sub
'*********************
'Previous code suggested used "Do While"
'on top of form
dim blnStop as boolean
'--------------------------------
'where you need the loop
do while blnStop = false
doevents
loop
blnStop= False
'--------------------------------
'on click of that button:
Private Sub Command1_Click()
blnStop = true
End Sub
'--------------------------------
a loop cannot be stoped directly.... u can create threads and that is possible in .net so in vb6 and below u need to add a check..
in the loop add a check (a varible)
on button clcik change the value of the check variable
have tried this but unable to get out of loop!
sorry... i think that can be done in diff eventQuote:
Originally posted by tottnik
have tried this but unable to get out of loop!
as i said that is possible in .net vb is a single thread so untill ur loop completes nothing can be done... i am not sure try using a timer and change the value of the check variable
Made a little app, and this seems to work fine.
Place this behind a form with 2 buttons and a label:
Option Explicit
Dim b As Boolean
Private Sub cmdStop_Click()
b = False
End Sub
Private Sub Command1_Click()
b = True
Dim i As Long
Do
i = i + 1
Me.Label1.Caption = i
DoEvents
Loop Until b = False
End Sub
Greetz,
Luc
DoEvents is the way to get this done. It will pause execution to process other events like button presses.
You should have some kind of a delay in your loop to prevent draining the PC's resources...
J.A.T.
Thanks everyone, I have tried your code Luc and it works though there seems some delay between clicking cmdStop and the loop stopping.
Shaggy i really wanted to stop execution of loop rather than just pause it.
Cheers everyone