Can please some one tell me how to disable "Microsoft access" control box

rk2002

New member
Local time
Today, 20:31
Joined
Mar 8, 2004
Messages
8
i need help on disabling the main access close button so that the only way a user can close my system is by clicking on my command button

i have tried putting the following code in my module:
Option Compare Database
Option Explicit

Public Declare Function apiEnableMenuItem Lib "user32" Alias "EnableMenuItem" (ByVal hMenu As Long, ByVal wIDEnableMenuItem As Long, ByVal wEnable As Long) As Long
Public Declare Function apiGetSystemMenu Lib "user32" Alias "GetSystemMenu" (ByVal hWnd As Long, ByVal flag As Long) As Long

Public Function EnableDisableControlBoxX(bEnable As Boolean, Optional ByVal lhWndTarget As Long = 0) As Long

On Error GoTo Err_EnableDisableControlBoxX

Const MF_BYCOMMAND = &H0&
Const MF_DISABLED = &H2&
Const MF_ENABLED = &H0&
Const MF_GRAYED = &H1&
Const SC_CLOSE = &HF060&

Dim lhWndMenu As Long
Dim lReturnVal As Long
Dim lAction As Long

lhWndMenu = apiGetSystemMenu(IIf(lhWndTarget = 0, Application.hWndAccessApp, lhWndTarget), False)

If lhWndMenu <> 0 Then
If bEnable Then
lAction = MF_BYCOMMAND Or MF_ENABLED
Else
lAction = MF_BYCOMMAND Or MF_DISABLED Or MF_GRAYED
End If
lReturnVal = apiEnableMenuItem(lhWndMenu, SC_CLOSE, lAction)
End If

EnableDisableControlBoxX = lReturnVal

Exit_EnableDisableControlBoxX:
Exit Function

Err_EnableDisableControlBoxX:
ErrorMsg "dProgramFunctions", "EnableDisableControlBoxX", Err.Number, Err.description
Resume Exit_EnableDisableControlBoxX

End Function

and tried running it from a command button using the following code

'I test if the current user is me (the programmer). Use this in a forms OnOpen event or a transparent command button...
If CurrentUser <> "programmer" Then
EnableDisableControlBoxX (False) 'Disable the programs X (close) function
Exit Sub
Else
EnableDisableControlBoxX (True)
End If

Itt gives me a compile error on the following line telling me that the sub or function is not defined.!

Public Function EnableDisableControlBoxX(bEnable As Boolean, Optional ByVal lhWndTarget As Long = 0) As Long


Pleaaaaaaassssssssssse Help me!!!!
 
I don't know why the code isn't working for you. A different method that you can use to exert some control over how Access is closed is to use a hidden form as the start up form. The hidden form then opens the application's switchboard or main form. This hidden form will be the last object to close when someone attempts to close the db. You can have it determine if your close button was pressed (use an unbound control on the hidden form to communicate this information). Then if it was not, you can execute the code yourself by calling it or you can cancel the hidden form's unload event which will prevent it from closing and that will prevent Access from closing. Keep in mind that all the other objects will already have closed so you'll need to open a visible form.
 
Last edited:
Pat Hartman said:
... exert some control over how Access is closed is to use a hidden form as the start up form. The hidden form then opens the application's switchboard or main form. This hidden form will be the last object to close when someone attempts to close the db. ..


Ah hah! Finally found one of the replies that I have been searching for.

Pat Hartman, have you described how to make a hidden form on startup in a post elsewhere? ghudson has informed me how to use it via doCmd's acHidden property but I haven't figured out how to do it for a startup form.

Anyone?
 
In the form's Open event, set its visible property to False.

Me.Visible = False
 
Pat Hartman said:
you can cancel the hidden form's unload event which will prevent it from closing and that will prevent Access from closing.

Ok, how do you kill the hidden form's unload event?
 
o1110010 said:
ghudson has informed me how to use it via doCmd's acHidden property but I haven't figured out how to do it for a startup form.
Put this in your start up forms OnOpen event to disable the Access application close X button...
Code:
    EnableDisableControlBoxX (False)
HTH
 
The following code will disable the close button

Private Declare Function GetSystemMenu Lib "user32" (ByVal hwnd As Long, ByVal bRevert As Long) As Long
Private Declare Function EnableMenuItem Lib "user32" (ByVal hMenu As Long, ByVal wIDEnableItem As Long, ByVal wEnable As Long) As Long
Private Const WM_NCLBUTTONDOWN = &HA1
Private Const HTCAPTION = 2
Private Const MF_BYCOMMAND = &H0&
Private Const MF_DISABLED = &H2&
Private Const MF_GRAYED = &H1&
Private Const SC_CLOSE = &HF060&

