Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Disable Access Application's Close Button 2

Status
Not open for further replies.

PaulF

Technical User
Jul 14, 2000
1,522
0
0
US
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
 
Hi Paul,

I guess I'm confused. In fact I know I am. I copied the code and moved the Const declarations the General Declarations area of the module.

In the form I tested it on I copied this to the Form_Open event procedure:

EnableDisableControlBox (False)

Where did I go wrong or what am I missing?

I also tried: EnableDisableControlBox (True) which didn't work either.

TIA

Dave
 
Rohdem! You've saved the day! I went to the link you suggested, applied it and it worked! I've been working on this all day! When I applied my &quot;exit&quot; macro to the form it did exactly what it was supposed to as if the user had clicked on my &quot;exit&quot; button or used my custom menu. THANK YOU, THANK YOU, THANK YOU. I had some network administrator try to tell me that I would have to write some complicated VB code to deactivate the close button on the Access app.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top