Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Timer

Posted on 2002-03-06
8
Medium Priority
?
295 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 53

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
Industry Leaders: 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 100 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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

609 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