Solved

Timer

Posted on 2002-03-06
8
291 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 52

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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

690 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