Disable close button of forms and application (1 Viewer)

Alvin85

Registered User.
Local time
Today, 20:20
Joined
Jul 7, 2018
Messages
17
Hi,

I want the user to click on the button - btnClose to close the application instead of the user closing the forms or application via the X button.
Is there any ways to disable the close button on the top right hand of the forms and on the top right hand of the access application?
 

isladogs

MVP / VIP
Local time
Today, 12:20
Joined
Jan 14, 2017
Messages
18,186
You can easily disable it from the property sheet
Close button = No - its still visible but does nothing

Better still in my view is to set Control Box = No which hides the close button as well minimize and maximize

OR change the form border style to NONE
 

Alvin85

Registered User.
Local time
Today, 20:20
Joined
Jul 7, 2018
Messages
17
Hi ridders,

Thanks.
Is there a way to disable the x button for the access application too?
 

MarkK

bit cruncher
Local time
Today, 05:20
Joined
Mar 17, 2004
Messages
8,178
The Access window won't close if you cancel the unload event of an open form. One trick then, is maintain a hidden form, always open but never visible, and cancel its unload event.
hth
Mark
 

MarkK

bit cruncher
Local time
Today, 05:20
Joined
Mar 17, 2004
Messages
8,178
Code sample...
Code:
Private Sub Form_Unload(Cancel As Integer)
    Cancel = True
End Sub
 

Alvin85

Registered User.
Local time
Today, 20:20
Joined
Jul 7, 2018
Messages
17
Code sample...
Code:
Private Sub Form_Unload(Cancel As Integer)
    Cancel = True
End Sub

Hi Markk,

I tried cancel = true. But I am no longer able to close the form and go to the next form. May i know where did I did wrong and anyway to go about it?

And may i ask what do u mean by having a hidden form open at all times? Do you have a sample?
 

isladogs

MVP / VIP
Local time
Today, 12:20
Joined
Jan 14, 2017
Messages
18,186
Hi Markk,

I tried cancel = true. But I am no longer able to close the form and go to the next form. May i know where did I did wrong and anyway to go about it?

And may i ask what do u mean by having a hidden form open at all times? Do you have a sample?

At startup open a form but set visible =false. Leave that form open in the 'background'. This idea is commonly used to create a persistent connection to a backend database

If you use the code Mark supplied, users will only be able to quit using an Exit button with appropriate code
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 20:20
Joined
May 7, 2009
Messages
19,175
this will Enable/Disable the Access Application Close (X) button.
Code:
Option Compare Database
Option Explicit
#If VBA7 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

now, to disable the (X) on your form, add a form-wide boolean variable.
also you must add a Button that will actually close the form.
on the form:
Code:
Option Compare Database
Option Explicit 

Dim bolClosing As Boolean

Private Sub Form_Load()
'* initially set to False so we will test in Unload Event of the Form
bolClosing=False
End Sub
on the Click Event of your CloseButton:
Code:
Private Sub CloseButton_Click()
bolClosing = True
DoCmd.Close acForm, Me.Name, acSaveNo
End Sub
you handle the Unload Event of the Form:
Code:
Private Sub Form_Unload(Cancel As Integer)
Cancel = (bolClosing=False)
End Sub
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 08:20
Joined
Feb 19, 2002
Messages
42,981
What problem is being caused by allowing access to the form and application close buttons? We may as well be working on solving the correct problem.
 

DatabaseTash

Registered User.
Local time
Today, 23:20
Joined
Jul 23, 2018
Messages
149
@arnelgp
Can you tell me what is meant by the form-wide boolean variable? Where is this code placed? Does it go on the form load event of each individual form?
now, to disable the (X) on your form, add a form-wide boolean variable.
also you must add a Button that will actually close the form.
on the form:
Code:
Option Compare Database
Option Explicit

Dim bolClosing As Boolean

Private Sub Form_Load()
'* initially set to False so we will test in Unload Event of the Form
bolClosing=False
End Sub
[/QUOTE]
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 07:20
Joined
Feb 28, 2001
Messages
27,001
There are two problems here. First, there is stopping the app from closing. Second, you want each form to only close on use of the correct button. To do the second part first ('cause it is easier),

In the form's class module, the area before the first subroutine or function in VBA is called the declaration area. It is that part of the class module before the first executable instruction in the module. Items that are declared PUBLIC in that area are visible in all routines within that one form. SO... declare a Boolean public variable there that is your OKtoClose flag. In the form's Form_Load code, set that flag FALSE. (Or you could do it in the form's Form_Open routine. Tomato/Tomahto.)

Now in the form's Form_Unload routine, test the flag. If it is FALSE, return Cancel = -1. If it is TRUE, do nothing.

In the close button's CloseBtn_Click routine, set the flag TRUE and then do a DoCmd.Close acForm, Me.Name, acSaveNo (which is exactly what ArnelGP told you to do.) So the logic is, if someone tries to close the form by any method OTHER THAN using the command button, the "Cancel" test in the Form_Unload will block the action. But if you use the button, you turn off the flag and the "Cancel" test does nothing, so the form closes.

Now as to the other problem, protecting the app...

You need a "global" button, which usually goes on something like a switchboard or a master control form. This one uses the same kind of logic used to keep forms open, but this one has a QUIT button that will do an Application.Quit for you. The Form_Unload on the switchboard will block the app from premature closing.
 

June7

AWF VIP
Local time
Today, 04:20
Joined
Mar 9, 2014
Messages
5,425
Then there is the challenge of disabling ribbon File > Exit. Seems the Form_Unload is not trapping this action. I swear I had this covered with a customized ribbon in Access 2007 but since Access 2010 installed, doesn't seem to work.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 20:20
Joined
May 7, 2009
Messages
19,175
let this sample do the explaining.
 

Attachments

  • quitApp.zip
    30.9 KB · Views: 371

DatabaseTash

Registered User.
Local time
Today, 23:20
Joined
Jul 23, 2018
Messages
149
There are two problems here. First, there is stopping the app from closing. Second, you want each form to only close on use of the correct button. To do the second part first ('cause it is easier),

In the form's class module, the area before the first subroutine or function in VBA is called the declaration area. It is that part of the class module before the first executable instruction in the module. Items that are declared PUBLIC in that area are visible in all routines within that one form. SO... declare a Boolean public variable there that is your OKtoClose flag. In the form's Form_Load code, set that flag FALSE. (Or you could do it in the form's Form_Open routine. Tomato/Tomahto.)

Now in the form's Form_Unload routine, test the flag. If it is FALSE, return Cancel = -1. If it is TRUE, do nothing.

In the close button's CloseBtn_Click routine, set the flag TRUE and then do a DoCmd.Close acForm, Me.Name, acSaveNo (which is exactly what ArnelGP told you to do.) So the logic is, if someone tries to close the form by any method OTHER THAN using the command button, the "Cancel" test in the Form_Unload will block the action. But if you use the button, you turn off the flag and the "Cancel" test does nothing, so the form closes.

Now as to the other problem, protecting the app...

You need a "global" button, which usually goes on something like a switchboard or a master control form. This one uses the same kind of logic used to keep forms open, but this one has a QUIT button that will do an Application.Quit for you. The Form_Unload on the switchboard will block the app from premature closing.
Wow thank you so much Doc Man. That has definitely given me a clearer understanding of so many aspects. Much appreciated!
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 20:20
Joined
May 7, 2009
Messages
19,175
close the db if open.
while Holding the Shift key, open the db.
 

Users who are viewing this thread

Top Bottom