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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Closing Access

Status
Not open for further replies.

mjake13

Programmer
Aug 26, 2001
28
0
0
US
How do you check for keystrokes? I have a hidden form and am using Microsofts code for detecting idle time. My problem is that the time is not being reset when people work in a memo field. Other posts suggest checking for keystrokes. Where and what code would be inserted in the following code?
Any help would be useful!!! Here's the code:

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

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 = &quot;&quot;) Or (PrevFormName = &quot;&quot;) 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


New line-
Sub IdleTimeDetected(ExpiredMinutes)
Application.Quit acQuitSaveAll
End Sub


 
To detect keystrokes, set your form's Key Preview property to Yes, and create a Form_KeyDown event procedure. The procedure will be called every time a key is pressed while the form has the focus. This won't work in the hidden form, because it can't get keystrokes; you'll have to do this in each visible form, and reset the hidden form's TimerInterval. Rick Sprague
 
Rick,
After doing some reading through the help files (duh), could I just use the key press event in the same way? Also, how would I code it to reset the timer interval? (This one is way out of my league...for now)

Thanks
 
Yes, you could use KeyPress instead. (For your purpose, the only difference is that KeyDown will fire when a Ctrl key, Shift key, or function key is pressed; KeyPress only occurs for character-generation keys and Ctrl-x sequences.)

To reset the timer interval to 'nnn' milliseconds for a form named, for example, frmHidden, you can execute the following VBA statement:
Form_frmHidden.TimerInterval = nnn

This works because for every form someForm, Access creates a global variable named Form_someForm that refers to it. (It's actually more complicated than that, but this will work for you until you start learning about class modules.) Rick Sprague
 
Rick,
Would I reset the forms timer interval or would I need to reset the actual timer built into the form through the code? Just pondering....
 
Actually, you'd reset the programmed timer interval. I didn't read the code deeply enough to understand that you weren't using just the form property.

Unfortunately, you've got the programmed timer interval as a private variable in the Form_Timer procedure. You can't change (or even reference) that from outside the form. You'll have to move it to the form's Declarations section, and change Static to Public on it. Then it's available from outside the form.

There's a tiny chance that a keystroke will happen just as the form's Timer goes off, so that both the KeyPress event and the Timer event are trying to update the variable concurrently. The result will probably be that one of these events will have no net effect on the overall timer function, because the other event overwrites the new ExpiredTime. That's probably not a problem, but if it is you'll have to add a semaphore variable to control it. If you think you'll need that, let me know. Rick Sprague
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top