Home > Software engineering >  Countdown with TimerInterval in VBA for MSAccess
Countdown with TimerInterval in VBA for MSAccess

Time:11-19

I want a timer that counts down from say, 05:00, or 5 minutes.

This countdown happens near instantaneously rather than one second increments.

I added the debug.print to confirm that the interval was set at 1000 milliseconds. Note, X is set to 1 elsewhere in the code.

Sub Form_Timer()
    
    Do While Me.DisplayTimer > Format(TimeSerial(0, 0, 0), "nn:ss")
        Debug.Print Me.TimerInterval
        
        Me.DisplayTimer = Format((CountDownTime - 1.15740740740741E-05 * X), "nn:ss")
        X = X   1
            
    Loop
                      
    Forms!TimerLogF.TimerInterval = 0
    Call LogProcess
            
End Sub

CodePudding user response:

Always handle date and time as Date.

Also, the form and the timer interval is the loop. So:

Private Sub Form_Timer()

    ' Me!DisplayTimer holds, say, "5:00".
    ' Me!DisplayTimer has the property Format set to: n:ss
    ' Me.TimerInterval property is set to, say, 1000.

    Dim ThisTime    As Date

    ThisTime = TimeValue(Me!DisplayTimer.Value)
    ThisTime = DateAdd("s", -Me.TimerInterval / 1000, ThisTime)
    Me!DisplayTimer.Value = ThisTime
    
    If ThisTime <= TimeSerial(0, 0, 0) Then
        Me.TimerInterval = 0
        Call LogProcess
    End If

End Sub

CodePudding user response:

Ok I figured this out. The entire looping structure was INSIDE the form timer, meaning it would run all that code once per second; it was NOT checking for an outside variable once per second and decrementing.

  • Related