count Time on click of a button!!

johnyjassi

Registered User.
Local time
Today, 14:19
Joined
Jun 6, 2008
Messages
64
Hi Experts!

I am designing the form where i need to put a timer on each call, It should be like, when I click button the time should start for 20 mins, it should show in this format "mm:ss" and need to change the color of the time when it reaches 15 mins. If it could be like countdown, that would be great!! Any help will be appreciated.
Thanks in advance
 
Here are some functions that might be of interest to you. I have used them before (they are all behind the same form that serves as a countdown timer of 60 seconds):
Code:
Public Function CountdownDISPLAY()

i = "60"

  Do While RunLoop = True

    If Len(i) = 2 Then

      If i = -1 Then
        [COLOR="Red"]Call RecordTime[/COLOR] (this function stops the timer, and is probably irrelevant to you)
      Else
        Me.lblseconds.Caption = ":" & i
      End If
      
    Else
      Me.lblseconds.Caption = ":0" & i
    End If

      [COLOR="Red"]CountDown (1)[/COLOR]
        i = i - 1

  Loop

End Function
____________________________________________________________________

Private Sub Form_Load()

  RunLoop = True
    Me.TimerInterval = 10
      Me.lblseconds.Caption = ":60"

End Sub
_______________________________________________________________________

Private Sub Form_Timer()

  Me.TimerInterval = 0
    Call CountdownDISPLAY

End Sub
Here is the function located in an outside module that is called from the DISPLAY code:
Code:
Public Function CountDown(NumSeconds As Variant)

Dim start As Variant
  start = Timer

  Do While Timer < start + NumSeconds
    DoEvents
  Loop

End Function
 
I think this will do what you want.

The button to start the timer:

Code:
Private Sub CallTimer_Click()
  Me.TimerInterval = 1000
End Sub
The time left is written to a textbox named TimeLeft. With 5 minutes left, the background turns Red. When the TimeLeft = 0:00, the countdown stops and the background of the textbox turns White again.
Code:
Private Sub Form_Timer()

Static StartTime As Date
Static Loops As Integer

If Loops = 0 Then StartTime = Time
 
 Min = (1200 - DateDiff("s", StartTime, Time)) \ 60
 Sec = (1200 - DateDiff("s", StartTime, Time)) Mod 60
 Me.TimeLeft = Min & ":" & Format(Sec, "00")
 Loops = Loops + 1
 If Me.TimeLeft = "5:00" Then Me.TimeLeft.BackColor = vbRed
 If Me.TimeLeft = "0:00" Then
   Me.TimeLeft.BackColor = vbWhite
   Me.TimerInterval = 0
   Loops = 0
 End If

End Sub
 
The solution provided by missinglinq is slightly better IMHO becuase of the use of TimerInterval of the form. This keeps your CPU much less bussy.
 
Hi Guys,

Just back to work. I tried missinglinq's solution and it works pretty well. Thank you alot guys! I appreciate your help.
 

Users who are viewing this thread

Back
Top Bottom