Autokick When Idle Code (1 Viewer)

Tezcatlipoca

Registered User.
Local time
Today, 08:27
Joined
Mar 13, 2003
Messages
246
I'm trying to impliment an autokick when idle process to my database, but am running into a problem and am not too sure how to resolve it.

Ok, basically I have an autoexec macro on my database which opens the form
DetectIdleTime as hidden. This form has its Timer Interval set to 1000 and its On Timer set to:

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

   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

I also have a module - called Form - which has the code:

Code:
Option Compare Database

Sub IdleTimeDetected(ExpiredMinutes)
   Application.Quit acSaveQuitSaveNone
End Sub

Now this works absolutely perfectly in it's current state (i.e. closing after 1 minute of idleness), but whenever I try to change the time - by altering the Const IDLEMINUTES = 1 line - to that required (in this case 10 minutes), the database refuses to close after that period of idleness.

I'm sure I'm missing something mind-numbingly simple, but can't for the life of me see what it is...
 

Robert88

Robbie
Local time
Today, 17:27
Joined
Dec 18, 2004
Messages
335
Hi Tezcatlipoca,

What value is Me.TimerInterval as 1000 units represent one second?

Look forward to your reply.

Robert88
 

Tezcatlipoca

Registered User.
Local time
Today, 08:27
Joined
Mar 13, 2003
Messages
246
Well, I am - perhaps incorrectly - assuming it should match the idle closetime, so 50000 for 5 mins, 100000 for 10, and so on. the problem is that this doesn't appear to work. I set it to 100000, then set the code to 10 minutes, but the blasted thing refuses to close. However, if I set 50000 and 5 mins respectively, or 10000 and 1 min, it works.

Any ideas?
 

Robert88

Robbie
Local time
Today, 17:27
Joined
Dec 18, 2004
Messages
335
Hi Tezcatlipoca,

I just looked up timerinterval in VBA Help and it appears that the timer interval should run for 10 minutes as its maximum is 2,147,483,647 milliseconds, which is amongst the figures as indicated;

The TimerInterval property setting of the form specifies the interval, in milliseconds, between Timer events. The interval can be between 0 and 2,147,483,647 milliseconds. Setting the TimerInterval property to 0 prevents the Timer event from occurring.

2,147,483,647 milliseconds = 2,147,483,647 / (24 (hours to days) X 60 (minutes to hours) X 60 (seconds to minutes) X 1000 (millisec to secs)) = 24.855 days....

But it would appear from the following help;

To run Visual Basic code at intervals specified by the TimerInterval property, put the code in the form's Timer event procedure. For example, to requery records every 30 seconds, put the code to requery the records in the form's Timer event procedure, and then set the TimerInterval property to 30000.

That you are going to need 60 seconds perminute and since you have 10 minutes, 60 X 10 X 1000 = 600,000 as a timer interval for 10 minutes, or not?

5 mins = 5 X 60 X 1000 = 300,000 as an interval?

HTH

Robert88
 
Last edited:

Tezcatlipoca

Registered User.
Local time
Today, 08:27
Joined
Mar 13, 2003
Messages
246
Hmmm...interesting. I'll try playing about with that, but I know as a result of testing that an interval of 50000 does indeed close my database after 5 minutes of inactivity.

Thanks for your help, Robert88; I shall poke this a bit further to see if I can get it to run properly.
 

gemma-the-husky

Super Moderator
Staff member
Local time
Today, 08:27
Joined
Sep 12, 2006
Messages
15,710
i was intrigued and had a look. I put your code in an open form and ran it, changing the final command to close the form not the app - after 10 minutes the form closed - worked perfectly . i added a counter (checks dim static) as you did with your tick count

Could you put some fields on your hidden form to display the timer count and the last accessed form - put the updates in the else event - you need a repaint method or you wont see the change, as below

Else
' ...otherwise the user was idle during the time interval, so
' increment the total expired time.
ExpiredTime = ExpiredTime + Me.TimerInterval

