Go Premium for a chance to win a PS4. Enter to Win


Timer Application

Posted on 1998-11-25
Medium Priority
Last Modified: 2010-05-03
I am trying to make an application to time my working hours. Eg. - an app that sits in the tray, and when I tell it to start, it counts how many hours:minutes:seconds. My main poblem, is that it is never accurate enough, and the doevents function stops the timer. Anyone got some good source?
Question by:bubbles

Expert Comment

ID: 1446838
A suggestion:  Instead of trying to use the timer write a piece of code to have your app store the results of TIME = NOW() in a database table with one Start column and one End column.  Then all you have to do is subtract one from the other...

Author Comment

ID: 1446839
That's sort of what I want to do. I havn't actually been using the timer control. I want some code though (that works).

Expert Comment

ID: 1446840
The easiest way is the following: You define two variables of type "Date". When you start, you assign the current date and time to the first variable using the "now" function, and you do the same with the second variable at the end of the desired time.
You can calculate the difference with the DateDiff function.
I just tried the following example in VB 4 and it worked great.

-----------Begin code----------
Dim StartTime As Date
Dim StopTime As Date

Private Sub Start_Click()
    StartTime = Now
End Sub

Private Sub Stop_Click()
    StopTime = Now
    s = DateDiff("s", StartTime, StopTime)
    Me.Print s   'print elapsed time in seconds (see note below)
End Sub
-----------End code----------

The first parameter of the "DateDiff" function is the interval, this will determine in which units will the time (or date) difference be returned.

interval      Time interval
yyyy      Year
q      Quarter
m      Month
y      Day of year
d      Day
w      Weekday
ww      Week
h      Hour
n      Minute
s      Second

The other advantage is that you don't waste time: first with the timer, and second with the DoEvents function.

The version with a Timer control would be something like this.
Dim seconds As Long

-----------Begin code----------
Private Sub Start_Click()
seconds = 0
Timer1.Interval = 1000
End Sub

Private Sub Stop_Click()
Timer1.Interval = 0
Me.Print seconds
End Sub

Private Sub Timer1_Timer()
seconds = seconds + 1
End Sub
-----------End code----------

Hope this helped
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!


Author Comment

ID: 1446841
Your answer is not suitable. Using a timer control is not accurate enough, and the other mothod doesn't allow for a timer that updates each second. Sorry

Expert Comment

ID: 1446842
What do you mean, It's not accurate enough?... It's enough accurate for your purpose... any of the 2 possibilities.
1) They're accurate up to 50 miliseconds... Or do you really need to know your working hours up to one milisecond?
2) If you want to know your working hours, you don't need a timer to update every "n" time.... it's a waste of memory.

I don't understand why you rejected the answer... maybe I did not understand your purpose, so I'll ask you to explain.


Accepted Solution

bear454 earned 200 total points
ID: 1446843
I am helping someone with a similar problem - the solution is to use the Timer to update your value, but use the system time as the value. That is, you are not relying on the accuracy of Timer, only using it to tell the app when to update.  As long as it occurs withing any given second, you are accurate.

Here's some code:

The key is to crunch the math of the date data type. left of the decimal is the number of days, to the right is the time. For example 1/24 or ~0.04166666667 is 1 hour, or 1/86400 or ~0.00001157407 is 1 second.

Public StartTime as date
Timer1.interval = 250 'run every quarter-second

    Label1.Caption = Format(((Now - StartTime) \(1/24)),"00") & _
      ":" & Format(((Now - Starttime) \ (1/1440)),"00" & _
      ":" & Fomtat(((Now-Starttime) \(1/86400)),"00"
End Sub

For a little more info, check this question:

Author Comment

ID: 1446844
Thanks. That's more like what I'm after

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

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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…

916 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