Computer Learning Zone CLZ Access Excel Word Windows

Tell me and I'll forget; show me and I may remember; involve me and I'll understand.

-Chinese Proverb
 
Home   Courses   TechHelp   Forums   Help   Contact   Merch   Join   Order   Logon  
 
Back to Visitor Forum    Comments List
Upload Images   @Reply   Bookmark    Link   Email   Next Unseen 
Code form ChatGPT
fred zwalm 

3 years ago
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
Richard Rost  @Reply  
          
3 years ago
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.
Richard Rost  @Reply  
          
3 years ago
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:

https://learn.microsoft.com/en-us/office/vba/access/concepts/miscellaneous/detect-user-idle-time-or-inactivity

I have not done this myself, but it seems like a promising solution for this problem.
fred zwalm OP  @Reply  

3 years ago
to Kevin Robertson: Works great!! thanx

grt. Fred

This thread is now CLOSED. If you wish to comment, start a NEW discussion in Visitor Forum.
 

Next Unseen

 
New Feature: Comment Live View
 
 

The following is a paid advertisement
Computer Learning Zone is not responsible for any content shown or offers made by these ads.
 

Learn
 
Access - index
Excel - index
Word - index
Windows - index
PowerPoint - index
Photoshop - index
Visual Basic - index
ASP - index
Seminars
More...
Customers
 
Login
My Account
My Courses
Lost Password
Memberships
Student Databases
Change Email
Info
 
Latest News
New Releases
User Forums
Topic Glossary
Tips & Tricks
Search The Site
Code Vault
Collapse Menus
Help
 
Customer Support
Web Site Tour
FAQs
TechHelp
Consulting Services
About
 
Background
Testimonials
Jobs
Affiliate Program
Richard Rost
Free Lessons
Mailing List
PCResale.NET
Order
 
Video Tutorials
Handbooks
Memberships
Learning Connection
Idiot's Guide to Excel
Volume Discounts
Payment Info
Shipping
Terms of Sale
Contact
 
Contact Info
Support Policy
Mailing Address
Phone Number
Fax Number
Course Survey
Email Richard
[email protected]
Blog RSS Feed    YouTube Channel

LinkedIn
Copyright 2026 by Computer Learning Zone, Amicron, and Richard Rost. All Rights Reserved. Current Time: 5/1/2026 10:40:36 PM. PLT: 0s