Stopping a Timer

M

MikeS

I have a simple program that refreshes a form once every minute ( or what
ever increment I decide) It is basically two functions - one that requeries
the data and updates the form and one that does the timer.

Private Function RefreshData()
Dim intCountLine0 As Integer, intCountLine2 As Integer, intCountLine3 As
Integer

If IsNull(intCountLine0 = DLookup("[Count Of UnitsByDay]",
"UnitsByDayLine0")) Then
Cancel = True
Else
intCountLine0 = DLookup("[Count Of UnitsByDay]",
"UnitsByDayLine0")
End If
If IsNull(intCountLine2 = DLookup("[Count Of UnitsByDay]",
"UnitsByDayLine2")) Then
Cancel = True
Else
intCountLine2 = DLookup("[Count Of UnitsByDay]",
"UnitsByDayLine2")
End If
If IsNull(intCountLine3 = DLookup("[Count Of UnitsByDay]",
"UnitsByDayLine3")) Then
Cancel = True
Else
intCountLine3 = DLookup("[Count Of UnitsByDay]",
"UnitsByDayLine3")
End If
Me.TotalLine0 = intCountLine0
Me.TotalLine2 = intCountLine2
Me.TotalLine3 = intCountLine3
Me.Refresh

If blnTimerOff = False Then
Application.Run MyTimer2
End If

End Function

Public Function MyTimer2()
Dim intPause As Integer
Dim dtStop As Date

lblTime.Caption = "Updated: " & Format(Now(), "hh:mm:ss")

intPause = 60
dtStop = DateAdd("s", intPause, Now())

Do Until (Now() >= dtStop) Or blnTimerOff
DoEvents
Loop

If blnTimerOff = False Then
Application.Run RefreshData
End If

End Function

Private Sub btnExit_Click()
blnTimerOff = True
DoCmd.Close
End Sub

Whenever I close or "Exit" the form, I get an error which I suspect is
because the timer is still in the loop. I also have tried using the
"blnTimerOff = True" statement in the OnClose and OnUnload events but that
doesn't fix the problem either.

Does anyone have any suggestions.

Thanks,
Mike
 
F

FatMan

Mike:
Not sure if this will help and by no way am I an access guru like others on
here but when I wanted to ensure mydatabase was closed by a certain time of
day I used the "On Timer" event and set my Timer Interval accordingly. I
believe the Timer Interval is measured in milli-seconds. So you should be
able to set it for what ever interval you need then just trigger your
refresh/requery procedure from the On Timer evernt. I think this might
eliminate your looping issue and closing the form. I know I have neever had
a problem with it.

Hope this helps,
FatMan
 
K

Klatuu

First, all forms have a built in timer event you can use, but for the most
part what you are doing is unnecessary.
Rather than using code to populate the controls where your are displaying
the information, you can just put the expression in the control source of the
control.

=Nz(DLookup("*","UnitsByDayLine0"), 0)

Additionally, your Cancel = True in your code is meaningless. I think you
are confusing what Cancel means. It is an argument in some event procedures
that allows you to cancel the event. Not all events can be canceled. For
example, a form open event can be canceled, but the load event cannot. The
before update event of forms and most controls can be canceled, but the after
update events can not. If you see an argument is the procedure's Arguments
as Cancel As Integer
That means the procedure can be canceled; otherwise, it can not.

Depending on how the form works, it may not be necessary to even run a timer
to get the values to refresh, but if you find it is, all you need to update
the calculated controls (those with an expression in the control source
property as described previously), is:
Me.ReCalc

--
Dave Hargis, Microsoft Access MVP


MikeS said:
I have a simple program that refreshes a form once every minute ( or what
ever increment I decide) It is basically two functions - one that requeries
the data and updates the form and one that does the timer.

Private Function RefreshData()
Dim intCountLine0 As Integer, intCountLine2 As Integer, intCountLine3 As
Integer

If IsNull(intCountLine0 = DLookup("[Count Of UnitsByDay]",
"UnitsByDayLine0")) Then
Cancel = True
Else
intCountLine0 = DLookup("[Count Of UnitsByDay]",
"UnitsByDayLine0")
End If
If IsNull(intCountLine2 = DLookup("[Count Of UnitsByDay]",
"UnitsByDayLine2")) Then
Cancel = True
Else
intCountLine2 = DLookup("[Count Of UnitsByDay]",
"UnitsByDayLine2")
End If
If IsNull(intCountLine3 = DLookup("[Count Of UnitsByDay]",
"UnitsByDayLine3")) Then
Cancel = True
Else
intCountLine3 = DLookup("[Count Of UnitsByDay]",
"UnitsByDayLine3")
End If
Me.TotalLine0 = intCountLine0
Me.TotalLine2 = intCountLine2
Me.TotalLine3 = intCountLine3
Me.Refresh

If blnTimerOff = False Then
Application.Run MyTimer2
End If

End Function

Public Function MyTimer2()
Dim intPause As Integer
Dim dtStop As Date

lblTime.Caption = "Updated: " & Format(Now(), "hh:mm:ss")

intPause = 60
dtStop = DateAdd("s", intPause, Now())

Do Until (Now() >= dtStop) Or blnTimerOff
DoEvents
Loop

If blnTimerOff = False Then
Application.Run RefreshData
End If

End Function

Private Sub btnExit_Click()
blnTimerOff = True
DoCmd.Close
End Sub

Whenever I close or "Exit" the form, I get an error which I suspect is
because the timer is still in the loop. I also have tried using the
"blnTimerOff = True" statement in the OnClose and OnUnload events but that
doesn't fix the problem either.

Does anyone have any suggestions.

Thanks,
Mike
 

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