Link to home
Start Free TrialLog in
Avatar of JARichard
JARichardFlag for United States of America

asked on

Access 2007 StopWatch on Form

So this is what I have.  Via Google I found what I needed as far a stopwatch to go onto a form I have for calculating the total time of an event.  I downloaded the db containing this "Stopwatch" form, opened it, and exported that one form to MY db.  Although both Datatbases are 2007 compatible, the form doesnt seem to work the same when put into MY db.  

As a form by itself in my db, it works fine.  I can open the form, press all the buttons and they do exactly as they should.  The stopwatch that I want, however, needs to be embedded into a form I already have.  When I simply copy the elements from the "Stopwatch form" and put them into my already present form, I get errors.  I fixed a few of them, but the main one now appears to be that the timer doesn't actually count, even though as a standalone form it does it just fine.  The coding on the buttons, and the OnTimer event is as follows:

Private Sub btnResetTime_Click()
Dim LngNumOfSecs As Long
Me.TimerInterval = 0
Me![lblTime].Caption = "00:00:00"
LngNumOfSecs = 0
End Sub


Private Sub btnStartTime_Click()
Me.TimerInterval = 1000
End Sub


Private Sub btnStopTime_Click()
Me.TimerInterval = 0
End Sub


Private Sub Form_Timer()
Dim lngNumOfHrs As Long
Dim lngNumOfMins As Long
Dim lngNumOfSecsRem As Long
Dim LngNumOfSecs As Long

LngNumOfSecs = LngNumOfSecs + 1

Select Case LngNumOfSecs
  Case Is > 86400        '>1 day - not equipped for that
  Case Is >= 3600        '>1 hour
    lngNumOfHrs = LngNumOfSecs \ 3600
    lngNumOfMins = ((LngNumOfSecs - (lngNumOfHrs * 3600)) \ 60)
    lngNumOfSecsRem = LngNumOfSecs - ((lngNumOfHrs * 3600) + (lngNumOfMins * 60))
  Case Is >= 60          '>1 minute
    lngNumOfMins = ((LngNumOfSecs - (lngNumOfHrs * 3600)) \ 60)
    lngNumOfSecsRem = LngNumOfSecs - ((lngNumOfHrs * 3600) + (lngNumOfMins * 60))
  Case Is > 0            '< 1 minute
    lngNumOfSecsRem = LngNumOfSecs - ((lngNumOfHrs * 3600) + (lngNumOfMins * 60))
  Case Else              'shouldn't happen, but who knows?
End Select

Me![lblTime].Caption = Format$(lngNumOfHrs, "00") & ":" & Format$(lngNumOfMins, "00") & _
                             ":" & Format$(lngNumOfSecsRem, "00")
End Sub

Open in new window


The timer interval on the form by default is set to 0.

I would really appreciate if someone adept in coding could tell me why this isn't working when placed within a form, but works when in its own form, and how to make it work embedded in my form.  All it does now is when you click [btnStartTime] the [lblTime] displays 00:00:01, and keeps flickering at the currect interval as if it should be counting, but its not changing at all.  The [btnStopTime] works, at least the flickering stops, so that tells me its stoping the cycles even if the time isn't acutally changing.  and [btnResetTime] sets [lblTime] to 00:00:00 as it should.

Thank you in advance for all your assistance.
Avatar of Rey Obrero (Capricorn1)
Rey Obrero (Capricorn1)
Flag of United States of America image

upload a copy of the db
Avatar of JARichard

ASKER

Here it is.  The form with my Stop Watch is "Call Details"  the form with the working one is "Stop Watch (Working Example).
SB.accdb
ASKER CERTIFIED SOLUTION
Avatar of mbizup
mbizup
Flag of Kazakhstan image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Its amazing something that simple makes the whole code work differently.  Learning something new with every step.  Thank you.
Glad to help out!

Another slightly cleaner option that I should have thought of earlier is to declare the variable as Static.  The is another way of remembering the value between calls to your timer event, while keeping the variable definition together with the rest of the function:

Private Sub Form_Timer()
Dim lngNumOfHrs As Long
Dim lngNumOfMins As Long
Dim lngNumOfSecsRem As Long
Static lngNumOfSecs As Long

Open in new window