Countdown Timer with reset button (1 Viewer)

oxicottin

Learning by pecking away....
Local time
Today, 06:51
Joined
Jun 26, 2007
Messages
851
Hello, I have a countdown timer that runs in the On_Load when the form opens and runs some code in the Form_Timer() after every 10 minutes. I also have a button cmdRunLogger that I need to reset the timer and run the code in the Form_Timer() again like it did when the form loaded. The cmdRunLogger button isnt doing anything but clicking once stops timer countdown and click again restarts timer but doesnt run the code again. What and where in whole process needs fixed?

Code:
Dim StartCount As Long
'
Private Function StartCounter()
    If Me.TimerInterval = 0 Then
            StartCount = 600 '10 Minutes in seconds
        Me.TimerInterval = 1000
    Else
        Me.TimerInterval = 0
    End If

End Function

Private Sub cmdRunLogger_Click()

    'Start the timer to run again in ??min
    Call StartCounter

End Sub

Private Sub Form_Load()

    'Start the timer to run again in ??min
    Call StartCounter

End Sub


Private Sub Form_Timer()
    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

      'Do my stuff I need to do***********************

        StartCount = 600  '10 Minutes in seconds
    End If
End Sub
 

CJ_London

Super Moderator
Staff member
Local time
Today, 10:51
Joined
Feb 19, 2013
Messages
16,553
there are 1000 milliseconds in a second, so your timer is running every 0.6 of a second. try 600000 for 10 minutes

misread the code so ignore this comment

the first time you click cmdlogger, the timer interval is not 0, so it sets it to 0
the second time will 'restart' the code

no obvious reason why the timer code does not run, have you put a break point in the code to see if it is triggered?
 
Last edited:

oxicottin

Learning by pecking away....
Local time
Today, 06:51
Joined
Jun 26, 2007
Messages
851
there are 1000 milliseconds in a second, so your timer is running every 0.6 of a second. try 600000 for 10 minutes
Ok thanks... But it still isnt doing what i needed it to do. It runs the timer when the form loads which is correct it runs again after 10 minutes which is correct The only thing that isn't correct is when I click the button to manually run the code in the form's timer it just stops the timer on one click on a second click it restarts the timer back at 10 minutes but never runs the code in a timer
 

oxicottin

Learning by pecking away....
Local time
Today, 06:51
Joined
Jun 26, 2007
Messages
851
I got it... In the button to restart and to run code again I just had to use:

StartCount = 0
 

oxicottin

Learning by pecking away....
Local time
Today, 06:51
Joined
Jun 26, 2007
Messages
851
Here is an example
 

Attachments

  • Database1.accdb
    504 KB · Views: 458

oxicottin

Learning by pecking away....
Local time
Today, 06:51
Joined
Jun 26, 2007
Messages
851
After creating the example I noticed the Form_Timer doesn't run upon opening the form then after it "Does stuff" start the countdown from 10 min and repeat.

What am I doing wrong?
 

theDBguy

I’m here to help
Staff member
Local time
Today, 03:51
Joined
Oct 29, 2018
Messages
21,357
After creating the example I noticed the Form_Timer doesn't run upon opening the form then after it "Does stuff" start the countdown from 10 min and repeat.

What am I doing wrong?
Not sure I follow. But if you're trying to reset the timer, try changing this part.
Code:
Private Sub cmdRunLogger_Click()

    'Start the timer to run again
    StartCount = 60

End Sub
Hope that helps...
 

oxicottin

Learning by pecking away....
Local time
Today, 06:51
Joined
Jun 26, 2007
Messages
851
@theDBguy I got it working with

Me.TimerInterval = 1000

In the on load event
 

Attachments

  • Database1_v2.accdb
    524 KB · Views: 446

Users who are viewing this thread

Top Bottom