How to Set Program to Close Automatically




Set Program to Close Automatically

MS Access program can be closed automatically if there is no activity. In this How To, the method of detecting the idle on active form and active control will be used. The idle time will count when a new form is opened or get focus and/or a control is changed within the active form. There are three steps to make this happen.

  1. Create a Detect Idle Time form
  2. Apply the VB code on the Timer event procedure
  3. Create Macro to load the Detect Idle Time form and hide it when program is opened.

detecting form

Step: 1 – Create a Detect Idle Time form
  • Create a form and names it “DetectIdle Time”
  • Open form in design mode
  • Insert the 1st textbox and names it “txtOpenForm”
  • Change a text label to “Active Form”

detect idle from

  • Insert the 2nd textbox and names it “txtMinutes”
  • Change a text label to “Amount of Time”

txt minutes

  • Change the value of Timer Interval to 1000
  • Select the [Event Procedure] for On Timer event

set timer

Step: 2 – Create or Apply the VB code on the Timer Event Procedure
  • Set Static old control and old name
Static OldControlName As String
Static OldFormName As String
Static ExpiredTime
  • Declare a new control and new form
Dim ActiveFormName As String
Dim ActiveControlName As String
Dim ExpiredMinutes
  • Set new current control and current form to new control and new form
ActiveFormName = Screen.ActiveForm.Name
Me.txtOpenForm = ActiveFormName
ActiveControlName = Screen.ActiveControl.Name
  • If old control or old form not equal to new control or new form then set ExpiredTime = 0 as not ticking a clock
If (OldControlName = "") Or (OldFormName = "") _
Or (ActiveFormName <> OldFormName) _
Or (ActiveControlName <> OldControlName) Then
OldControlName = ActiveControlName
OldFormName = ActiveFormName
ExpiredTime = 0
  • Else keep ticking a clock for counting the idle time
ExpiredTime = ExpiredTime + Me.TimerInterval
  • Convert milliseconds to minute
ExpiredMinutes = (ExpiredTime / 1000) / 60
  • Close program if the ticking clock reaching a set time.
If ExpiredMinutes >= 10 Then
ExpiredTime = 0
' ...quit application
MsgBox "No activity in last " & "10 minutes", vbCritical, "Terminate Program"
Application.Quit acSaveYes
End If




Full VB code below:

full code

For Testing: you can set the expired time in 20 seconds like

  1. Set “ExpiredMinutes = (ExpiredTime / 1000)”
  2. Change “If ExpiredMinutes >= 20 then”
Step 3 – Create Macro
  • Click on Create Menu and Click on Macro

create Macro

  • Name Macro as “AutoExec”
  • Select OpenForm command and select DetectIdle Time form
  • Select Hidden for window mode
  • Save and close

open form macro

Related Video

Related posts