Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Timer Application

Posted on 1998-11-25
7
Medium Priority
?
222 Views
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?
0
Comment
Question by:bubbles
[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
7 Comments
 

Expert Comment

by:tlw2651
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...
0
 

Author Comment

by:bubbles
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).
0
 
LVL 2

Expert Comment

by:trillo
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
Trillo
0
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

by:bubbles
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
0
 
LVL 2

Expert Comment

by:trillo
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.

Trillo
0
 
LVL 1

Accepted Solution

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

Timer1_Timer()
    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:
experts-exchange.com/topics/comp/lang/visualbasic/Q.10114745
0
 

Author Comment

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

Featured Post

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!

Question has a verified solution.

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

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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…
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…
Suggested Courses

722 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