Timer Form

D

Don

I have a timer form that I need to change a parameter. This form is only on
the front-end part of the application. The forms' purpose is to close the
front end of the database if the user forgot to close it before going home.
Closing the front end or making sure everyone in not accessing records is
required to be able to update, append, delete and recreate tables on the
backend of the database. When the front ends are open the process on the back
end errors out and the update is not done.
The problem with the current timer is when the user does close the front end
not allowing the it's code to run at the time selected. What happens is when
the user opens the database the application closes. This works for any user
who understands what is going on but those who do not, think something is
wrong with the application (besides my abilities). The way the code works now
is if the date is less than today’s date and if it is after 5:10 AM then
process the code that closes the database.
What I would like is to look at the same criteria but if the time is after
say 6:15 AM then only update the date in the timer table and do not close the
database. The following is the current code:
Private Sub Form_Timer()

On Error GoTo Err_Form_Timer


If Time() > #5:10:00 AM# Then

If DLookup("LastTimerDate", "tblTimerDate") < DATE Then

CurrentDb.Execute _
"UPDATE tblTimerDate SET LastTimerDate = " & _
Format(DATE, "\#mm/dd/yyyy\#")
' Closes database and compacts and repairs database allowing
' Server to update and create needed daily reports.
DoCmd.Quit

End If
Form_Timer_Exit:

Exit Sub

Err_Form_Timer:
MsgBox Error$
Resume Form_Timer_Exit
MsgBox Err.Number & Err.Description

Resume Form_Timer_Exit
End If

End Sub

This is my attempt to modify the code to 1) perform the above if between
5:10 and 6:15 AM if not, then 2) only change the date without closing the
front end after 6:15. With my additions, nothing happens. What am I missing?
My changes in bold.

Private Sub Form_Timer()

On Error GoTo Err_Form_Timer


If Time() > #5:10:00 AM# And < #6:15 AM# Then

If DLookup("LastTimerDate", "tblTimerDate") < DATE Then

CurrentDb.Execute _
"UPDATE tblTimerDate SET LastTimerDate = " & _
Format(DATE, "\#mm/dd/yyyy\#")
' Closes database and compacts and repairs database allowing
' Server to update and create needed daily reports.
DoCmd.Quit

Else
If DLookup("LastTimerDate", "tblTimerDate") < DATE Then

CurrentDb.Execute _
"UPDATE tblTimerDate SET LastTimerDate = " & _
Format(DATE, "\#mm/dd/yyyy\#")

End If
End If
Form_Timer_Exit:

Exit Sub

Err_Form_Timer:
MsgBox Error$
Resume Form_Timer_Exit
MsgBox Err.Number & Err.Description

Resume Form_Timer_Exit
End If

End Sub

Thanks,
Dennis
 
A

Allan Murphy

Dennis

Try this code,

Using the autoexec macro, the macro, on startup, opens a hidden blank form
with the code below attached to the form. The code is running in the
background and closes the database if there is no activity for a
predetermined period, in this case 10 minutes.

Sub Form_Timer()
' IDLEMINUTES determines how much idle time to wait for before
' running the IdleTimeDetected subroutine.
Const IDLEMINUTES = 10

Static PrevControlName As String
Static PrevFormName As String
Static ExpiredTime

Dim ActiveFormName As String

Dim ActiveControlName As String
Dim ExpiredMinutes

On Error Resume Next

' Get the active form and control name.

ActiveFormName = Screen.ActiveForm.Name
If Err Then
ActiveFormName = "No Active Form"
Err = 0
End If

ActiveControlName = Screen.ActiveControl.Name
If Err Then
ActiveControlName = "No Active Control"
Err = 0
End If

' Record the current active names and reset ExpiredTime if:
' 1. They have not been recorded yet (code is running
' for the first time).
' 2. The previous names are different than the current ones
' (the user has done something different during the timer
' interval).
If (PrevControlName = "") Or (PrevFormName = "") _
Or (ActiveFormName <> PrevFormName) _
Or (ActiveControlName <> PrevControlName) Then

PrevControlName = ActiveControlName
PrevFormName = ActiveFormName
ExpiredTime = 0
Else
' ...otherwise the user was idle during the time interval, so
' increment the total expired time.
ExpiredTime = ExpiredTime + Me.TimerInterval
End If

' Does the total expired time exceed the IDLEMINUTES?
ExpiredMinutes = (ExpiredTime / 1000) / 60
If ExpiredMinutes >= IDLEMINUTES Then

' ...if so, then reset the expired time to zero...
ExpiredTime = 0
' ...and call the IdleTimeDetected subroutine.
IdleTimeDetected ExpiredMinutes
End If
End Sub

Sub IdleTimeDetected(ExpiredMinutes)

Application.Quit acQuitSaveAll

End Sub
 
D

Don

Allan,

Thanks,

Dennis


Allan Murphy said:
Dennis

Try this code,

Using the autoexec macro, the macro, on startup, opens a hidden blank form
with the code below attached to the form. The code is running in the
background and closes the database if there is no activity for a
predetermined period, in this case 10 minutes.

Sub Form_Timer()
' IDLEMINUTES determines how much idle time to wait for before
' running the IdleTimeDetected subroutine.
Const IDLEMINUTES = 10

Static PrevControlName As String
Static PrevFormName As String
Static ExpiredTime

Dim ActiveFormName As String

Dim ActiveControlName As String
Dim ExpiredMinutes

On Error Resume Next

' Get the active form and control name.

ActiveFormName = Screen.ActiveForm.Name
If Err Then
ActiveFormName = "No Active Form"
Err = 0
End If

ActiveControlName = Screen.ActiveControl.Name
If Err Then
ActiveControlName = "No Active Control"
Err = 0
End If

' Record the current active names and reset ExpiredTime if:
' 1. They have not been recorded yet (code is running
' for the first time).
' 2. The previous names are different than the current ones
' (the user has done something different during the timer
' interval).
If (PrevControlName = "") Or (PrevFormName = "") _
Or (ActiveFormName <> PrevFormName) _
Or (ActiveControlName <> PrevControlName) Then

PrevControlName = ActiveControlName
PrevFormName = ActiveFormName
ExpiredTime = 0
Else
' ...otherwise the user was idle during the time interval, so
' increment the total expired time.
ExpiredTime = ExpiredTime + Me.TimerInterval
End If

' Does the total expired time exceed the IDLEMINUTES?
ExpiredMinutes = (ExpiredTime / 1000) / 60
If ExpiredMinutes >= IDLEMINUTES Then

' ...if so, then reset the expired time to zero...
ExpiredTime = 0
' ...and call the IdleTimeDetected subroutine.
IdleTimeDetected ExpiredMinutes
End If
End Sub

Sub IdleTimeDetected(ExpiredMinutes)

Application.Quit acQuitSaveAll

End Sub
 
B

Barbara

I think your code is what I'm looking for. How would I change it to close
after 30 minutes of inactivity?

A collegue has this code in their form, which I think closes it after 30
minutes, but doesn't look at activity?

Private Sub Form_Activate()
Me![COUNT] = 0

End Sub

Private Sub Form_Deactivate()
Me![COUNT] = 0
End Sub

Private Sub Form_Timer()
Me![COUNT] = Me![COUNT] + 1
If Me![COUNT] > 30 Then DoCmd.Quit acSaveNo 'quit application after 30 minutes

End Sub


Thanks!
Barbara
 

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

Similar Threads


Top