[AccessD] Elapsed time class - Stuart please ignore...

jwcolby jwcolby at colbyconsulting.com
Sat Jul 16 08:35:28 CDT 2011


I'm building a little database to time / log my son's computer time.  I have a form which he uses to 
select a game which opens the game for him, creates a record in the table and starts the timer, 
leaving this form up in the background behind the game.  As he closes the game the form becomes 
visible and he clicks a button to update the stop time in the table and stops the elapsed time timer.

Not yet done, I will be causing the form to beep at him as he goes over the allotted time to play 
the game.

Nothing special here, just wrapping the code / date I needed to measure elapsed time into a class. 
Obviously the point of putting it in a class is that it can be used in one or a hundred forms to 
perform elapsed time calcs, and it carves out all of that data / logic into a single place.

To use it in any form just dimension a variable and set it to a new instance of the variable:

Dim mcElapsedTime As clsElapsedTime

Private Sub Form_Open(Cancel As Integer)
     Set mcElapsedTime = New clsElapsedTime
     Me.TimerInterval = 1000	- set up the timer for one second timer ticks
End Sub

Private Sub Form_Timer()
     mcElapsedTime.UpdateElapsedTime
     txtElapsedTime = mcElapsedTime.pElapsedTime
End Sub



The elapsed time class:

Option Compare Database
Option Explicit

Private mlngSeconds As Long
Private mlngMinutes As Long
Private mlngHours As Long
Private mstrElapsedTime

Property Get pElapsedTime() As String
     pElapsedTime = mstrElapsedTime
End Property
Property Get pSeconds() As Long
     pSeconds = mlngSeconds
End Property
Property Get pMinutes() As Long
     pMinutes = mlngMinutes
End Property
Property Get pHours() As Long
     pHours = mlngHours
End Property

Private Function CalcElapsedTime()
     mstrElapsedTime = mlngHours & ":" & mlngMinutes & ":" & mlngSeconds
End Function
Function mResetElapsedTime()
     mlngSeconds = 0
     mlngMinutes = 0
     mlngHours = 0
End Function
Function UpdateElapsedTime()
     mlngSeconds = mlngSeconds + 1
     If mlngSeconds >= 60 Then
         mlngSeconds = 0
         mlngMinutes = mlngMinutes + 1
     End If
     If mlngMinutes >= 60 Then
         mlngMinutes = 0
         mlngHours = mlngHours + 1
     End If
     CalcElapsedTime
End Function

-- 
John W. Colby
www.ColbyConsulting.com



More information about the AccessD mailing list