Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Time Counter/Stop watch

Status
Not open for further replies.

pleg12345

IS-IT--Management
Dec 21, 2003
48
EU
Please can somebody help me?

I need to build a time counter in short-time with a button to stop and start the time.

The time counter would count up from 00:00 up to say 99:59?

Please can someone advise?
 
Hi,

Try this:

(I will put the names I have used in brackets).

Create a label on the form (lblDisplay). Create a Start/Stop command button (cmdStopStart), and (maybe) a reset command button (cmdReset).

Then add the following code to your form:
Code:
[COLOR=blue]
Dim datCurrent As Date
Dim blnCount As Boolean

Private Sub [b]cmdReset[/b]_Click()
    datCurrent = #00:00#
    [b]lblDisplay[/b].Caption = Format(datCurrent, "Short Time")
End Sub

Private Sub [b]cmdStopStart[/b]_Click()
    If Not blnCount Then
        [b]cmdStopStart[/b].Caption = "Stop"
    Else
        [b]cmdStopStart[/b].Caption = "Start"
    End If
    blnCount = Not blnCount
End Sub

Private Sub Form_Timer()
    If blnCount Then
        datCurrent = DateAdd("n", 1, datCurrent)
        [b]lblDisplay[/b].Caption = Format(datCurrent, "Short Time")
    End If
End Sub[/color]

Set the Timer Interval on the Forms property sheet on the events tab to 60000, so it will fire once every 60 secs.

For added effect you could set the label controls properties (on property sheet) to make it look like a real clock:

Caption - 00:00
Fore Color - 65280
Back Color - 0
Text Align - Center
Special Effect - Sunken

Hope I haven't forgotten anything.
Good luck,

Dean :)
 
cheers for the code Dean, but there is a problem!

The Timer does not start for some reason?

Can you help?
 
In the OnTimer event, do you have [Event Procedure] selected in the property sheet for the form.

 
I just forgot to explicitly mention setting that in my original post and thought you may not have...

Anyway, is it still not working then? You definitely have the Timer Interval Set to 60000?


Actually, when I tested this I included seconds which made it more obvious that the timer was running. You said you wanted the time in a Short-Time format. So the above set up only counts minutes. Maybe you wanted seconds and minutes?

If so, make the following changes to these functions:
Code:
Private Sub cmdReset_Click()
    datCurrent = #00:00[b]:00[/b]#
    lblDisplay.Caption = Format(datCurrent, "[b]Long[/b] Time")
End Sub

Private Sub Form_Timer()
    If blnCount Then
        datCurrent = DateAdd("[b]s[/b]", 1, datCurrent)
        lblDisplay.Caption = Format(datCurrent, "[b]Long[/b] Time")
    End If
End Sub
Also Change the Timer Interval to 1000 and the original caption of the display label to "00:00:00".

Maybe that's what you wanted - I thought it was a bit strange you didn't want seconds on it.

Dean.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top