checks = checks + 1 'refers to my new variable
txtChecked = checks 'text box on the hidden form
txtTicks = ExpiredTime 'text box on the hidden form

Me.Repaint
End If

'you could then unhide the hidden form to see whats happening, although i know this might mess your timimng around - as your timer refreshes every ten seconds, if you get the focus back to the right control within ten seconds, the hidden form won't notice the change!
 
Last edited:

kyoch

Registered User.
Local time
Today, 02:27
Joined
Mar 5, 2014
Messages
58
Hello,

First of all, thank you for posting this code. This is exactly what I was looking for!

There are two "challenges" I'm having trouble trying to overcome with this code.

First, if the users desktop is set to go to screen saver after 5 minutes and the detectidletime is set to 10 minutes, the database does not successfully close. How can I change this to where the database will still close after 10 minutes of inactivity even if the user is in screensaver mode?

Second, within my database I have a button which opens up another form as a pop up. When that form pops up, the database will close after one minute even if I am typing or clicking during that period. I have to go back to the original form, click something, then return to my pop form to keep it from auto closing.

Please help! I don't think I explained this in the best way but I'd appreciate any help.
 

MarvinM

Registered User.
Local time
Today, 00:27
Joined
Nov 26, 2013
Messages
64
Kyoch,

I found another challenge similar to the first one you mentioned. When the computer goes to sleep/hibernates, this seems to suspend time too.

As for your second challenge, I think the issue can be narrowed down to this code line:
Code:
If (PrevControlName = "") Or (PrevFormName = "") _
Or (ActiveFormName <> PrevFormName) _
Or (ActiveControlName <> PrevControlName) Then
Please examine it to make sure there are no typos and the variables are set correctly.

Good luck!
_________________
Regards,
Marvin M :cool:
Windows 7 Professional, MS Access 2007/2010
Windows 8 Professional, MS Access 2013
-------------------------------------------------------------------------------------------------------------------
If my post has helped you, please click the scales or click the 'Thumbs up'. Thanks!
-------------------------------------------------------------------------------------------------------------------
 

MarvinM

Registered User.
Local time
Today, 00:27
Joined
Nov 26, 2013
Messages
64
Robbie is right, but I don't like making those calculations every time I want to change the time. I will set the Timer Interval to 1000 almost every time (60000 on certain projects). Then in the code, I use a constant to specify the number of minutes. A conversion factor of 60 is built into the code.

I have discovered a couple of shortcuts that I prefer. Since I know that I am firing off this timer event every 1 second, I don't use:
Code:
      ' increment the total expired time.
      ExpiredTime = ExpiredTime + Me.TimerInterval
   End If
 
   ' Does the total expired time exceed the IDLEMINUTES?
   ExpiredMinutes = (ExpiredTime / 1000) / 60

I prefer this instead:
Code:
         ElapsedTime = ElapsedTime + 1
 
and
         ElapsedMinutes = ElapsedTime / 60
I know there are reasons for using Me.TimerInterval, but I don't believe they apply here. Since I count the seconds, I can also show a countdown timer when the IDLEMINUTES have expired.

For example, I set the idle timer to 15 minutes. When that 15 minutes is up, a warning message pops up and gives the user a 5 minute grace period to choose whether to continue or quit, or else Access shuts down automatically. Do you want the code for that?

_________________
Regards,
Marvin M :cool:
Windows 7 Professional, MS Access 2007/2010
Windows 8 Professional, MS Access 2013
-------------------------------------------------------------------------------------------------------------------
If my post has helped you, please click the scales or click the 'Thumbs up'. Thanks!
-------------------------------------------------------------------------------------------------------------------
 

Partyandbs

New member
Local time
Today, 00:27
Joined
May 20, 2014
Messages
1
Robbie is right, but I don't like making those calculations every time I want to change the time. I will set the Timer Interval to 1000 almost every time (60000 on certain projects). Then in the code, I use a constant to specify the number of minutes. A conversion factor of 60 is built into the code.

