How to Disable(gray out) X button in Access window

jmriddic

Registered User.
Local time
Today, 12:25
Joined
Sep 18, 2001
Messages
150
Hi,

How do i gray out/disable the X button on the main application window. I believe this is causing errors when the users do this and then try to get back into the database. I would like them to use close buttons on the forms I have. I have grayed the ones on the forms just not the application window. Even giving them instructions does seem to stop them doing this so I need make them use the buttons.
 
This will allow you to disable the 'X' to prevent the user from closing your application...

'Copy this function into a new module...
'Courtesy of Calvin Smith

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

'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

'HTH
 
Didn't work

Hi,

I tried it but it didn't work

Here is what I have in Startup Form open method.


ListDesc.Requery
txtInput.SetFocus
EnableDisableControlBoxX (True)

I am still able to click on the X button and exit the application. I do have Access 2000 if that matters.

The only thing I did in Module1(where I put the function) was change the error msg since it was giving me an error)
Any ideas?
 
Sample

I've used Calvin's code for a year now, and it works like a charm. It is included in every db I have out there.
See attached sample.
I believe I took the security off - just let me know, OK?

Michael
 

Attachments

Sorry still not the solution

Hi,

I am talking about the Application window X button not the form. I already have the forms disable. Your code removes the buttons for the form only. If this could be used for the application window it would be great.
 
Thanks ghudson and mrabrams.

I have been looking for a Win32 version of this API call.

I also converted and tried it with Access 2000 and it works just fine.

RichM
 
It does

It does indeed. I had put in a separate module to begin with . Needed to include it in the module with the startup form. Michael(mrabrams) had the right solution. I just won't enabled the button at all in the database. Thanks for the solution. Will implement
 
jmriddic wrote
<<
I had put in a separate module to begin with . Needed to include it in the module with the startup form.
>>

No, that's not true.

You can put the API declarations and Functions in a separate module.

Just remember to call the functions from the Open and Close events of your main form.

RichM
 

Users who are viewing this thread

Back
Top Bottom