Here's an example of tying the code to the specific form that you use to add/edit data. This example disables the ACCESS Application Close Button when it opens, and turns it back on when it is closed using the command button. If you attempt to close the form without using the command button the Unload event checks for the value of a module level variable and cancels the event. Be sure to change the name of the Sub tied to the command button to match the name of the command button in your form.
However, this can still be overridden by the Task Manager.
first add this to a Module
'Calvin Smith
'
'Tip # 19
'**********************************************************
'************ 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
Const MF_BYCOMMAND = &H0&
Const MF_DISABLED = &H2&
Const MF_ENABLED = &H0&
Const MF_GRAYED = &H1&
Const SC_CLOSE = &HF060&
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
' ----------------------------------------------------------------------
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
Next add this to the Form you want to disable the Close button:
Option Compare Database
Option Explicit
Dim blnOktoClose As Boolean
Private Sub cmdCloseAndEnable_Click()
blnOktoClose = True
Dim lRetVal As Variant
lRetVal = EnableDisableControlBox(True)
DoCmd.Close acForm, Me.Name
End Sub
Private Sub Form_Load()
Dim lRetVal As Variant
lRetVal = EnableDisableControlBox(False)
End Sub
Private Sub Form_Unload(Cancel As Integer)
If blnOktoClose = False Then
MsgBox "You Can Only Close This Form By Using The Command Button"
Cancel = True
End If
End Sub
PaulF