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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can the Application Close Button ("X") Be Disabled 4

Status
Not open for further replies.

amacbean

Programmer
Nov 18, 2002
9
US
Need to stop the user exiting DB without getting some cleanup code executed. I have provided my own DB exit button to do this.

Can the Application close button be disabled or can i catch the close event and execute the required code?
 
to disable/enable the X button for the app paste this into a module and call disablex/restorex as appropriate

Code:
Option Compare Database
Option Explicit

Public Const SC_CLOSE = &HF060
Public Const MF_BYCOMMAND = &H0
Declare Function GetActiveWindow Lib "user32" () As Integer
Public Declare Function GetSystemMenu Lib "user32" _
                                      (ByVal hWnd As Long, ByVal bRevert As Long) As Long
Public Declare Function DeleteMenu Lib "user32" _
                                   (ByVal hMenu As Long, ByVal nPosition As Long, ByVal wFlags As Long) As Long
Declare Function DrawMenuBar Lib "user32" (ByVal hWnd As Long) As Long

Public Function DisableX()
Dim tmp As Long
    tmp = GetActiveWindow()
    Dim hMenu As Long
    hMenu = GetSystemMenu(tmp, 0&)
    If hMenu Then
        DeleteMenu hMenu, SC_CLOSE, MF_BYCOMMAND
        DrawMenuBar (tmp)
    End If
End Function


Public Function RestoreX()
Dim hMenu As Long
Dim tmp As Long
    tmp = GetActiveWindow()
    hMenu = GetSystemMenu(tmp, 1)
    DrawMenuBar (tmp)
End Function
[\code]
 
Huh. I'll want to play with this code, as it looks quite compact. I'll be happy to have something like this.

What I've always done is to have a form that's always open (though it can be invisible) with code in the unload/close event (whichever one has the cancel parameter) that cancels the event unless a flag has been set in an invisible text box on the form. That flag get set only by the cleanup code. If the flag isn't set, the application doesn't close. I also check for that flag in the same event of all my other forms, so that you can't even close a form except with the close button I've put on there.

Jeremy

==
Jeremy Wallace
AlphaBet City Dataworks
Access Databases for Non-Profit Organizations

Please post in the appropriate forum with a descriptive subject; code and SQL, if referenced; and expected results. See thread181-473997 for more pointers.
 
Hi,

In my opinion the simplest way to control the closure of a form or DB regardless of whether the X button is enabled or File Exit or File Close are available from the Menu Bar etc, etc is:

In the Form's Declaration section:
Dim booCanClose As Boolean

In the On Click event of the Form's Close button:
booCanClose = True
DoCmd.Close acForm, Me.Name

In the UnLoad event of your Form:
If booCanClose = False Then
MsgBox "Please use the Exit Button!"
Cancel = True
Else
'DoCmd.Quit 'Place this only on the form that you want to close the DB
End If

The only way to close the DB is to press Ctrl+Alt+Del or turn the PC off.

Bill
 
Thanks tony! (ireland) Thats exactly what I needed.
 
Have another star - always looking for a good API trick! [worm]

VBSlammer
redinvader3walking.gif

[sleeping]Unemployed in Houston, Texas
 
Hi amacbean,

Not trying to detract from tonyireland's excellent method above and not star hunting. I strongly suggest that you use a method similar to my suggestion in your Main/Switchboard or similar Form at least.

Alt-F4 will still close the DB from anywhere and so will File>> Exit from the menu in any Form or Report, unless of course you have a custom menu in every Form and Report. I, personally gave up on disabling the X years ago when I realised this.

Just thought I'd point it out.

Bill
 
I have to agree with bill, I personally handle the unload event of the main form since it fires no matter how the user decides to close the program.

But, disabling the close button is a good visual cue to the users that there is a different expectation for closing the program. Every little bit helps... [pipe]

VBSlammer
redinvader3walking.gif

[sleeping]Unemployed in Houston, Texas
 
I have actually been getting memory read exceptions using Tonys method so may change it after all, cheers guys.
 
amacbean,

Can you expand a little on that? I'd love to know a bit more about what's going wrong.

Jeremy

==
Jeremy Wallace
AlphaBet City Dataworks
Access Databases for Non-Profit Organizations

Please post in the appropriate forum with a descriptive subject; code and SQL, if referenced; and expected results. See thread181-473997 for more pointers.
 
I would bet it's due to the outdated API function he used that returns an integer rather than a long. Try replacing with this one:

Code:
  Public Declare Function GetActiveWindow Lib "user32" Alias "GetActiveWindow" () As Long


VBSlammer
redinvader3walking.gif

[sleeping]Unemployed in Houston, Texas
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top