Link to home
Start Free TrialLog in
Avatar of toylibrary
toylibrary

asked on

Access 2003+ - Disable or Remove Close "X" button from modal forms

I have read an old thread about how to disable the form close "X" button which involves running some code in a module.  THe thread was a few years old and  I couldnt find anything more recent.  Can someone please tell me if there is "new code" for this process or if the code I found (below) is still the best solution?
Cheers
Jan
Private Const GWL_EXSTYLE = (-20)
Private Const GWL_STYLE = (-16)
Private Const WS_MAXIMIZEBOX = &H10000
Private Const WS_MINIMIZEBOX = &H20000
Private Const WS_SYSMENU = &H80000
Private Const HWND_TOP = 0
Private Const SWP_NOMOVE = &H2
Private Const SWP_NOSIZE = &H1
Private Const SWP_FRAMECHANGED = &H20
Private Const SWP_DRAWFRAME = SWP_FRAMECHANGED
Private Declare Function SetWindowLong Lib "user32" _
Alias "SetWindowLongA" (ByVal hwnd As Long, _
ByVal nIndex As Long, ByVal dwNewLong As Long) _
As Long
Private Declare Function GetWindowLong Lib "user32" _
Alias "GetWindowLongA" (ByVal hwnd As Long, _
ByVal nIndex As Long) As Long
Private Declare Function SetWindowPos Lib "user32" _
(ByVal hwnd As Long, ByVal hWndInsertAfter As Long, _
ByVal x As Long, ByVal y As Long, ByVal cx As Long, _
ByVal cy As Long, ByVal wFlags As Long) As Long
Sub HideAccessCloseButton()
    Dim lngStyle As Long
    lngStyle = GetWindowLong(hWndAccessApp, GWL_STYLE)
    lngStyle = lngStyle And Not WS_SYSMENU
    Call SetWindowLong(hWndAccessApp, GWL_STYLE, lngStyle)
    Call SetWindowPos(hWndAccessApp, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE Or SWP_DRAWFRAME)
End Sub

Open in new window

Avatar of DatabaseMX (Joe Anderson - Former Microsoft Access MVP)
DatabaseMX (Joe Anderson - Former Microsoft Access MVP)
Flag of United States of America image

Avatar of toylibrary
toylibrary

ASKER

I think that solution stops access from being closed using the Big Red X.  I wanted to stop the x on the form itself (the one under the red one I guess).  So I didnt think this would work.
Have I understood the reference document correctly?
Hi toylibrary,

If you're talking about a form then just set the close button property to No

Otherwise look at http://support.microsoft.com/?kbid=245746
How to Disable the Close Button (X) on the Access Application Window



Good Luck!

Gary
In general, it's not really good UI design to disable X buttons.  On a form, you can always use to On Unload event - which can be Cancelled - to test for any special conditions wherein you don't want the user to close the form.  Users *expect* that button to work and will *always* be clicking it - since that is how almost every other window in a Windows app is closed.

mx
What would I check for specifically in the Unload Event VBA code - to check if the x button is pressed?
Would you be able to tell me the statement code for the "IF <statement> THEN" bit?
Cheers
Jan
PS Sorry for the delay in replying - I am a world away in Australia.
ok.
I need to know the reason you want to disable the X on the form?

But basically ... you Form On OnLoad event would look something like this:

Private Sub Form_UnLoad(Cancel As Integer)
   If <someCondition> =True Then   '  or False
      MsgBox "Some message to the user what they need to do before they can close"
      Cancel = True   ' this will cancel closing the form
   End If
End Sub

So ... what happens is ... they click the X ... which will trigger the On Unload event .. which check for whatever condition you want to test for ... like some fields are not filled in or what ever.
If the Condition is True (or False - whichever makes more sense to you) ... then the event is Cancelled ... and the form will not close.


Note that when you first open the On Unload event from the property sheet ... the event looks like this:

Private Sub Form_UnLoad(Cancel As Integer)

End Sub

I added an example of the code you would need.
mx

 

Okay - my misiunderstanding - is the unload event only for when users click the X?  Is it never triggered on any other form close action (like docmd.close)?  
If other close form action do not trigger the unload event then I understand not that I dont need to check for a specific value - just program the unload event.
I had asssumed I was looking for a keypress type value and didnt know what value to check!

If I understand this correctly now, then does the ON ENTER and ON EXIT have similar foibles?  I use these events for command buttons to requery lists after some action or other.  Do they also work no matter how the command is closed/finished?  Usually I use the command to open a form and then requery the list of records on exit to get an updated list.  Is this always going to work?

Thanks for your patience...
Jan
"(like docmd.close)?   "
It will trigger on any action that tries to close the form

Again ... exactly what condition are you testing for to allow closing?

I have to got out for a bit ... back later.

mx
The form is set it up as popup and modal - to stop users going to any other screen (tey must inish this entry first).
There are some actions on the form as command buttons - save/close, delete and cancel/close.
However, the form shouldnt close if there are validation errors.
The command buttons trap the invalid entries and stop the user from closing the form.
However, there are approximately 50 transactions a week which are invalid and skewing the statistics because the users are clicking the X close form button instead of using the command buttons.
By clicking the X button, it simply closes the form and bypasses the validation checks - thereby leaving an invalid record sitting there.

What I would like, is for the X button to be either:
1. disabled - but then they might click on the higher Access X Close button and the invalid record would still be created by default and the problem isnt  really solved.
OR
2. Trapped - if the X is pressed then perform the validation and cancel the X action.   This way, there is no way out  (if I can replicate the same condition trapping for the access button too - but only temporarily for this screen - but this is a second problem).

I have attached a screen print of the screen - the backend VBA is very long and compllicated so I dont want to change the command button actions which already work if I can avoid it.

The record is created by the previous screen with empty fields (due to the complexity of the system I couldnt use a standard type entry screen which would create the record when data is entered).  Therefore I have to get the users to enter valid data or delete it.


close-dilemma.jpg
By the way, is the UNLOAD event used triggered by the frm closing by any means (not just the X)?
ASKER CERTIFIED SOLUTION
Avatar of DatabaseMX (Joe Anderson - Former Microsoft Access MVP)
DatabaseMX (Joe Anderson - Former Microsoft Access MVP)
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial