Close the Access database if Idle.

Anonymous
2011-08-09T21:48:32+00:00

I am looking for a way to close access if the user is idle for 5 minutes.  I found this link to code that I cannot get to work.  I am not a advanced user in VBA and any help would be greatly appreciated.

http://bb.dzsoundnirvana.com/viewtopic.php?f=4&t=60

If you want this to run when the database file is open then create a new macro with the name of "AutoExec". Set the action to "OpenForm" and the action arguments are:

Form Name:DetectIdleTime
View:Form
Filter Name: Leave blank
Where Condition: Leave blank
Data Mode:Edit
Window Mode:Hidden

Create a new module in modules with the name of "TimeForm" and copy and paste the following code.

Option Compare Database

Sub IdleTimeDetected(ExpiredMinutes)

Dim Msg As String

'Msg = "No user activity detected in the last "

'Msg = Msg & ExpiredMinutes & " minute(s)!"

'MsgBox Msg, 48

Application.Quit acSaveYes

End Sub

Create a blank form in design view. You do not have to set any data sources etc. Copy and paste the following code and save the form as "DetectIdleTime". Set the time in minutes where it says "Const IDLEMINUTES = 5".

Option Compare Database

Private Sub Form_Timer()

' 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

End If

End Sub

Microsoft 365 and Office | Access | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Anonymous
    2014-06-16T20:46:28+00:00

    I have used this code for a form, much simpler and does exactly what I need.

    Closes the database after a set amount of idle time.

    Just set the timer to 1000 and how long in seconds you want it to remain open if idle, then it closes after the specified time.

    Code:

    'General Declarations

    Dim lgCounter As Long

    __________________________________

    '(In the detail events section)

    Private Sub Detail_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)

    lgCounter = 0

    End Sub

    _____________________________________

    '(In form events section)

    Private Sub Form_Timer()

    lgCounter = lgCounter + 1

    'Enter the amount of seconds before you want the database to close, and set the form timer to 1000.

    If lgCounter >= 60 Then

            Application.Quit acQuitSaveAll

    End If

    End Sub

    Or copy and paste the code below into your form  :-)

    Dim lgCounter As Long

    Private Sub Detail_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)

    lgCounter = 0

    End Sub

    Private Sub Form_Timer()

    lgCounter = lgCounter + 1

    'Enter the amount of seconds allowed to be idle before the database closes here and set the form timer to 1000.

    If lgCounter >= 60 Then

       Application.Quit acQuitSaveAll

    End If

    End Sub

    Was this answer helpful?

    10+ people found this answer helpful.
    0 comments No comments
  2. Anonymous
    2011-08-11T17:45:58+00:00

    If not the above code, is there anything else we could use to close access if a use is ible for too long?

    Was this answer helpful?

    0 comments No comments