hello I want a timer in my Access database form using Visual Basic I have the code

dim i as integer=0
 timer1= i
 label1.Text = "Remaining time:" + CStr(2500 - j) + "seconds"

i = i + 1
 If i > 5000 Then
 Timer1.Enabled = False

End If

which works but I am confused on how to add more time while the time is counting down...such as a "add hour" button next to the "stop" button Also in the future I will be getting 1 hour instead of 5000 which is only 5 seconds..

Edited 2 Years Ago by geoffy0404


You need a variable to hold the current value and a second one to hold the "target" value...

The timer control fires depending on the interval you set for it in milliseconds.

dim iCount as integer 'Count or current value
dim iLimit as intger 'Limit value

sub Timer1_timer()
    iCount = iCount + 1
    if iCount >= iLimit then
        timer1.enabled = false
    End if
end sub

sub IncreaseLimit(byref Limit as Interval)
    iLimit = iLimit + Limit
end sub

As for what values to pass in, it depends how often you wish to check the values i.e. if you set the timer interval to 1000 (i.e. every second,) then inut your limit in seconds. 1 hour = 60 minutes = 3600 seconds

Hello, Thank you for your input. It turns out there is a way to reduce the amount of clock cycles while using a loop according to one of the programmers at my work, he suggested to use some sort of sleep function that just uses the system clock perhaps rather. Would you or anyone have any input on that?

I actually modified the code so it can use the time function instead I am still a little confused how to add a button to increment an hour of time.

Public Sub Time_counter()
   Dim Time As Date
   Dim Test_Limit
   Time = TimeValue(Now)
   Time = Time + 1

    If Time >= Test_Limit Then
        timer1.Enabled = False
        MsgBox ("Time is up")
            End If
End Sub

Edited 2 Years Ago by geoffy0404

code to add the wait time to current time

Dim fields() As String
Dim hours As Long
Dim minutes As Long
Dim seconds As Long

fields = Split(txtDuration.Text, ":")
hours = fields(0)
minutes = fields(1)
seconds = fields(2)

m_StopTime = Now
m_StopTime = DateAdd("h", hours, m_StopTime)
m_StopTime = DateAdd("n", minutes, m_StopTime)
m_StopTime = DateAdd("s", seconds, m_StopTime)

tmrWait.Enabled = True

code for the timer

Private Sub tmrWait_Timer()
Dim time_now As Date
Dim hours As Long
Dim minutes As Long
Dim seconds As Long

    time_now = Now
    If time_now >= m_StopTime Then
        Me.WindowState = vbMaximized
        tmrWait.Enabled = False
        lblRemaining.Caption = "0:00:00"
        seconds = DateDiff("s", time_now, m_StopTime)
        minutes = seconds \ 60
        seconds = seconds - minutes * 60
        hours = minutes \ 60
        minutes = minutes - hours * 60

        lblRemaining.Caption = _
            Format$(hours) & ":" & _
            Format$(minutes, "00") & ":" & _
            Format$(seconds, "00")
    End If
End Sub
This article has been dead for over six months. Start a new discussion instead.