-
1
- #1
I've seen many postings in the past saying that you can't disable the Close button for Access's Application Window, and have seen various codes to perform housekeeping when a form is closed by some means other than the intended command button.
While browsing through another Forum, I came upon a post that dealt with this problem. However, this time they provided the solution. It comes from a tip (#19) on a website for Calvin Smith, and is as follows: (I had to separate the Const declarations and move them to the General Declarations area of the module, but it definitely works).
Calvin Smith
'**********************************************************
'************ Code provided by CodeDisk II ****************
'**********************************************************
'We need the following API declarations first
Private Declare Function apiEnableMenuItem Lib "user32" Alias _
"EnableMenuItem" (ByVal hMenu As Long, ByVal wIDEnableMenuItem As Long, _
ByVal wEnable As Long) As Long
Private Declare Function apiGetSystemMenu Lib "user32" Alias _
"GetSystemMenu" (ByVal hWnd As Long, ByVal flag As Long) _
As Long
Function EnableDisableControlBox(bEnable As Boolean, _
Optional ByVal lhWndTarget As Long = 0) As Long
On Error GoTo ErrorHandling_Err
' ----------------------------------------------------------------------
' Purpose: Example of how to disable or enable the control box of
' a form, report, or the Access parent window.
'
' Accepts: bEnable, which determines whether to disable or enable
' the control box
'
' Also accepts lhWndTarget (which is optional), if you want
' to use a window handle other than the Access parent window.
'
' Returns: N/A
'
' Example usage: lRetVal = EnableDisableControlBox(True) to enable -OR-
' lRetVal = EnableDisableControlBox(False) to disable
'
' NOTE: If no hWnd is passed in for a specific form, then the code
' assumes you want to enable/disable the Access parent window
' ----------------------------------------------------------------------
Const MF_BYCOMMAND = & Const MF_DISABLED = & Const MF_ENABLED = & Const MF_GRAYED = & Const SC_CLOSE = & 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
EnableDisableControlBox = lReturnVal
ErrorHandling_Err:
If Err Then
'Trap your error(s) here, if any!
End If
End Function
PaulF
While browsing through another Forum, I came upon a post that dealt with this problem. However, this time they provided the solution. It comes from a tip (#19) on a website for Calvin Smith, and is as follows: (I had to separate the Const declarations and move them to the General Declarations area of the module, but it definitely works).
Calvin Smith
'**********************************************************
'************ Code provided by CodeDisk II ****************
'**********************************************************
'We need the following API declarations first
Private Declare Function apiEnableMenuItem Lib "user32" Alias _
"EnableMenuItem" (ByVal hMenu As Long, ByVal wIDEnableMenuItem As Long, _
ByVal wEnable As Long) As Long
Private Declare Function apiGetSystemMenu Lib "user32" Alias _
"GetSystemMenu" (ByVal hWnd As Long, ByVal flag As Long) _
As Long
Function EnableDisableControlBox(bEnable As Boolean, _
Optional ByVal lhWndTarget As Long = 0) As Long
On Error GoTo ErrorHandling_Err
' ----------------------------------------------------------------------
' Purpose: Example of how to disable or enable the control box of
' a form, report, or the Access parent window.
'
' Accepts: bEnable, which determines whether to disable or enable
' the control box
'
' Also accepts lhWndTarget (which is optional), if you want
' to use a window handle other than the Access parent window.
'
' Returns: N/A
'
' Example usage: lRetVal = EnableDisableControlBox(True) to enable -OR-
' lRetVal = EnableDisableControlBox(False) to disable
'
' NOTE: If no hWnd is passed in for a specific form, then the code
' assumes you want to enable/disable the Access parent window
' ----------------------------------------------------------------------
Const MF_BYCOMMAND = & Const MF_DISABLED = & Const MF_ENABLED = & Const MF_GRAYED = & Const SC_CLOSE = & 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
EnableDisableControlBox = lReturnVal
ErrorHandling_Err:
If Err Then
'Trap your error(s) here, if any!
End If
End Function
PaulF