have "2 timers" in one form, possible?

A

ali

I have a form, but i need different timers

1) for updating Elapsed time every second (1000ms)
2) Auto Update a query every 20 Minutes (20000ms)
3) User Log-off time session 1 hour (60000 ms)


How can that be done?

Dear expert, thanks a lot!
 
A

Allen Browne

Create a form-level variable (i.e. one declared in the General Declarations
section of the form's module), e.g.:
Private mdtFormOpen As Date

Assign the time the form was opened to it:
Private Sub Form_Open(Cancel As Integer)
mdtFormOpen = Now()
End Sub

In the form's timer event, test the number of seconds since the form opened.
If it's a multiple of 20 * 60, fire the code for your #2, e.g.:
If DateDiff("s", mdtFormOpen, Now()) Mod 1200 = 0 Then
MsgBox "20 minute warning."
End IF

This assumes the form's Timer Interval is 1000, so it fires each second.

Same kind of thing for the hourly warning, i.e. Mod 3600.
 
K

Klatuu

Question, Allen.
Why would Static varialbes in the Timer event not do just as well as Form
level variables?
IMHO, keeping variable scope as local as possible is good.
 
A

Allen Browne

A static variable in the Timer event might be better.

You can't intialize it in Form_Open that way, but so it would need some If
logic in Form_Timer to test for initialization.

(The initialization might also need to correct for the 1 second delay before
the timer event fires for the first time.)
 
M

Marshall Barton

ali said:
I have a form, but i need different timers

1) for updating Elapsed time every second (1000ms)
2) Auto Update a query every 20 Minutes (20000ms)
3) User Log-off time session 1 hour (60000 ms)


A different way that does not rely on an exact match of time
values could be:

Sub Form_Timer(
Static lngUpdate As Long
Static lngLogOff As Long

lngUpdate = lngUpdate + 1
lngLogOff = lngLogOff + 1

'One second processing
. . .

If lngLogOff >= 1200 Then
lngLogOff = 0
'Twenty minute processing
. . .
End If

If lngLogOff >= 3600 Then
lngLogOff = 0
'One hour processing
. . .
End If

End Sub
 

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