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
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,
The OP will go away happy because he thinks he stopped Access from closing until the method fails due to something beyond his control. Users do some seriously stupid things.
This has not yet been discussed, but it is suitable to continue the discussion with a further 13 contributions and to increase the "popularity" of the topic.save button