Update Elapsed Time Every Second

G

Guest

I'm writing a race timing application and I want to design a form whereby
after I click on the "Start Timing" button, my time field will auto update
every
second to show the elapsed time since I hit "Start Timing".

Here's the code I already wrote against the "Start Timing" button:

Private Sub Start_Time_Click()
StartTime = Timer()
End Sub

My Time field is a text box titled "CurTime".
 
A

Allen Browne

1. Set the form's TimerInterval to 1000 (that's 1000 milliseconds.)

2. Leave the CurTime text box unbound (nothing in its Control Source.)

3. Something like this in the form's module:

Option Compare Database
Option Explicit

Dim mvarStartTime As Variant

Private Sub Form_Timer()
Dim lngSeconds As Long
With Me.CurTime
If IsDate(mvarStartTime) Then
lngSeconds = DateDiff("s", mvarStartTime, Now())
.Value = lngSeconds \ 60 & Format(lngSeconds Mod 60, "\:00")
Else
If Not IsNull(.Value) Then
.Value = Null
End If
End If
End With
End Sub

Private Sub Start_Time_Click()
mvarStartTime = Now()
End Sub

Private Sub Stop_Time_Click()
mvarStartTime = Null
Me.CurTime = Null
End Sub

--
Allen Browne - Microsoft MVP. Perth, Western Australia.

Reply to group, rather than allenbrowne at mvps dot org.

message
news:[email protected]...
 
G

Guest

Thanks. That helped.

Allen Browne said:
1. Set the form's TimerInterval to 1000 (that's 1000 milliseconds.)

2. Leave the CurTime text box unbound (nothing in its Control Source.)

3. Something like this in the form's module:

Option Compare Database
Option Explicit

Dim mvarStartTime As Variant

Private Sub Form_Timer()
Dim lngSeconds As Long
With Me.CurTime
If IsDate(mvarStartTime) Then
lngSeconds = DateDiff("s", mvarStartTime, Now())
.Value = lngSeconds \ 60 & Format(lngSeconds Mod 60, "\:00")
Else
If Not IsNull(.Value) Then
.Value = Null
End If
End If
End With
End Sub

Private Sub Start_Time_Click()
mvarStartTime = Now()
End Sub

Private Sub Stop_Time_Click()
mvarStartTime = Null
Me.CurTime = Null
End Sub

--
Allen Browne - Microsoft MVP. Perth, Western Australia.

Reply to group, rather than allenbrowne at mvps dot org.

message
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top