troubleshooting Question

How to circumvent a Form_Unload event after issuing an Application.Quit command

Avatar of Nick67
Nick67Flag for Canada asked on
Microsoft AccessVBA
6 Comments1 Solution113 ViewsLast Modified:
Consider the attached file.
I has a form with the following code behind it
Option Compare Database
Option Explicit
Const timeToRun As Date = #11:17:00 AM#
Const DelayFromLastActivity As Long = 1
'minutes of delay
Public TheInterval As Long
Public db As Database
Public rs As Recordset
Dim SkipMessage As Boolean

Private Sub Form_Activate()

If DateDiff("s", Time(), timeToRun) < DelayFromLastActivity Then
    TheInterval = DelayFromLastActivity * 60 * 1000
Else
    TheInterval = DateDiff("s", Time(), timeToRun) * 1000
End If
Me.TimerInterval = TheInterval

End Sub

Private Sub Form_Current()
'this should only fireon opening because this form is unbound
'MsgBox "Current Event fired"
'MsgBox GetLocalTimeFromGMT(timeToRun)
'MsgBox ConvertLocalToGMT(timeToRun)

If DateDiff("s", Time(), timeToRun) < DelayFromLastActivity Then
    TheInterval = DelayFromLastActivity * 60 * 1000
Else
    TheInterval = DateDiff("s", Time(), timeToRun) * 1000
End If
Me.TimerInterval = TheInterval

End Sub



Private Sub Form_Timer()

Set db = CurrentDb
Set rs = db.OpenRecordset("select *  from tblTimes where 1=2;", dbOpenDynaset, dbSeeChanges)
With rs
    .AddNew
    !thedatetime = Now()
    !theuser = ReturnUserName
    !thecomputer = ReturnComputerName
    .Update
    Me.txtTimer.Value = Now()
    Me.txtTimer.Requery
End With
rs.Close
Set rs = Nothing
db.Close
Set db = Nothing
SkipMessage = True
Application.Quit acQuitSaveAll
SkipMessage = True
End Sub


Private Sub Form_Unload(Cancel As Integer)
Dim response As Integer
If SkipMessage = False Then
    response = MsgBox("Did you really intend to quit The Program? Yes to Quit, no to remain in The Program", vbYesNo + vbCritical, "Quit?")
    If response = vbNo Then
        Cancel = True
    End If
End If
End Sub

Open in new window


The application is designed to close itself based on two constants
Const timeToRun As Date = #11:17:00 AM#
Const DelayFromLastActivity As Long = 1
Adjust these in your tests as needful.

Now, I want the application to close DelayFromLastActivity minutes after the last form activity after timeToRun time.
It does this swimmingly.

If the Timer event is NOT the cause of the form's closure, I need a message allowing the user to bail on the closure.
This, too, works swimmingly.

However, you can watch this go down in the Timer event


SkipMessage = True 'skip the prompt in the unload
Application.Quit acQuitSaveAll
SkipMessage = True ' dammit I said skip the prompt in the unload


SkipMessage gets set to False at the end of the Timer event, regardless.
So, when I want the Timer to provide an automated close, instead I get a message box whose response gets ignored.
I don't want the message box to fire if the Timer event is the cause of the Unload()
Making SkipMessage a Public variable of  a code module doesn't help the issue, either.

So, what does Application.Quit do to module-level variables and how can I get clean, automated shutdown?
AutoClose.mdb
ASKER CERTIFIED SOLUTION
omgang
IT Manager

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Log in to continue reading
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform for $9.99/mo
View membership options
Unlock 1 Answer and 6 Comments.
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
The Value of Experts Exchange in My Daily IT Life

Experts Exchange (EE) has become my company's go-to resource to get answers. I've used EE to make decisions, solve problems and even save customers. OutagesIO has been a challenging project and... Keep reading >>

Mike

Owner of Outages.IO
Phoenix, Arizona, United States
Member Since 2016
Join a full scale community that combines the best parts of other tools into one platform.
Unlock 1 Answer and 6 Comments.
View membership options
“All of life is about relationships, and EE has made a virtual community a real community. It lifts everyone's boat.”
William Peck

Member since 2004