Sub DisableCloseButton(Optional lngHWND As Long = 0)
Dim vntMenu As Variant
Dim vntRETP As Variant
Dim lngTargetHWND As Long

'Find the window.
If lngHWND = 0 Then
'If you want MS Access's close to be disabled
lngTargetHWND = Application.hWndAccessApp
Else
'If you want any other program's close disabled - need hwnd
lngTargetHWND = lngHWND
End If
If lngTargetHWND <> 0 Then
vntMenu = GetSystemMenu(lngTargetHWND, False)
vntRETP = EnableMenuItem(vntMenu, SC_CLOSE, MF_BYCOMMAND Or MF_DISABLED Or MF_GRAYED)
End If
End Sub
 
Oh yes!! ;) This works the best from all the variants I have tried Thank You!!
 
This works great. Straightforward, easy to use.

What would the code look like to reverse the process i.e. if I wanted to put something behind the On Click event for a button, allowing the ‘X’ to be re-enabled while the database was open? Can’t think of why I would need to, but I’m trying to better understand the use of ‘H” values to alter controls.

Alternatively, is there an online list of the various ‘H’ values?
 
Just do the opposite.

Code:
EnableDisableControlBoxX (True)
 
Thanks for that, but I wasn't being clear enough. I used the code suggested by brianb99999.

I wasn't stupid enough to use it the live version of the db (tested it successfully on a copy) and before I do I'd like to know it can be easily undone. I know the 'X' works again the next time the db is opened, but I'd like to understand how to change it at will.
 
Can't help you there with the othe code but the code I have posted works fine as is to enable/disable the X when ever you call it. Why not use what works?
 
I just tried it out but obviously did something wrong.

My steps were as follows:

1) I created a new module and called it EnableDisableControlBoxX. the entire contents are as follows:
----------------------------------------------------------
Option Compare Database
Option Explicit

Public Declare Function apiEnableMenuItem Lib "user32" Alias "EnableMenuItem" (ByVal hMenu As Long, ByVal wIDEnableMenuItem As Long, ByVal wEnable As Long) As Long
Public Declare Function apiGetSystemMenu Lib "user32" Alias "GetSystemMenu" (ByVal hWnd As Long, ByVal flag As Long) As Long

Public Function EnableDisableControlBoxX(bEnable As Boolean, Optional ByVal lhWndTarget As Long = 0) As Long
On Error GoTo Err_EnableDisableControlBoxX

Const MF_BYCOMMAND = &H0&
Const MF_DISABLED = &H2&
Const MF_ENABLED = &H0&
Const MF_GRAYED = &H1&
Const SC_CLOSE = &HF060&

Dim lhWndMenu As Long
Dim lReturnVal As Long
Dim lAction As Long

lhWndMenu = apiGetSystemMenu(IIf(lhWndTarget = 0, Application.hWndAccessApp, lhWndTarget), False)

If lhWndMenu <> 0 Then
If bEnable Then
lAction = MF_BYCOMMAND Or MF_ENABLED
Else
lAction = MF_BYCOMMAND Or MF_DISABLED Or MF_GRAYED
End If
lReturnVal = apiEnableMenuItem(lhWndMenu, SC_CLOSE, lAction)
End If

EnableDisableControlBoxX = lReturnVal

Exit_EnableDisableControlBoxX:
Exit Function

Err_EnableDisableControlBoxX:
ErrorMsg Err.Number & " - " & Err.Description
Resume Exit_EnableDisableControlBoxX

End Function

----------------------------------------------------------

2) I put the following line of code behind the OnClick event of a button.
--------------------------------
EnableDisableControlBoxX (True)
--------------------------------

3) When I click the button, I get the following error message:
--------------------------------------------
'Compile Error:

Expected variabke or procedure, not module.'
--------------------------------------------

I (obviously) have little experience of using functions, so is my error in the way I'm calling it or in how I set up what's being called?
 
rename your module so it doesn't have the same name as the function you are trying to call.
 
The module and function name can not be the same and that is exactly what you have done. Rename your module [stick a mod prefix in front of it].

modEnableDisableControlBoxX
 
Thanks, both.

Knew it would be something simple.
Enable and Disable buttons work perfectly, now.
 

Users who are viewing this thread

Back
Top Bottom