473,411 Members | 2,019 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,411 software developers and data experts.

How to Close a Form after a Certain Time of Inactivity

AbaAbdillah
How to close a from after a certain time pass inactive. After the user stop working with the database the tmer count down and after a certain time either close the form and lock it or close the whole application. using VBA coding can anyone help? thanks
Dec 5 '12 #1

✓ answered by ADezii

I honestly feel that an Attachment is a better Option here, but I will bow to my wiser and 'older' friend NeoPa. Here goes, the Timer Interval for the Form is set to 1000:
Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_Timer()
  2. ' IDLEMINUTES determines how much idle time to wait for before running the IdleTimeDetected subroutine.
  3. Const IDLEMINUTES = 5
  4.  
  5. Static PrevControlName As String
  6. Static PrevFormName As String
  7. Static ExpiredTime
  8.  
  9. Dim ActiveFormName As String
  10. Dim ActiveControlName As String
  11. Dim ExpiredMinutes
  12. Dim intNumOfMins As Long
  13.  
  14. On Error Resume Next
  15.  
  16. 'Get the Active Form and Control Name.
  17. ActiveFormName = Screen.ActiveForm.Name
  18. If Err Then
  19.   ActiveFormName = "No Active Form"
  20.     Err = 0
  21. End If
  22.  
  23. ActiveControlName = Screen.ActiveControl.Name
  24. If Err Then
  25.   ActiveControlName = "No Active Control"
  26.     Err = 0
  27. End If
  28.  
  29. ' Record the current active names and reset ExpiredTime if:
  30. '    1. They have not been recorded yet (code is running
  31. '       for the first time).
  32. '    2. The previous names are different than the current ones (the user has done something different during the timer interval).
  33.  
  34. If (PrevControlName = "") Or (PrevFormName = "") _
  35.                           Or (ActiveFormName <> PrevFormName) _
  36.                           Or (ActiveControlName <> PrevControlName) Then
  37.                             PrevControlName = ActiveControlName
  38.                             PrevFormName = ActiveFormName
  39.                               ExpiredTime = 0
  40. Else
  41.   ' ...otherwise the user was idle during the time interval, so increment the total Expired Time.
  42.   ExpiredTime = ExpiredTime + Me.TimerInterval
  43. End If
  44.  
  45. ' Does the total Expired Time exceed the IDLEMINUTES?
  46. ExpiredMinutes = (ExpiredTime / 1000) / 60
  47.  
  48. If ExpiredMinutes >= IDLEMINUTES Then
  49.   ' ...if so, then reset the Expired Time to zero...
  50. ExpiredTime = 0
  51.   ' ...and call the IdleTimeDetected subroutine.
  52. IdleTimeDetected ExpiredMinutes
  53. End If
  54.  
  55. 'For Display purposes only
  56. If ExpiredTime < 60000 Then                    '< 1 Minute
  57.   Me![txtExpiredTime] = "Idle Time Period: " & (ExpiredTime / 1000) & " Second(s)"
  58. ElseIf ExpiredTime Mod 60000 = 0 Then          'Even Minute
  59.   Me![txtExpiredTime] = "Idle Time Period: " & Int(ExpiredTime / 60000) & " Minute(s)"
  60. ElseIf ExpiredTime > 86400000 Then             '> 24 Hours
  61.   Me![txtExpiredTime] = "Idle Time Period: Sorry, didn't program for that!"
  62. Else                                           '> 1 Minute
  63.   intNumOfMins = Int(ExpiredTime / 60000)
  64.     Me![txtExpiredTime] = "Idle Time Period: " & intNumOfMins & " Minute(s) and " & _
  65.                           (ExpiredTime - (intNumOfMins * 60000)) / 1000 & " Second(s)"
  66. End If
  67. End Sub
Expand|Select|Wrap|Line Numbers
  1. Sub IdleTimeDetected(ExpiredMinutes)
  2.   DoCmd.Close acForm, "frmTest", acSaveNo
  3.     DoCmd.Quit
  4. End Sub
NeoPa:
Having now read your code again (I believe you posted this once before and I remember being quite impressed with the logic of it), I remember now that it works based on checking the value of two properties within the Screen object (of the Application) called ActiveForm and ActiveControl. If these are checked every second or so, and they both remain constant during the whole timeout period, this indicates that the application has been inactive throughout that period and can safely be closed down. By default, closing a form will save away any unsaved changes automatically, so the acSaveNo parameter is passed to your IdleTimeDetected() procedure to avoid this. Also, care must be taken to handle the real possibility of a reference to Screen.ActiveForm or Screen.ActiveControl returning Nothing, which is always possible if nothing is currently active.

