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

Warn before exiting msaccess 1

Status
Not open for further replies.

bailey11

Technical User
Jan 18, 2005
103
US
I have several people who accidentally click the msaccess program close button "X".


Is there a way to pop up a yes no warning before closing down access?

Thanks, Bailey11
 
How are ya bailey11 . . .

You can disable the application close button. They can still exit thru the file menu or Alt-F4.

In a new module in the modules window, copy/paste the following:
Code:
[blue]Option Compare Database
Option Explicit

Private Declare Function GetSystemMenu Lib "user32" (ByVal hWnd As Long, _
    ByVal bRevert As Long) As Long

Private Declare Function EnableMenuItem Lib "user32" (ByVal hMenu As _
    Long, ByVal wIDEnableItem As Long, ByVal wEnable As Long) As Long

Const MF_GRAYED = &H1&
Const MF_BYCOMMAND = &H0&
Const SC_CLOSE = &HF060&

'Disable the Close Button Option
Sub CloseButtonState(boolClose As Boolean)
    Dim hWnd As Long
    Dim wFlags As Long
    Dim hMenu As Long
    Dim result As Long
       
    hWnd = Application.hWndAccessApp
    hMenu = GetSystemMenu(hWnd, 0)
    If Not boolClose Then
        wFlags = MF_BYCOMMAND Or MF_GRAYED
    Else
        wFlags = MF_BYCOMMAND And Not MF_GRAYED
    End If
    
    result = EnableMenuItem(hMenu, SC_CLOSE, wFlags)
End Sub[/blue]
A good place to disable the button is in the [blue]On Open[/blue] event of a startup form:
Code:
[blue]   Call CloseButtonState(False)[/blue]
[blue]Your Thoughts? . . .[/blue]

Calvin.gif
See Ya! . . . . . .
 
In addition to what the AceMan describes, you could add a command button to your startup form (assuming you have one?) to allow the users to exit the application. The on click event for this button could be something along the lines of...
Code:
Dim intMsg As Integer
intMsg = MsgBox("Are you sure you wish to quit ?", vbYesNo + vbQuestion + vbDefaultButton2, "Confirm Exit")
If intMsg = vbYes Then
    Application.Quit
Else
    DoCmd.CancelEvent
End If
 
bailey11 . . .

My post is a bit out of context (you want to know about posting a message and control if the application closes) but it does prevent access closing if the user tries to use the application close button [blue]X[/blue]

The above allows closing methods like [blue]Spenney's[/blue] post . . .

Calvin.gif
See Ya! . . . . . .
 
I don't want to disable the "X" from closing the database, but I do want the user to have a chance to say yes or no if they click on it.
 
bailey11 . . .

Access is [blue]event driven[/blue] and there's [blue]no event to capture and/or cancel[/blue] activation of the application close button X! . . . sorry [sad]

Calvin.gif
See Ya! . . . . . .
 
Perhaps this ?
In the UnLoad event procedure of your never closed StartUp form:
If MsgBox("You really want to close the database ?", vbYesNo) <> vbYes Then
Cancel = True
End If

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top