Prevent Windows from being Closed from top Corner

Moore71

DEVELOPER
Local time
Today, 10:36
Joined
Jul 14, 2012
Messages
158
Hi house, can someone help me with code to prevent windows from being closed either accidentally or from the red X by the right-top-corner?

Thanks
Moore
 
In the form properties remove the close button option... ?
 
You can also cancel the unload event . . .
Code:
Private Sub Form_Unload(Cancel as Integer)
[COLOR="Green"]   'cancels the closing of the form[/COLOR]
   Cancel = True
End Sub
 
Thanks for your response. But I mean the Application windows not just a form inside the Application

Thank you, please try more
 
You could try this:

Code:
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)   
    ' Prevents use of the Close button   
    If CloseMode = vbFormControlMenu Then   
        MsgBox "Clicking the Close button does not work." & vbCrLf & vbCrLf & _   
            "Please Exit by Pressing ""Return"", ""Cancel"" or ""Exit"" buttons.", _   
            vbOKOnly + vbCritical, _   
            "                Close 'X' Inactive"   
        Cancel = True   
    End If   
End Sub

I found this code on this website: http://vba-programmer.com/Snippets/Code_Access/Disable_X_Exit_in_Userforms.html

I haven't tried it myself. I was just looking at how to do this yesterday.
 
this will prevent your access database from being closed from the "right X button", actually disabling it.

Code:
Option Compare Database
Option Explicit
#If Win64 Then
    
    Private Declare PtrSafe Function GetSystemMenu Lib "user32" _
            (ByVal hwnd As LongPtr, ByVal bRevert As Long) As LongPtr
    
    Private Declare PtrSafe Function EnableMenuItem Lib "user32" _
            (ByVal hMenu As LongPtr, ByVal wIDEnableItem As Long, ByVal wEnable As Long) As Long
#Else
    Private Declare Function GetSystemMenu Lib "user32" _
                                           (ByVal hwnd As Long, ByVal wRevert As Long) As Long
    Private Declare Function EnableMenuItem Lib "user32" _
                                            (ByVal hMenu As Long, ByVal wIDEnableItem As Long, ByVal wEnable As Long) As Long
#End If
Public Function AccessCloseButtonEnabled(pfEnabled As Boolean) As Boolean
' Comments: Control the Access close button.
'           Disabling it forces the user to exit within the application
' Params  : pfEnabled       TRUE enables the close button, FALSE disabled it
' Owner   : Copyright (c) 2008-2011 from FMS, Inc.
' Source  : Total Visual SourceBook
' Usage   : Permission granted to subscribers of the FMS Newsletter

    On Error Resume Next

    Const clngMF_ByCommand As Long = &H0&
    Const clngMF_Grayed As Long = &H1&
    Const clngSC_Close As Long = &HF060&
#If Win64 Then
    Dim lngWindow As LongPtr
    Dim lngMenu As LongPtr
#Else
    Dim lngWindow As Long
    Dim lngMenu As Long
#End If
    Dim lngFlags As Long

    lngWindow = Application.hWndAccessApp
    lngMenu = GetSystemMenu(lngWindow, 0)
    If pfEnabled Then
        lngFlags = clngMF_ByCommand And Not clngMF_Grayed
    Else
        lngFlags = clngMF_ByCommand Or clngMF_Grayed
    End If
    Call EnableMenuItem(lngMenu, clngSC_Close, lngFlags)
    AccessCloseButtonEnabled = True
End Function
 

Users who are viewing this thread

Back
Top Bottom