Disabling the app X close is tricky. I have never figured out how to hide the X Close at top right nor the X Close in the app icon right click menu. I think this code was originally written in Access 2007.
Code in a general module:
Code:
Option Compare Database
Option Explicit
'API function to disable the X Close button is for 32-bit machine
'If Lab gets 64-bit machines use the Unload event of Menu form to capture Access X close and Office button Exit
Private Declare PtrSafe Function GetSystemMenu Lib "user32" (ByVal hwnd As Long, ByVal wRevert As Long) As Long
Private Declare PtrSafe Function EnableMenuItem Lib "user32" (ByVal hMenu As Long, ByVal wIDEnableItem As Long, ByVal wEnable As Long) As Long
Public Sub SetAccessXCloseButton(pfEnabled As Boolean)
' Comments: Control the Access close button.
' Disabling it forces the user to exit within the application
' In : pfEnabled TRUE enables the close button, FALSE disabled it
' Owner : Copyright (c) 2008-2009 from FMS, Inc.
' Source : Total Visual SourceBook
' Usage : Permission granted to subscribers of the FMS Newsletter
On Error Resume Next
Const clngMF_ByCommand As Long = &H0&
Const clngMF_Grayed As Long = &H1&
Const clngSC_Close As Long = &HF060&
Dim lngWindow As Long
Dim lngMenu As Long
Dim lngFlags As Long
lngWindow = Application.hWndAccessApp
lngMenu = GetSystemMenu(lngWindow, 0)
If pfEnabled Then
lngFlags = clngMF_ByCommand And Not clngMF_Grayed
Else
lngFlags = clngMF_ByCommand Or clngMF_Grayed
End If
Call EnableMenuItem(lngMenu, clngSC_Close, lngFlags)
End Sub
Then I call that procedure in form Open event. Form is MainMenu set to open by default when db opens and never closes until user exits db.
If Me!Permissions <> "admin" Then Call SetAccessXCloseButton(False)
Here is form Unload event code:
Code:
Private Sub Form_Unload(Cancel As Integer)
'only administrator is permitted to close DB with Office button exit if forms other than Menu are open
'other code disables the X close for non-admin users so this procedure should only run for admin
If Me!Permissions <> "admin" Then
If Forms.Count > 1 Then
MsgBox "Do not use the Office button Exit or X Close to quit database. Use the Menu screen Quit button."
Cancel = True
End If
End If
End Sub