"X" button

peggypph

Registered User.
Local time
Tomorrow, 04:06
Joined
Jan 2, 2004
Messages
33
I know how to disable "X" button on each form that will close the form. But how to disable the "X" button at the most top line which display "Microsoft Access - ....". The user kill the application accidentally by pressing this "X" button.
 
If you had searched this forum then you should have stumbled upon this...
Code:
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
 
Or Make a Public Variable and a form

Call the form: frmHidden
In it's OnOpen event: put:

Code:
Me.Visible = False
DoCmd.OpenForm "YourMainForm"

and in it's Unload event put:

Code:
If Not booCloseAccess Then Cancel = True

In the module put:

Code:
Public booCloseAccess As Boolean

Try and close Access now...
 
I meant to say. Ensure that frmHidden is opened first from the StartUp menu due to Access's "First Opened, Last Closed" policy.
 
hidden form does not hide

Thank you for your advise. I did exactly what you advise. The "X" button is disabled, but my "hidden form" does not hide (not invisible), "my form" is opened in another windows. The hidden form is here, then I have to switch to "my form" by hand. Pls. help me!
 
Can someone help me understand what the booCloseAccess thing is doing. I added a defintion for booCloseAccess and then added the IF NOT then CANCEL statement as described above to my Main Menu form (it is the one that is automatically opened on startup) in the UNLOAD event. Yes, the X button on the MS Access bar is disabled, but I don't understand how the little bit of code did it.

Thanks.
 
Mae said:
Can someone help me understand what the booCloseAccess thing is doing. I added a defintion for booCloseAccess and then added the IF NOT then CANCEL statement as described above to my Main Menu form (it is the one that is automatically opened on startup) in the UNLOAD event. Yes, the X button on the MS Access bar is disabled, but I don't understand how the little bit of code did it.


The booCloseAccess is a Boolean variable meaning that it can only hold one of two values -- True or False. (On dimensioning it, it defaults to False.)

Basically it just sits there doing nothing while your database is in use. If someone tries to close the database then, because Access works on a first open, last closed basis, it tries to close the forms but the last form (frmHidden) has a proviso on its Unload() event that basically says "If the boolean is not true then cancel the unloading". Any other value (i.e. True) allows it to close.
 
Ahhhh, very clever. Thanks for the explaination; it does make sense now.

This forum has been a tremendous help to me. I have found numerous solutions to problems and different ways to accomplish things. Thanks to all that take the time to share their knowledge.

Mae
 
hi,

i'm not sure if this thread is dead, i can't get the frmhidden to be hidden at the begining. it is always visible.

How is this done? is there a setting i am missing?

Thanks

ian
 
Last edited:
Don't use the Tools -> Startup -> Startup Form option but make a macro, call it autoexec and make it:

OpenForm "frmHidden" and select the Hidden option

then pick OpenForm again and select your main form.
 
hey guys how do i create a new module to close the application from the "exit application button on the switchboard" and not the Microsoft Access Window!? Also how do i get an error message of "Are you sure you want to quit"?

can someone please explain without using too much technical terms as i'm a beginner in all of this. it's my first time making a proper application.

thanks guys you all have been a valuable help to me in all of this!
 

Users who are viewing this thread

Back
Top Bottom