Stop a procedure from running with a click event

ajetrumpet

Banned
Local time
Today, 08:42
Joined
Jun 22, 2007
Messages
5,638
Guys,

How do I terminate a procedure with the "OnClick" event of a button? I have a public function running through 60 short code loops, and I want to terminate the process if a button is clicked...
 
You can try adding

DoEvents

within your code, which yields control to the OS during code execution. Not sure if it will work in your situation, but it's worth a try.
 
I already have the DoEvents line in the Called Function, just like the help menu tells you to do.

I also used one of GHudson's samples from a previous thread, as he had copied the help menu's instructions and put it into practice.

I'm obviously doing a countdown here, but I want the countdown (the loop) to stop running if I push the cancel button...
 
It may be possible to declare some public boolean variable and (as pbaldy said) use the DoEvents command:

Code:
Public RunLoop as Boolean

Sub MyLoop()

Do While RunLoop And .... (other condition)
    ...
    ...
    ...
    DoEvents
Loop
RunLoop = False

End Sub

On your form:

Code:
Private Sub MyStartButton_OnClick()
RunLoop = False
End 

Private Sub MyStopButton_OnClick()
RunLoop = True
Call MyLoop()
End

I haven't tried it but at a glance it looks like it could make sense?

Pete.
 
I wrote a stopwatch type thing in VB6 as practice. I'll try and dig it up to see what I did there.
 
OK Guys, I got it to work. Here is the Form's module:
Code:
Private i As String
  Private RunLoop As Boolean
[COLOR="Red"]****************************************[/COLOR]
Public Function CountdownDISPLAY()

i = "10"

  Do While RunLoop = True
    Me.lblseconds.Caption = i
      CountDown (1)
        i = i - 1

          If i = 0 Then
            RunLoop = False
          End If
  Loop

    DoCmd.Close

End Function
[COLOR="Red"]****************************************[/COLOR]
Private Sub cmdcancel_Click()

  RunLoop = False

End Sub
[COLOR="Red"]****************************************[/COLOR]
Private Sub Form_Load()

  Me.TimerInterval = 1000
    Me.lblseconds.Caption = "10"
      RunLoop = True

End Sub
[COLOR="Red"]****************************************[/COLOR]
Private Sub Form_Timer()

  Call CountdownDISPLAY

End Sub
What do you think of this? It works pretty darn good! :)
 

Users who are viewing this thread

Back
Top Bottom