Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Forced shutdown/inactivity shutdown

OpnSeasonOpnSeason Member Posts: 12
Hello Hungry Code Vultures!

This is the code, strategy, etc. for forced shutdown or inactivity shutdown of an app.
I think I have everything included here. I had to cherry pick all this from the working application.
The one bug that I haven't had a chance to debug in all this is that, on a fatal empirical error that doesn't get trapped in the app, it causes shutdown to occur as if the Boolean flag, ShowMsg, is set to True in the MsgCenter table.


The variables:

Public MsgDB As Database
Public MsgTblOpn As Boolean
Public MsgTbl As Recordset
Public IsInactive As Boolean
Public sngStartTime As Single
Public ShutDownNow As Boolean
Public sngElapsedTime As Single

are declared in a module in the Module window of the database to make them visible to all modules.


The code:

Public Function OpnMsgCenterTbl()

On Error GoTo OpnMsgCenterTblErr

Set MsgDB = CurrentDb
Set MsgTbl = MsgDB.OpenRecordset("MsgCenter", DB_OPEN_SNAPSHOT)
MsgTblOpn = True

If MsgTbl!ShowMsg Then
ShutDownNow = True
End If

OpnMsgCenterTblXit:

Exit Function

OpnMsgCenterTblErr:
If MsgTblOpn Then
MsgTbl.Close
MsgTblOpn = False
End If
Set MsgDB = Nothing
Resume OpnMsgCenterTblXit
End Function

is contained in a module in the module window of the database and is executed in the autoexec macro before opening ShutdownForm. Macro call: RunCode - Function Name - OpnMsgCenterTbl().



We have a ShutdownMsg form. This form displays itself on inactivity shutdown or forced shutdown.
The control ShutDnMsg is a label that is used to display the message in field MSG in the MsgCenter table.
The control Counter is a Textbox for displaying the decrementing seconds till shutdown.
The Ok button allows the user to immediately exit the app.

Pop Up is set to True.
Modal is set to False.
Scroll Bars is set to Neither.
Record Selectors is set to False.
Navigation Buttons is set to False.
Auto Center is set to True.
Modal is set to False.

Code in the ShutdownMsg form module:

Option Compare Database
Option Explicit

Private Sub Form_Open(Cancel As Integer)
On Error Resume Next

If MsgTbl!ShowMsg Then
Me.Caption = MsgTbl!MsgBxTitlBar
Me.ShutDnMsg.Caption = vbCrLf & vbCrLf & MsgTbl!MSG
Else
Me.ShutDnMsg.Caption = "This application is shutting down due to inactivity of " & MsgTbl!MaxInactiveTime & " minute" & IIf(MsgTbl!MaxInactiveTime > 1, "s", "") & "."
End If

End Sub

Private Sub OkButton_Click()
DoCmd.Close acForm, Me.Name
End Sub



MsgCenter table:

Fields:
ShowMsg - Boolean - set to False normally - manually set to True for forced shutdown.
MSG - Text - Message to display for forced shutdown - "This application needs shutdown for maintenance" - for ShutdownMsg form.
MsgBxTitlBar - Text - "Application Maintenance" - for ShutdownMsg form.
MaxInactiveTime - Single - Max Inactivity Minutes - set to 180 minutes in this app.
StartMonitor - Integer - Time of day to start monitoring inactivity - set to 17 (17:00 Hrs 7PM) in this app.
EndMonitor - Integer - Time of day to end monitoring inactivity - set to 5 (5:00 Hrs 5AM) in this app.



This code is in the ShutdownForm module. ShutdownForm is the actual name of the form.
The form is opened in an autoexec macro with Window Mode set to Hidden.
Macro Call: OpenForm - Form Name - ShutdownForm - View - Form - Window Mode - Hidden.
It monitors a linked table called MsgCenter for inactivity or forced shutdown. Named MsgCenter to not reveal its true purpose in its table name.

The Timer Interval is set to 1000.

Option Compare Database
Option Explicit

Dim ctlSave As Control
Private Const SW_RESTORE = 9
Private Const SWP_NOZORDER = &H4
Private Const SWP_NOMOVE = &H2
Private Const SWP_NOSIZE = &H1
Private Const SWP_SHOWWINDOW = &H40
Private Const HWND_TOP = 0
Private Const HWND_TOPMOST = -1
Private Declare Function SetForegroundWindow& Lib "user32" (ByVal hwnd As Long)
Private Declare Function IsIconic Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
Private Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, ByVal x As Long, ByVal Y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long
Dim frmSave As Form

