Hi Richard. In answer of your question to send code from ChatGPT here my question and his answer:
Question: I like to use a timer event of 60 seconds on a form in msaccess. When a user is not moving the mouse or keyboard, the form will close. However when there is any action reset the timer. Can you create the vbacode?
Answer ChatGPT which unfortunatly was NOT working. I do not know why. Perhaps you?
Option Compare Database Option Explicit
Private Sub Form_Timer() Static lastInputTime As Date
' Check if there has been any input in the last 60 seconds If DateDiff("s", lastInputTime, Now) >= 60 Then DoCmd.Close acForm, Me.Name End If End Sub
Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single) ' Update last input time on mouse movement UpdateLastInputTime End Sub
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer) ' Update last input time on key press UpdateLastInputTime End Sub
Private Sub UpdateLastInputTime() ' Update last input time to current time Static lastInputTime As Date lastInputTime = Now End Sub
Private Sub Form_Open(Cancel As Integer) Me.TimerInterval = 1000 ' Set timer interval to 1 second Me.TimerOn = True ' Start timer UpdateLastInputTime ' Initialize last input time End Sub
This code uses a lastInputTime variable to keep track of the last time the user input any mouse or keyboard activity. The Form_Timer event checks if the current time is more than 60 seconds after the last input time. If so, it closes the form using the DoCmd.Close method.
The Form_MouseMove and Form_KeyDown events update the lastInputTime variable to the current time whenever the user moves the mouse or presses a key.
The UpdateLastInputTime sub is used to update the lastInputTime variable to the current time. It is called from the Form_MouseMove and Form_KeyDown events as well as from the Form_Open event to initialize the variable.
Note that this solution may not be as precise as the previous one, since it relies on the TimerInterval property, which is not guaranteed to be exactly one second. However, it should work reliably in most cases.
Kevin Robertson
@Reply 3 years ago
There is no TimerOn Method.
I would declare lastInputTime only once.
This appears to work (though will need to be fully tested, and tweaked if necessary):
Option Compare Database
Option Explicit
Dim lastInputTime As Date
Private Sub Form_Timer()
' Check if there has been any input in the last 60 seconds
If DateDiff("s", lastInputTime, Now) >= 60 Then
DoCmd.Close acForm, Me.Name
End If
End Sub
Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
' Update last input time on mouse movement
UpdateLastInputTime
End Sub
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
' Update last input time on key press
UpdateLastInputTime
End Sub
Private Sub UpdateLastInputTime()
' Update last input time to current time
lastInputTime = Now
End Sub
Private Sub Form_Open(Cancel As Integer)
Me.TimerInterval = 1000 ' Set timer interval to 1 second
UpdateLastInputTime ' Initialize last input time
End Sub
That's an interesting bit of code but yeah I can see why it's not working. It does rely on the mouse move so if the user is busy typing with the keyboard and entering in a record and doesn't touch the mouse it's going to cause a problem as well. A few people have asked me about this in the past and I'm kind of against automatically closing down forms and whole databases but since a lot of people have asked maybe I'll add this to my list for a future video. There are some issues to chat GPT isn't aware of LOL. I guess having 30 years of experience working with access may actually be better than artificial intelligence. LOL.
EDIT: I missed that it did cover the keyboard... but that timer event also has some problems.
Kevin Yip
@Reply 3 years ago
One way to do this is to create *one form* specifically to detect idle user activity in all the forms, instead of having to code this for every single form.
We need to define what idleness means, since different people may have different definitions. Here is mine: if the *active form* and the *active control* BOTH remain the same for N seconds or minutes, that means the user isn't doing anything. "Active" means having the focus. If MyForm1 has the focus, and Textbox1 on the form has the focus, and that remains unchanged for N amount of time, that indicates an idle user -- because a non-idle user would be navigating and changing focus among the controls on the form, and/or changing focus among different forms. And when idleness is detected, the clock starts ticking, until it reaches the time of expiration, or not.
That is the idea behind this article from Microsoft:
If you are a Visitor, go ahead and post your reply as a
new comment, and we'll move it here for you
once it's approved. Be sure to use the same name and email address.
This thread is now CLOSED. If you wish to comment, start a NEW discussion in
Visitor Forum.