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
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: -
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
-
Dim intNumOfMins As Long
-
-
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
-
-
'For Display purposes only
-
If ExpiredTime < 60000 Then '< 1 Minute
-
Me![txtExpiredTime] = "Idle Time Period: " & (ExpiredTime / 1000) & " Second(s)"
-
ElseIf ExpiredTime Mod 60000 = 0 Then 'Even Minute
-
Me![txtExpiredTime] = "Idle Time Period: " & Int(ExpiredTime / 60000) & " Minute(s)"
-
ElseIf ExpiredTime > 86400000 Then '> 24 Hours
-
Me![txtExpiredTime] = "Idle Time Period: Sorry, didn't program for that!"
-
Else '> 1 Minute
-
intNumOfMins = Int(ExpiredTime / 60000)
-
Me![txtExpiredTime] = "Idle Time Period: " & intNumOfMins & " Minute(s) and " & _
-
(ExpiredTime - (intNumOfMins * 60000)) / 1000 & " Second(s)"
-
End If
-
End Sub
- Sub IdleTimeDetected(ExpiredMinutes)
-
DoCmd.Close acForm, "frmTest", acSaveNo
-
DoCmd.Quit
-
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 12868 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.
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.
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 :-)
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: -
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
-
Dim intNumOfMins As Long
-
-
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
-
-
'For Display purposes only
-
If ExpiredTime < 60000 Then '< 1 Minute
-
Me![txtExpiredTime] = "Idle Time Period: " & (ExpiredTime / 1000) & " Second(s)"
-
ElseIf ExpiredTime Mod 60000 = 0 Then 'Even Minute
-
Me![txtExpiredTime] = "Idle Time Period: " & Int(ExpiredTime / 60000) & " Minute(s)"
-
ElseIf ExpiredTime > 86400000 Then '> 24 Hours
-
Me![txtExpiredTime] = "Idle Time Period: Sorry, didn't program for that!"
-
Else '> 1 Minute
-
intNumOfMins = Int(ExpiredTime / 60000)
-
Me![txtExpiredTime] = "Idle Time Period: " & intNumOfMins & " Minute(s) and " & _
-
(ExpiredTime - (intNumOfMins * 60000)) / 1000 & " Second(s)"
-
End If
-
End Sub
- Sub IdleTimeDetected(ExpiredMinutes)
-
DoCmd.Close acForm, "frmTest", acSaveNo
-
DoCmd.Quit
-
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.
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.
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.
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.
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
- Whatever Value you give to the Form's Timer Interval Property, remember that it is in Milliseconds, namely:
-
Timer Interval Value Actual (secs.)
-
1000 1
-
5000 5
-
10000 10
-
- The bulk of the Code would be contained within the Form's Timer() Event.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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.
...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
| |