You can disable the close button (x on the right top of the application) with this code:Hi all
on my database I have macro buttons to close tabs etc, is there a way to disable the main window close 'X' button for the whole of the access database?
#If Win64 Then
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
#Else
Private Declare Function GetSystemMenu Lib "USER32" (ByVal hWnd As Long, ByVal wRevert As Long) As Long
Private Declare Function EnableMenuItem Lib "USER32" (ByVal hMenu As Long, ByVal wIDEnableItem As Long, ByVal wEnable As Long) As Long
#End If
Public Sub AccessCloseButtonEnabled(pfEnabled As Boolean)
' Comments: Control the Access close button.
' Disabling it forces the user to exit within the application
' Params : pfEnabled TRUE enables the close button, FALSE disabled it
' Owner : Copyright (c) 2008-2011 from FMS, Inc.
' Source : Total Visual SourceBook
' Usage : Permission granted to subscribers of the FMS Newsletter
' Reference http://www.fmsinc.com/microsoftaccess/startup/preventclose.asp
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
Access closes forms in the inverse order in which they were opened. If you have code that needs to run when the app closes, put it in the form that opens when the app opens like a login or switchboard. Then when the user logs in, don't close the login form, hide it. It will stay resident and be the last form that closes. Or if the code is in your menu/switchboard, hide that form when you open other forms.However, I assume you want to ensure users close the app from one of your forms in order to ensure certain code runs before closedown.
As I suspected.I disable the access close button to ensure that the user closes the application from the main menu. Otherwise they could have another form open
For what reason would you ever "need" to prevent the application from closing except to protect data?you need to prevent user closing the database
Maybe, just maybe, you need code in the form's BeforeUpdate event to prevent the form from closing if it is incomplete. What happens if there is a power outage while this process is going on? What if someone spills orange juice on the keyboard? If the user just loses something he typed, then maybe he should pay a little more attention. When you prevent the form from closing because it is incomplete or has an error, you can also prevent the form from unloading and that will prevent the database from closing. So, you protect the things that need protecting and the ripple effect protects the other stuff.If users make a mistake and click on X button of the database (while he wants to close other applications) everything is gone.
You forgot to read the copyright text?You can disable the close button (x on the right top of the application) with this code:
I actually used it several years ago.
Code:#If Win64 Then 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 #Else Private Declare Function GetSystemMenu Lib "USER32" (ByVal hWnd As Long, ByVal wRevert As Long) As Long Private Declare Function EnableMenuItem Lib "USER32" (ByVal hMenu As Long, ByVal wIDEnableItem As Long, ByVal wEnable As Long) As Long #End If Public Sub AccessCloseButtonEnabled(pfEnabled As Boolean) ' Comments: Control the Access close button. ' Disabling it forces the user to exit within the application ' Params : pfEnabled TRUE enables the close button, FALSE disabled it ' Owner : Copyright (c) 2008-2011 from FMS, Inc. ' Source : Total Visual SourceBook ' Usage : Permission granted to subscribers of the FMS Newsletter ' Reference http://www.fmsinc.com/microsoftaccess/startup/preventclose.asp 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
To disable the X button, run this :
AccessCloseButtonEnabled Flase
To enable it again :
AccessCloseButtonEnabled True
@Pat Hartman I don't think you ever read what I posted. And if you did, you didn't understand the situation. Maybe because of my English.For what reason would you ever "need" to prevent the application from closing except to protect data?
Do you mean don't try to do what I have in my power because every 100 years there may be a power shortage? I do what I can do, and if there was a power shortage, then that's that.What happens if there is a power outage while this process is going on? What if someone spills orange juice on the keyboard?
Again you're not listening. The form IS NOT INCOMPLETE. The form is empty and has nothing. The form is just waiting for a log file to come in.When you prevent the form from closing because it is incomplete or has an error, you can also prevent the form from unloading and that will prevent the database from closing.
That was also the motivation for my thoughts in #2. Which form exactly controls my NoClose flag would of course depend on a specific use case.When you prevent the form from closing because it is incomplete or has an error, you can also prevent the form from unloading and that will prevent the database from closing.
And the before update won't be triggered if the form is not dirty.We cannot forget that there are no update events on an unbound form,