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 2
Hankwembo Christopher,FCCA,FZICA,CIA,MAAT,B.A.ScDirectorAsked:
Who is Participating?
 
Dale FyeConnect With a Mentor Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.