Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

MS Access: Auto form close on timeout after last key press

Status
Not open for further replies.

mikeg8

Technical User
Sep 11, 2003
32
GB
Don't get all complicated if you do reply: what I want to do might be clever...I'm not!

MS Access 2000 on XP Pro. I have a form open for editing and want to automatically close the form say 5 mins after last key press or mouse click. I can get the form to close after n millisecs, but I cannot get the TimerInterval to reset to n after a key press or mouse click.

Its a tiny prob in reality and doesn't affect the app at all, but it would be neat to be able to do so!

Hopefully

Mike
 
Mike,

This has probably been viewed by numerous people...
And any considering replying have decided it's too complicated.

I "just happened" to have example code.
It's up to you to decide if this is "all complicated".

You need to create an On Timer event for the form and copy the following code into it. Also, you need to set the Timer Interval property to how often you want to have the elapsed idle time checked. 10000 = Check every 10 seconds as the field is in milliseconds. Don't reduce this too much because it's going to be executing even when the user is actively using the form. In my case, I didn't want to close the form, just notify the user of idle time - I commented those lines by putting ' at the beginning.

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

Static strPrevControlName As String
Static strPrevFormName As String
Static sngExpiredTime As Single

Dim strActiveFormName As String
Dim strActiveControlName As String
Dim sngExpiredMinutes As Single

On Error Resume Next

' Get the active form and control name.

strActiveFormName = Screen.ActiveForm.Name
If Err Then
strActiveFormName = "No Active Form"
Err = 0
End If

strActiveControlName = Screen.ActiveControl.Name
If Err Then
strActiveControlName = "No Active Control"
Err = 0
End If

' Record the current active names and reset sngExpiredTime 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 (strPrevControlName = "") Or (strPrevFormName = "") _
Or (strActiveFormName <> strPrevFormName) _
Or (strActiveControlName <> strPrevControlName) Then
strPrevControlName = strActiveControlName
strPrevFormName = strActiveFormName
sngExpiredTime = 0
Else
' ...otherwise the user was idle during the time interval, so
' increment the total expired time.
sngExpiredTime = sngExpiredTime + Me.TimerInterval
End If

' Does the total expired time exceed the IDLEMINUTES?
sngExpiredMinutes = (sngExpiredTime / 1000) / 60
If sngExpiredMinutes >= IDLEMINUTES Then
' ...if so, then reset the expired time to zero...
sngExpiredTime = 0
' ...and call the IdleTimeDetected subroutine.
IdleTimeDetected sngExpiredMinutes
End If
End Sub


Sub IdleTimeDetected(sngExpiredMinutes)
Dim strMessage As String
'strMessage = &quot;No user activity detected in the last&quot; & vbCrLf
'strMessage = strMessage & sngExpiredMinutes & &quot; minute(s)!&quot;
'MsgBox strMessage, vbInformation, &quot;No Sign of Activity!&quot;

DoCmd.Close

End Sub


HTH,
Bob
Thread181-473997 provides information regarding this site.
 
Bob

Wow! Thanks for the reply. Don't know much about Access yet (you may have guessed!) but reply and code snippet much appreciated. I shall look at this now and see how I can incorporate it.

Ta Da

Mike
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top