Timer Application

Posted on 1998-11-25
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
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

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


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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

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 …
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…

724 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