Solved

Timer Application

Posted on 1998-11-25
7
214 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
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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

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

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
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…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

758 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now