The first post is the one I use. The following is taken from my project where it works...
Create a form called frmDetectIdleTime
You can leave it completely blank as you will never see it.
Set the TimerInterval property of the form to 1000
In the OnTimer event put the code at the bottom of this post. You also need to change the CONST IDLEMINUTES = 5 line to the number of minutes you want.
Then, on your "main" form (the first form you have open or the primary form you use), put the following in the OnOpen event: DoCmd.OpenForm "frmDetectIdleTime", , , , , acHidden
The frmDetectIdleTime will be opened invisibily and run in the background. Every second it will see if there is activity. If so, it resets its timer. If there is no activity, it increments its timer. If the timer has reached the maximum, it closes the database.
That's about it. Good luck and let me know if you have any issues.
Code:
' IDLEMINUTES determines how much idle time to wait for before
' running the IdleTimeDetected subroutine.
Const IDLEMINUTES = 5
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
DoCmd.Quit
End If
=======================================
People think it must be fun to be a super genius, but they don't realize how hard it is to put up with all the idiots in the world. (Calvin from Calvin And Hobbs)
Robert L. Johnson III
CCNA, CCDA, MCSA, CNA, Net+, A+, CHDP
VB/Access Programmer