Solved close the form after 10 second (1 Viewer)

FahadTiger

Member
Local time
Today, 22:38
Joined
Jun 20, 2021
Messages
115
Hi experts.. I get some code below from CHAT-GPT to close form after 10 second when mouse not move
is there any suggest ..thanks
Private Declare PtrSafe Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Private Type POINTAPI
X As Long
Y As Long
End Type

Private m_x As Long
Private m_y As Long
Private m_TimeElapsed As Double
Private m_StopWatchStarted As Boolean
Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
m_x = X
m_y = Y
If Not m_StopWatchStarted Then
m_StopWatchStarted = True
Timer.Enabled = True
End If
End Sub

Private Sub Timer_Timer()
m_TimeElapsed = m_TimeElapsed + Timer.Interval / 1000
If m_TimeElapsed >= 10 Then
Timer.Enabled = False
m_StopWatchStarted = False
m_TimeElapsed = 0
Me.Close
End If
End Sub
 

cheekybuddha

AWF VIP
Local time
Today, 20:38
Joined
Jul 21, 2014
Messages
2,280
The simple version:
Code:
Option Explicit
Option Compare Database

Private LastMoved As Date

Private Sub Form_Load()
  LastMoved = Now
  Me.TimerInterval = 1000
End Sub

Private Sub Form_Timer()
  If DateDiff("s", LastMoved, Now) > 10 Then
    DoCmd.Close acForm, Me.Name
  End If
End Sub

Private Sub Detail_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
  LastMoved = Now
End Sub

It gets more complex if you have lots of controls on your form and subforms, form headers or form footers as well, but that should give you a start.
 

FahadTiger

Member
Local time
Today, 22:38
Joined
Jun 20, 2021
Messages
115
The simple version:
Code:
Option Explicit
Option Compare Database

Private LastMoved As Date

Private Sub Form_Load()
  LastMoved = Now
  Me.TimerInterval = 1000
End Sub

Private Sub Form_Timer()
  If DateDiff("s", LastMoved, Now) > 10 Then
    DoCmd.Close acForm, Me.Name
  End If
End Sub

Private Sub Detail_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
  LastMoved = Now
End Sub

It gets more complex if you have lots of controls on your form and subforms, form headers or form footers as well, but that should give you a start.
thank you cheekybuddha.. its simple and good for now..I dont know if I will get more complex at future
 

cheekybuddha

AWF VIP
Local time
Today, 20:38
Joined
Jul 21, 2014
Messages
2,280
Out of curiosity, what was the exact question you asked ChatGPT?
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 14:38
Joined
Feb 28, 2001
Messages
27,188
Just as a side note, we have seen other code posted here by ChatGPT for which there is some question as to accuracy. It is entirely possible that ChatGPT doesn't know (or doesn't care) about differences between VBA, VB scripting, and VB6.
 

Users who are viewing this thread

Top Bottom