10 12896
NeoPa
32,556 Expert Mod 16PB
Forms have a timer event. If you were to set a counter to your maximum value every time anything else happened on the form then allow the Timer event procedure to decrement this counter every time it runs then if it ever gets to 0 it could close itself down.
Dec 6 '12 #2
ADezii
8,834 Expert 8TB
Checking for a Form's Idle Time/Inactivity involves slightly complex Code in the Timer() Event of the Form as well as some kind of mechanism to determine if any Controls on the Form have been accessed. I have some Sample Code that illustrates this point, so if you are interested, I'll simply upload the DB as an Attachment where you can see everything in action. Should you then have any questions, we would be glad to answer them for you. Just let me know if you are interested.
Dec 6 '12 #3
NeoPa
32,556 Expert Mod 16PB
I'd be pretty interested to see that ADezii, but much more so in the thread than as an attachment. As a general rule of thumb, almost no-one benefits from attachments as they are only ever downloaded if the need is great and the interest high.

I was hoping you'd get involved in this thread as I know your experience in this area is impressive. It would be great if we could all benefit from it :-)
Dec 6 '12 #4
ADezii
8,834 Expert 8TB
I honestly feel that an Attachment is a better Option here, but I will bow to my wiser and 'older' friend NeoPa. Here goes, the Timer Interval for the Form is set to 1000:
Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_Timer()
  2. ' IDLEMINUTES determines how much idle time to wait for before running the IdleTimeDetected subroutine.
  3. Const IDLEMINUTES = 5
  4.  
  5. Static PrevControlName As String
  6. Static PrevFormName As String
  7. Static ExpiredTime
  8.  
  9. Dim ActiveFormName As String
  10. Dim ActiveControlName As String
  11. Dim ExpiredMinutes
  12. Dim intNumOfMins As Long
  13.  
  14. On Error Resume Next
  15.  
  16. 'Get the Active Form and Control Name.
  17. ActiveFormName = Screen.ActiveForm.Name
  18. If Err Then
  19.   ActiveFormName = "No Active Form"
  20.     Err = 0
  21. End If
  22.  
  23. ActiveControlName = Screen.ActiveControl.Name
  24. If Err Then
  25.   ActiveControlName = "No Active Control"
  26.     Err = 0
  27. End If
  28.  
  29. ' Record the current active names and reset ExpiredTime if:
  30. '    1. They have not been recorded yet (code is running
  31. '       for the first time).
  32. '    2. The previous names are different than the current ones (the user has done something different during the timer interval).
  33.  
  34. If (PrevControlName = "") Or (PrevFormName = "") _
  35.                           Or (ActiveFormName <> PrevFormName) _
  36.                           Or (ActiveControlName <> PrevControlName) Then
  37.                             PrevControlName = ActiveControlName
  38.                             PrevFormName = ActiveFormName
  39.                               ExpiredTime = 0
  40. Else
  41.   ' ...otherwise the user was idle during the time interval, so increment the total Expired Time.
  42.   ExpiredTime = ExpiredTime + Me.TimerInterval
  43. End If
  44.  
  45. ' Does the total Expired Time exceed the IDLEMINUTES?
  46. ExpiredMinutes = (ExpiredTime / 1000) / 60
  47.  
  48. If ExpiredMinutes >= IDLEMINUTES Then
  49.   ' ...if so, then reset the Expired Time to zero...
  50. ExpiredTime = 0
  51.   ' ...and call the IdleTimeDetected subroutine.
  52. IdleTimeDetected ExpiredMinutes
  53. End If
  54.  
  55. 'For Display purposes only
  56. If ExpiredTime < 60000 Then                    '< 1 Minute
  57.   Me![txtExpiredTime] = "Idle Time Period: " & (ExpiredTime / 1000) & " Second(s)"
  58. ElseIf ExpiredTime Mod 60000 = 0 Then          'Even Minute
  59.   Me![txtExpiredTime] = "Idle Time Period: " & Int(ExpiredTime / 60000) & " Minute(s)"
  60. ElseIf ExpiredTime > 86400000 Then             '> 24 Hours
  61.   Me![txtExpiredTime] = "Idle Time Period: Sorry, didn't program for that!"
  62. Else                                           '> 1 Minute
  63.   intNumOfMins = Int(ExpiredTime / 60000)
  64.     Me![txtExpiredTime] = "Idle Time Period: " & intNumOfMins & " Minute(s) and " & _
  65.                           (ExpiredTime - (intNumOfMins * 60000)) / 1000 & " Second(s)"
  66. End If
  67. End Sub
Expand|Select|Wrap|Line Numbers
  1. Sub IdleTimeDetected(ExpiredMinutes)
  2.   DoCmd.Close acForm, "frmTest", acSaveNo
  3.     DoCmd.Quit
  4. End Sub
