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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can I make a stopwatch work when a form opens? 1

Status
Not open for further replies.

MikeFL

Programmer
Jul 12, 2002
58
US
A friend of mine sent be the following code that works with a form only after the user clicks the start button, stops when they click the stop button and resets to 0 when they click the rest button.
.............................................
Option Compare Database
Option Explicit

Dim TotalElapsedMilliSec As Long
Dim StartTickCount As Long
Private Declare Function GetTickCount Lib "kernel32" () As Long


Private Sub btnStartStop_Click()
If Me.TimerInterval = 0 Then
StartTickCount = GetTickCount()
Me.TimerInterval = 15
Me!btnStartStop.Caption = "Stop"
Me!btnReset.Enabled = False
Else
TotalElapsedMilliSec = TotalElapsedMilliSec + (GetTickCount() - StartTickCount)
Me.TimerInterval = 0
Me!btnStartStop.Caption = "Start"
Me!btnReset.Enabled = True
End If
End Sub


Private Sub Form_Timer()
Dim Hours As String
Dim Minutes As String
Dim Seconds As String
Dim MilliSec As String
Dim Msg As String
Dim ElapsedMilliSec As Long

ElapsedMilliSec = (GetTickCount() - StartTickCount) + TotalElapsedMilliSec

Hours = Format((ElapsedMilliSec \ 3600000), "00")
Minutes = Format((ElapsedMilliSec \ 60000) Mod 60, "00")
Seconds = Format((ElapsedMilliSec \ 1000) Mod 60, "00")
MilliSec = Format((ElapsedMilliSec Mod 1000) \ 10, "00")

Me!ElapsedTime = Hours & ":" & Minutes & ":" & Seconds & ":" & MilliSec

End Sub

Private Sub btnReset_Click()
TotalElapsedMilliSec = 0
Me!ElapsedTime = "00:00:00:00"
End Sub
..................................................
I need to have the stopwatch run continually when the form it’s embedded in opens for the users to view the continuous elapsed time a form/process was open. Then after the form is closed I need it to stop and rest to 0.

Can this be accomplished or is there and easier way?


 
Delete the btnStartStop_Click and btnReset_Click events.

Use this as your Form_Activate Event
[tt]
Private Sub Form_Activate()
StartTickCount = GetTickCount()
Me.TimerInterval = 15
End Sub
[/tt]

and this in Form_Unload
[tt]
Private Sub Form_Unload(Cancel As Integer)
TotalElapsedMilliSec = 0
Me!ElapsedTime = "00:00:00:00"
End Sub
[/tt]

Leave the rest of the code As IS.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top