Private Sub Form_Close()
If Forms.Count = 1 And Reports.Count = 0 Then
Set MsgTbl = Nothing
Set MsgDB = Nothing
End If
Me.TimerInterval = 0
End Sub

Private Sub Form_Open(Cancel As Integer)
sngStartTime = Timer
End Sub

Private Sub Form_Timer()
Dim ctlNew As Control, x As Integer, CurrentHour As Integer, frmNew As Form, InactivMsg$

On Error Resume Next

If MsgTblOpn Then
MsgTbl.Requery ' get the latest setting
If MsgTbl!ShowMsg Then
ShutDownNow = True
Me.TimerInterval = 0
End If
End If


If ShutDownNow Then
sngElapsedTime = MsgTbl!MaxInactiveTime + 1
GoTo ShDnNow
End If

CurrentHour = Hour(Time)

If Forms.Count > 1 Then
If (CurrentHour >= MsgTbl!StartMonitor And CurrentHour <= 24) Or _
(CurrentHour >= 1 And CurrentHour <= MsgTbl!EndMonitor) Then

Set frmNew = Screen.ActiveForm
Set ctlNew = Screen.ActiveControl
If Err <> 0 Then
'* No activecontrol
sngElapsedTime = (Timer - sngStartTime) / 60
Err.Clear
Else
sngElapsedTime = (Timer - sngStartTime) / 60
If ctlNew.Name = ctlSave.Name And frmNew.Name = frmSave.Name Then
'* Still at same control
sngElapsedTime = (Timer - sngStartTime) / 60
Else
'* Some change has occured, we're at a new control and/or a new form
Set frmSave = frmNew
Set ctlSave = ctlNew
sngStartTime = Timer
sngElapsedTime = (Timer - sngStartTime) / 60
End If
If Err <> 0 Then
Set frmSave = Screen.ActiveForm
Set ctlSave = Screen.ActiveControl
End If
End If
Err.Clear
End If ' (CurrentHour >= MsgTbl!StartMonitor And CurrentHour <= 24) or (CurrentHour >=1 and CurrentHour <=MsgTbl!EndMonitor )

Set ctlNew = Nothing
End If 'Forms.Count > 1

ShDnNow:

If sngElapsedTime >= MsgTbl!MaxInactiveTime Then

'* Un-minimize Access application if it is minimized
If IsIconic(Application.hWndAccessApp) Then
ShowWindow Application.hWndAccessApp, SW_RESTORE
End If
'* Make it the foreground window - open it in front of other application windows.
SetForegroundWindow (Me.hwnd)

'* Open it on top of other modal windows.
SetWindowPos Me.hwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE Or SWP_SHOWWINDOW

Beep
DoCmd.OpenForm "ShutdownMsg"
Forms![ShutdownMsg]![Counter] = 20
Forms![ShutdownMsg]![Counter].Repaint
For x = 1 To 20000
DoEvents
If Not IsFrmOpn("ShutdownMsg") Then
Exit For
Else
If (x / 1000) = CInt((x / 1000)) Then
Forms![ShutdownMsg]![Counter] = (20000 / 1000) - (x / 1000)
Forms![ShutdownMsg]![Counter].Repaint
End If
End If
Next x

MsgTbl.Close
MsgTblOpn = False
Set MsgTbl = Nothing

'* Set global timeout variable, then shut down each form
'* This code can be used if there is code in the form's BeforeUpdate,
'* or OnClose event procedure that requires user input.
'* The variable "gintInactiveTimeout" can be checked in the form events
'* and can be used to prevent the user prompt code from executing.
Dim frm As Form

For Each frm In Forms
DoCmd.Close acForm, frm.Name
Next frm

Set frm = Nothing
Set frmSave = Nothing
Set ctlSave = Nothing
Set ctlNew = Nothing
Set frmNew = Nothing
Set MsgDB = Nothing
DoCmd.Quit

End If

End Sub


The last entry in the autoexec macro for opening the main form of the application:
Macro Call: OpenForm -
. Condition - OpnApp()
This prevents the app from initializing if the Boolean flag, ShowMsg, is set to True in the MsgCenter table.

Code contained in the Module window of the database:

Public Function OpnApp() As Boolean
OpnApp = (Not ShutDownNow)
End Function


This is intense shutdown strategy, but sometimes ya gotta do what ya gotta do....

Sign In or Register to comment.