Idle Time Auto log off

Hi People

I have a code below on a blank unbound form which is open by a start up form as hidden , the purpose of the form is to log off users who have been inactive for 3 minutes in the application:

Private Sub Form_Load()

   ' start the count down in 3 minutes (60 x 3 = 180)
   Me.Tag = 180
   
   ' interval is every second
   Me.TimerInterval = 1000
   
End Sub

Private Sub Form_Timer()

   On Error Resume Next
   '**********************************
   ' Bail out when count down is zero
   '**********************************
   
   Me.Tag = Val(Me.Tag) - (Me.TimerInterval / 1000)
   
   Me.Caption = "Form will exit in " & Me.Tag & " seconds"
   
   If Val(Me.Tag) <= 0 Then
      DoCmd.Quit
   End If

End Sub

But what is happening  is that the code runs every 3 minutes whether one is in the data base or not active.

(1) How do I fix it so that it should only run when there is no activity after 3 minutes

Regards

Chris
LVL 3
Hankwembo Christopher,FCCA,FZICA,CIA,MAAT,B.A.ScDirectorAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dale FyeOwner, Developing Solutions LLCCommented:
First off, 3 minutes is much to small a time for this, in most cases.  Second, simply shutting down the application, without doing so elegantly can cause unexpected results.  First off, you must decide what you want to do with unsaved records, if you have any kind of validation code in the form_BeforeUpdate event of your forms, it will cause your AutoClose process to fail, so you must undo any changes in the currently active form.  I also recommend that you close all other forms before quitting your application, you would do this by looping through the Forms collection (I generally do this backwards, last opened, first closed).

1.  Start out by setting the timer interval to a smaller timerinterval  10000 (10 seconds)
2.  Then, every time that interval fires, you need to test to see whether anything has happened during the previous minute, something like the following.
Private Sub Form_Timer()

    Static frm as form
    Static frmCount as int
    Static rptCount as int
    Static Control as Control
    Static dtLastUpdated as date

    'Check to see whether the number of open forms or reports has changed
    if forms.Count <> frmCount then
        frmCount = forms.Count
        dtLastUpdated = Now()
        Exit Sub
    elseif reports.Count <> rptCount then
        rptCount = reports.Count
        dtLastUpdated = Now()
        Exit sub
    End If

    'Check to see if use is on same form
    'If not, save form and exit because there has been activity
    if (frm Is Nothing) then 
        set frm = Screen.ActiveForm
        dtLastUpdated = Now()
        Exit Sub
    Elseif frm.Name <> Screen.ActiveForm,Name Then
        set frm = Screen.ActiveForm
        dtLastUpdated = Now()
        Exit Sub
    End If

    'Now check to see if they are on the same control
    if (ctrl is nothing) Then
        set ctrl = frm.ActiveControl
        dtLastUpdated = Now()
        exit sub
    elseif ctrl.name <> frm.ActiveControl.name Then
        set ctrl = frm.ActiveControl
        dtLastUpdated = Now()
        Exit Sub
    End If

    'nothing has changed, check to see whether the elapsed time > some value
    if datediff("n", dtLastUpdated, Now) > 3 Then
        'Execute shutdown process here
    End If

End Sub

Open in new window

Note that I have left the shutdown process for you to implement at line 45
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.