Stop Timer

M

Michael

Hi Folks - I am using a Do While Loop to display the running time in a text
box. I would like to have a command button that stops the procedure. What
code do I need to stop a running procedure elegantly? I do not need to
calculate elapsed time. Thanks.

Mike

Here's the code:

Private Sub cmdUntilLoop_Click()

Dim intPause As Integer
Dim dblstart As Double

txtResults.SetFocus

intPause = Val(InputBox("Count how many seconds?", "Timer"))
dblstart = Timer

MsgBox Timer
Do Until Timer > (dblstart + intPause)

'if txtresults is not current time, then display current time

If txtResults.Text <> Format(Now, "Long Time") Then
txtResults.Text = Format(Now, "Long Time")
End If
Loop

End Sub
 
P

pietlinden

Hi Folks - I am using a Do While Loop to display the running time in a text
box. I would like to have a command button that stops the procedure. What
code do I need to stop a running procedure elegantly? I do not need to
calculate elapsed time. Thanks.

Mike

Here's the code:

Private Sub cmdUntilLoop_Click()

    Dim intPause As Integer
    Dim dblstart As Double

    txtResults.SetFocus

    intPause = Val(InputBox("Count how many seconds?", "Timer"))
    dblstart = Timer

    MsgBox Timer
    Do Until Timer > (dblstart + intPause)

       'if txtresults is not current time, then display current time

       If txtResults.Text <> Format(Now, "Long Time") Then
            txtResults.Text = Format(Now, "Long Time")
        End If
    Loop

End Sub

You need a cleanup procedure in a Goto event that cleans up all object
pointers etc. (like if you had a recordset open, you would need to
close and dereference the variable by setting it to nothing...)
 
D

Dirk Goldgar

Michael said:
Hi Folks - I am using a Do While Loop to display the running time in a
text box. I would like to have a command button that stops the procedure.
What code do I need to stop a running procedure elegantly? I do not need
to calculate elapsed time. Thanks.

Mike

Here's the code:

Private Sub cmdUntilLoop_Click()

Dim intPause As Integer
Dim dblstart As Double

txtResults.SetFocus

intPause = Val(InputBox("Count how many seconds?", "Timer"))
dblstart = Timer

MsgBox Timer
Do Until Timer > (dblstart + intPause)

'if txtresults is not current time, then display current time

If txtResults.Text <> Format(Now, "Long Time") Then
txtResults.Text = Format(Now, "Long Time")
End If
Loop

End Sub


You can generally halt a loop by pressing Ctrl+Break, unless that option is
disabled. But that's not something you design into your application.

A more elegant way is to have a public variable or control on the form that
can be set from outside the loop, and that will be tested in each iteration
of the loop to see if it's time to stop. In order to allow the user to set
that "Stop" variable or control, you'll need to put a DoEvents statement
inside the loop, so that Access can process user-interface events.

I also see a couple of things about your code worth considering:

1. The Timer() function returns the number of seconds since midnight, so if
you started this loop before midnight with an intPause value that would end
after midnight, the loop would never end.

2. There's probably no reason to set the focus to txtResults. You're
referring to the control's .Text property inside your loop, which does
require the control to have the focus, but you don't need to be using that
property. For most purposes you can use the .Value property, which doesn't
require the focus.

Here's an alternative form of your procedure, based on the idea that there
will be a command button, named cmdStop, to stop the timer.

'----- start of relevant module code -----

Dim mblnStopTimer As Boolean ' declared at module level

Private Sub cmdStop_Click()

mblnStopTimer = True

End Sub

Private Sub cmdUntilLoop_Click()

Dim intPause As Integer
Dim dtStop As Date
Dim strTime As String

mblnStopTimer = False

intPause = Val(InputBox("Count how many seconds?", "Timer"))

dtStop = DateAdd("s", intPause, Now())

Me.txtResults = Format(Now, "Long Time")

Do Until (Now() >= dtStop) Or mblnStopTimer
strTime = Format(Now, "Long Time")
If Me.txtResults <> strTime Then
Me.txtResults = strTime
End If
DoEvents
Loop

If mblnStopTimer Then
MsgBox "You stopped the timer."
Else
MsgBox "Time's up!"
End If

End Sub
'----- end of module code -----
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top