I have discovered a couple of shortcuts that I prefer. Since I know that I am firing off this timer event every 1 second, I don't use:
Code:
      ' increment the total expired time.
      ExpiredTime = ExpiredTime + Me.TimerInterval
   End If
 
   ' Does the total expired time exceed the IDLEMINUTES?
   ExpiredMinutes = (ExpiredTime / 1000) / 60

I prefer this instead:
Code:
         ElapsedTime = ElapsedTime + 1
 
and
         ElapsedMinutes = ElapsedTime / 60
I know there are reasons for using Me.TimerInterval, but I don't believe they apply here. Since I count the seconds, I can also show a countdown timer when the IDLEMINUTES have expired.

For example, I set the idle timer to 15 minutes. When that 15 minutes is up, a warning message pops up and gives the user a 5 minute grace period to choose whether to continue or quit, or else Access shuts down automatically. Do you want the code for that?

_________________
Regards,
Marvin M :cool:
Windows 7 Professional, MS Access 2007/2010
Windows 8 Professional, MS Access 2013
-------------------------------------------------------------------------------------------------------------------
If my post has helped you, please click the scales or click the 'Thumbs up'. Thanks!
-------------------------------------------------------------------------------------------------------------------

Could you send me that code please. Law665 @yahoo
 

Samantha

still learning...
Local time
Today, 03:27
Joined
Jul 12, 2012
Messages
180
MarvinM,

Could you post your code to the thread? Pretty please!

Thanks, Samantha
 

kyoch

Registered User.
Local time
Today, 02:27
Joined
Mar 5, 2014
Messages
58
I frequently get a pop up that says "2475" and can't figure out how to make this stop. Any suggestions?
 

Samantha

still learning...
Local time
Today, 03:27
Joined
Jul 12, 2012
Messages
180
Kyoch,

I couldn't get this code to work either, I am in no way an expert at this. I found an alternate way to accomplish what I wanted. I just wanted to kick out everyone and run backups/make changes to the BE. Upon checking the box in my form the other users receive a message telling them that the db is closing for maintenance. I attached images of the forms to help explain.

Code:
Option Compare Database

Private Sub dbExit_AfterUpdate()
    DoCmd.RunCommand acCmdSaveRecord
    
End Sub

Private Sub Form_Load()
    Me.dbExit = False
    DoCmd.RunCommand acCmdSaveRecord
End Sub

Private Sub Form_Timer()
     ' prepare to close database
     If bolFirstTime = True Then
      ' do nothing
     Else
      
        Call fGetOut
     End If
End Sub

This is the code for the function
Code:
Function fGetOut() As Integer
Dim stDocname As String
On Error GoTo Err_fGGO

 varCloseDB = DLookup("dbExit", "tblExit")
 dbExitTime = DLookup("dbExitTime", "tblExit")
 bolFirstTime = True
     
     If varCloseDB = -1 Then
          '   'Application.Quit
          'MsgBox "DatabaseClose", vbOKOnly
          stDocname = "frmCountDown"
        If Not CurrentProject.AllForms(stDocname).IsLoaded Then
                DoCmd.OpenForm stDocname, acNormal
                dbExitTime2 = dbExitTime * 60
        End If
    Else
        ' close countdown form if no longer required
        If CurrentProject.AllForms(stDocname).IsLoaded Then
            DoCmd.Close acForm, stDocname
        End If
             
    End If

    ' set initial time for countdown
 
Exit_fGGO:
' fGetOut = RetVal
 Exit Function
Err_fGGO:
 'Note lack of message box on error
 Resume Next
End Function

Best of Luck!
Samantha
 

Attachments

  • Capture Close Form.GIF
    Capture Close Form.GIF
    41.4 KB · Views: 253
  • frmCountDown.GIF
    frmCountDown.GIF
    51.3 KB · Views: 282

Anna.f

New member
Local time
Today, 09:27
Joined
May 15, 2020
Messages
1
I'm trying to impliment an autokick when idle process to my database, but am running into a problem and am not too sure how to resolve it.

Ok, basically I have an autoexec macro on my database which opens the form
DetectIdleTime as hidden. This form has its Timer Interval set to 1000 and its On Timer set to:

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

   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