NeoPa:
Having now read your code again (I believe you posted this once before and I remember being quite impressed with the logic of it), I remember now that it works based on checking the value of two properties within the Screen object (of the Application) called ActiveForm and ActiveControl. If these are checked every second or so, and they both remain constant during the whole timeout period, this indicates that the application has been inactive throughout that period and can safely be closed down. By default, closing a form will save away any unsaved changes automatically, so the acSaveNo parameter is passed to your IdleTimeDetected() procedure to avoid this. Also, care must be taken to handle the real possibility of a reference to Screen.ActiveForm or Screen.ActiveControl returning Nothing, which is always possible if nothing is currently active.
Dec 6 '12 #5
NeoPa
32,556 Expert Mod 16PB
ADezii:
but I will bow to my wiser and 'older' friend NeoPa.
You wish :-D Even I'm not that old!

Please understand my friend, there is nothing wrong with posting attachments per se, simply that they are no explanation and they're not much use for disseminating information. They're great to make available for people to go off and play with and develop their own understanding of certain matters, but for passing across ideas they come a very poor second to posting explanations that are clearly visible and can be read or scanned through while browsing forum threads. I may not (or even possibly may if I'm being honest) be that old, but I've been working with forums for quite a few years now (as I know you have too of course) and I have a fair idea of all the different ways people use them and the sorts of things that work and when and how and why.

Having now read your code again (I believe you posted this once before and I remember being quite impressed with the logic of it), I remember now that it works based on checking the value of two properties within the Screen object (of the Application) called ActiveForm and ActiveControl. If these are checked every second or so, and they both remain constant during the whole timeout period, this indicates that the application has been inactive throughout that period and can safely be closed down. By default, closing a form will save away any unsaved changes automatically, so the acSaveNo parameter is passed to your IdleTimeDetected() procedure to avoid this. Also, care must be taken to handle the real possibility of a reference to Screen.ActiveForm or Screen.ActiveControl returning Nothing, which is always possible if nothing is currently active.
Dec 6 '12 #6
ADezii
8,834 Expert 8TB
Just wanted to say that the Code is not 'mine' per say. I may have made minor to changes to it, but in honesty, I cannot remember where I obtained it from.
Dec 6 '12 #7
NeoPa
32,556 Expert Mod 16PB
Nevertheless it is well worth disseminating I think. It's the sort of thing people will often search for. I suspect having such code here will prove helpful to many.
Dec 6 '12 #8
Thank you for your help! and do you mind if you tell me what value to give to the timer interval for the form. Do I need to give time interval value for each form. I believe that this code is going to be behind the main menu form on timer event right?
thank you again
Dec 6 '12 #9
ADezii
8,834 Expert 8TB
  1. Whatever Value you give to the Form's Timer Interval Property, remember that it is in Milliseconds, namely:
    Expand|Select|Wrap|Line Numbers
    1. Timer Interval Value    Actual (secs.)
    2.  1000                          1
    3.  5000                          5
    4. 10000                         10
    5.  
  2. The bulk of the Code would be contained within the Form's Timer() Event.
Dec 6 '12 #10
NeoPa
32,556 Expert Mod 16PB
Inactivity Detection in Access may be helpful on this point. It's a bit more robust, but works to the same fundamental principles.
Sep 29 '13 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: CH | last post by:
Hi, I recently encountered a problem while trying to create a report grouped by certain time frames. However, Access only allows grouping in Minutes or Hours. For example, the first time frame is...
2
by: Vivek Sharma | last post by:
How can I close a form in C#? Here is the code that I am using but closes everything? private void frmSplash_Click(object sender, EventArgs e) { this.Close(); Form frmLogin = new...
4
by: Mindy | last post by:
I have two questions here: (1) what is the difference of close form and close table? (2) How "Prompt" works I used close form macro in my database. I hope when a user close the form, he/she will...
4
by: eladla | last post by:
I have a windows service that I want to carry out an action on a certain day of the week at a certain time and if the time is missed for some reason, I want it to be carried out as soon as the...
6
by: Robert Dufour | last post by:
On my form if the user clicks the upper right hand corner to close the form I want to trap that event and do a check to see if closing is allowed, if not, I want to stop the form closing action. ...
3
by: kev | last post by:
Hi folks, I have a form for registration (frmRegistration) whereby i have two buttons. One is Save which saves record using the OnClick property. I used wizard to create the save button. The...
22
AccessIdiot
by: AccessIdiot | last post by:
Hello all, I have a form (frm_Entrainment) with a button that opens a 2nd form (frm_Specimen_Entrainment). The 2nd form shares an ID field with the first form (Entrainment_ID - its like opening...
2
by: CSmith | last post by:
I have a database that holds all the tables for multiple other databases that the tables are linked to. I need to access the database everyday at a certain time and can't seem to get the users to...
2
by: Hulas | last post by:
Guys, I have two questions. (a) How do I add two fields of the same table to a single combo box. For example if I have two fields ID1 and ID2 in a table called Identification, than how should I...
0
by: ncsthbell | last post by:
I have an MS2007 access database. On one of the forms that is used to edit/change data, there is a button 'Return to main menu'. Once the user has finished making the changes on the form and clicks...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.