Solved

Timer

Posted on 2002-03-06
8
287 Views
Last Modified: 2012-05-04
Hello All,

I need to display a clock value in a label that starts at 00:00:00 and increases upward using a timer control. When a hit a button it goes back to zero (00:00:00 and starts over. Any ideas?

Thanks ADawn
0
Comment
Question by:ADawn
8 Comments
 
LVL 49

Expert Comment

by:Ryan Chong
ID: 6846138
You need a Date Time/ Long int Value, maybe GetTickCount API, format the Value into 00:00:00 use Timer to display result only ..
0
 
LVL 4

Expert Comment

by:trkcorp
ID: 6846166
'Add a timer(Timer1), label (label1) & button (command1) button

Option Explicit
Private lTlSecs As Long

Private Sub Timer1_Timer()
Dim nHrs As Integer
Dim nMins As Integer
Dim nSecs As Integer
Dim nWk As Integer
lTlSecs = lTlSecs + 1
'be sure to use integer divide symbol \ not /
nHrs = lTlSecs \ 3600
nWk = lTlSecs Mod 3600
nMins = nWk \ 60
nWk = nWk Mod 60
nSecs = nWk
Label1.Caption = CStr(nHrs) & ":" & Format$(nMins, "00") & ":" & Format$(nSecs, "00")
End Sub

Private Sub Command1_Click()
'reset timer
lTlSecs = 0
End Sub

0
 
LVL 4

Expert Comment

by:trkcorp
ID: 6846172
OOPS! Forgot to tell you to set Timer1.Interval = 1000
0
 
LVL 18

Accepted Solution

by:
deighton earned 25 total points
ID: 6846724
'WITH A TEXT BOX TEXT1, command buttons cmdstart and cmdreset + a timer timer1.





Option Explicit



Private Sub cmdReset_Click()

    Text1.Tag = CStr(Now)
    increment
   
End Sub

Private Sub cmdStart_Click()

    Timer1.Enabled = True
    Timer1.Interval = 1000
    Text1.Tag = CStr(Now)
    increment

End Sub

Private Sub Form_Load()

    Timer1.Enabled = False
   
End Sub

Sub increment()

    Text1 = Format(Now - CDate(Text1.Tag), "hh:mm:ss")

End Sub

Private Sub Timer1_Timer()

    increment
   
End Sub
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 4

Expert Comment

by:trkcorp
ID: 6847500
deighton, pretty elegant! I like that.
0
 
LVL 18

Expert Comment

by:mdougan
ID: 6847647
Just be aware that the thread priority of timer controls is lower than your average processes thread priority so, if your user is doing anything processor intensive during the timing, you probably wont get the Timer event to fire.  Or, it might fire inconsistently.  You'll still get the right elapsed time, it's just that you might not see the label update every second as you'd like.
0
 

Author Comment

by:ADawn
ID: 6847675
deighton
I like the simplicity of this code.

trkcorp
Your code work as expected, but much more complicated for me to understand.

Thanks to you both. ADawn
0
 
LVL 4

Expert Comment

by:trkcorp
ID: 6847828
ADawn, deighton's code is better and more accurate.  As mdougan points out, if there are other processes in your program interfering with the firing of the timer event, my timer code could become quite inaccurate as it may not fire every second on the second in this circumstance.  Deighton's approach, using current time as a baseline and getting the difference between that and NOW will always report an accurate lapse of time even if the timer event fails to fire at precisely every second.  Good choice, just wish I'd thought of it!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now