Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Minutes Seconds Countdown Timer With Pause and Continue

Status
Not open for further replies.

oxicottin

Programmer
Jun 20, 2008
353
US
Ok I have a label that counts down from 10 minutes then runs some code in a module then the timer resets back to to minutes and it keeps running untill I shut the form down. I have two issues here and I cant figure out how to do them or what to add into the VBA.

Issue one:
The button on my form starts the countdown of a ten minute timer then runs the code as explained above. I want to be able to on the first time I hit the cmdRunLogger button I want to run the events in the module then start the countdown and do a continuous cycle there like I already have.

Issue two:
I want to add a stop and I figure once issue one is resolved I would be able to just use "sTimerOn = False" Correct? BUT what if I wanted to pause the timer, how would I do that in a button and resume it?

Code:
Public Loops As Integer
Dim sTimerOn As Boolean

Private Sub cmdRunLogger_Click()
    sTimerOn = True
End Sub

Private Sub Form_Timer()
    Static StartTime As Date
    Dim SecondsToCount As Integer

    SecondsToCount = 600    'Set this variable to the total number of seconds to count down

    If sTimerOn = True Then
        If Loops = 0 Then StartTime = Time

        Min = (SecondsToCount - DateDiff("s", StartTime, Time)) \ 60
        Sec = (SecondsToCount - DateDiff("s", StartTime, Time)) Mod 60
        Me.lblCountDown.Caption = "Next Table Update " & Min & ":" & Format(Sec, "00")
        Loops = Loops + 1

        If Me.lblCountDown.Caption = "Next Table Update 0:00" Then
            'Run functions in modUtilities
            'DO SOME STUFF
            Loops = 0    'Resets Timer
        End If
    End If
End Sub

I did however create a button called cmdPausePLCLogger and tried the below code but it just goofed up the loop numbers... But the buttons fipping back and forth is what I wanted.

Code:
Private Sub cmdPausePLCLogger_Click()

If Me.cmdPausePLCLogger.Caption = "Pause" Then
   Me.cmdPausePLCLogger.Caption = "Resume"
   Me.txtHiddenLoops.Value = Loops
   sTimerOn = False
   
    ElseIf Me.cmdPausePLCLogger.Caption = "Resume" Then
    Me.cmdPausePLCLogger.Caption = "Pause"
    sTimerOn = True
    Loops = Me.txtHiddenLoops.Value
    Me.txtHiddenLoops = Null
    End If
  
End Sub



Thanks,
SoggyCashew.....
 
Once your Form_Timer starts, it will run untill done - no matter what you do to sTimerOn because Form_Timer never has a chance to check the value of sTimerOn if set outside Form_Timer event once started.

You may want to include a DoEvents in Form_Timer

Have fun.

---- Andy

A bus station is where a bus stops. A train station is where a train stops. On my desk, I have a work station.
 
How are ya oxicottin . . .

Are we not talking a stop watch with reset? The following should show how its done ...

Create a StopWatch

See Ya . . .

Be sure to see FAQ219-2884 Worthy Reading! [thumbsup2]
Also FAQ181-2886 Worthy Reading! [thumbsup2]
 
Hello AceMan!

I needed something that would show a countdown from 10:00 and at 00:00 do something like a requery then reset the counter and start counting down from 10:00 again. But i would like to have the ability to pause and resume the coutdown. On a side note when the form open and the first time i press the "start countdown" button how can i get it to do the requery or events then start the countdown.

Thanks

Thanks,
SoggyCashew.....
 
When you Pause the timer, you have to START a SecondsPause counter and add that to your SecondsToCount, so that when you Resume the timer, it will account for the pause time.

BTW, all you're doing is delaying the next time you run your functions.

Skip,
[sub]
[glasses]Just traded in my OLD subtlety...
for a NUance![tongue][/sub]
 
oxicottin . . .

Try the following in your forms module. I setup the buttons from the link in my prior post, along with your label [blue]lblCountDown[/blue]. Works for me.

Code:
[blue]Option Explicit
Option Compare Database

Dim StartCount As Long, flgStart As Boolean

Private Sub btnReset_Click()
   flgStart = False
   Me!lblCountDown.Caption = "Next Table Update 10:00"
   StartCount = 600

End Sub

Private Sub btnStartStop_Click()
   If Me.TimerInterval = 0 Then
      If Not flgStart Then
         StartCount = 600
         Me!btnStartStop.Caption = "Stop"
         Me!btnReset.Enabled = False
         flgStart = True
      End If
      
      Me.TimerInterval = 1000
   Else
      Me.TimerInterval = 0
      Me!btnStartStop.Caption = "Start"
      Me!btnReset.Enabled = True
   End If

End Sub

Private Sub Form_Timer()
   'Dim Hours As String
   Dim Min As String
   Dim Sec As String
   Dim MilliSec As String
   Dim Msg As String
   Dim ElapsedMilliSec As Long
   
   StartCount = StartCount - 1
   Min = StartCount \ 60
   Sec = StartCount - (60 * Min)
   Me.lblCountDown.Caption = "Next Table Update " & Min & ":" & Format(Sec, "00")
   
   If StartCount <= 0 Then
      MsgBox "[purple][b]Your Functions Here! ...[/b][/purple]"
      StartCount = 600
   End If

End Sub[/blue]

Cheers! ...

See Ya . . .

Be sure to see FAQ219-2884 Worthy Reading! [thumbsup2]
Also FAQ181-2886 Worthy Reading! [thumbsup2]
 
AceMan, that worked perfectly..... Thank You very much! I have been trying to figure this out for days.....

Thanks,
SoggyCashew.....
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top