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.
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
Me.Visible = False
DoCmd.OpenForm "YourMainForm"
If Not booCloseAccess Then Cancel = True
Public booCloseAccess As Boolean
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.