I also have a module - called Form - which has the code:

Code:
Option Compare Database

Sub IdleTimeDetected(ExpiredMinutes)
   Application.Quit acSaveQuitSaveNone
End Sub

Now this works absolutely perfectly in it's current state (i.e. closing after 1 minute of idleness), but whenever I try to change the time - by altering the Const IDLEMINUTES = 1 line - to that required (in this case 10 minutes), the database refuses to close after that period of idleness.

I'm sure I'm missing something mind-numbingly simple, but can't for the life of me see what it is...


Hi,

I am using this code but it's not working and after 2-3 minutes I get the error below. Does anyone know what I've done wrong? and what do I need to do to make it work? Thanks.
1589555732498.png
 

Micron

AWF VIP
Local time
Today, 03:27
Joined
Oct 20, 2018
Messages
3,478
The simplest thing you're missing is that you're not requiring variable declaration. I can tell by seeing
Option Compare Database
without seeing
Option Explicit
Turn on the setting (vb editor - options - require variable declaration) and compile you code and fix the errors. Then try again.
If still the same error, put a break on your code and step through to locate the offending line, or comment out the On Error Resume Next
You have no error handler, so that is a bad idea any way.

Do you realize you are running this code once every second?

EDIT - you will have to type in Option Explicit at the top of every module if you want it there. It will not automatically be added to existing code.
EDIT 2 - the forum took me to the last post in this thread, so I read it and replied without noticing that the poster was not the original poster. So Anna.f, what you did here was hijack the thread by adding your own issue. Best to start your own thread IMO.
 

Oupike1723

New member
Local time
Today, 02:27
Joined
Mar 24, 2022
Messages
1
Kyoch,

I found another challenge similar to the first one you mentioned. When the computer goes to sleep/hibernates, this seems to suspend time too.

As for your second challenge, I think the issue can be narrowed down to this code line:
Code:
If (PrevControlName = "") Or (PrevFormName = "") _
Or (ActiveFormName <> PrevFormName) _
Or (ActiveControlName <> PrevControlName) Then
Please examine it to make sure there are no typos and the variables are set correctly.

Good luck!
_________________
Regards,
Marvin M :cool:
Windows 7 Professional, MS Access 2007/2010
Windows 8 Professional, MS Access 2013
-------------------------------------------------------------------------------------------------------------------
If my post has helped you, please click the scales or click the 'Thumbs up'. Thanks!
-------------------------------------------------------------------------------------------------------------------
Can you send me this code Marvin , please!
Robbie is right, but I don't like making those calculations every time I want to change the time. I will set the Timer Interval to 1000 almost every time (60000 on certain projects). Then in the code, I use a constant to specify the number of minutes. A conversion factor of 60 is built into the code.

I have discovered a couple of shortcuts that I prefer. Since I know that I am firing off this timer event every 1 second, I don't use:
Code:
      ' increment the total expired time.
      ExpiredTime = ExpiredTime + Me.TimerInterval
   End If

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

I prefer this instead:
Code:
         ElapsedTime = ElapsedTime + 1

and
         ElapsedMinutes = ElapsedTime / 60
I know there are reasons for using Me.TimerInterval, but I don't believe they apply here. Since I count the seconds, I can also show a countdown timer when the IDLEMINUTES have expired.

For example, I set the idle timer to 15 minutes. When that 15 minutes is up, a warning message pops up and gives the user a 5 minute grace period to choose whether to continue or quit, or else Access shuts down automatically. Do you want the code for that?

_________________
Regards,
Marvin M :cool:
Windows 7 Professional, MS Access 2007/2010
Windows 8 Professional, MS Access 2013
-------------------------------------------------------------------------------------------------------------------
If my post has helped you, please click the scales or click the 'Thumbs up'. Thanks!
-------------------------------------------------------------------------------------------------------------------
Can you send me this the code for that Marvin, please .. new member here but long time viewer of this board
 

Users who are viewing this thread

Top Bottom