Disabling the Access X “close” (1 Viewer)

Garcimat

Member
Local time
Today, 20:49
Joined
Jun 7, 2022
Messages
67
Hi guys
I am trying to not allow users to close the application using the Access X button.
I have an public variable called fcanIclose as Boolean

here is the code:

Private Sub Form_Unload(Cancel As Integer)
If fcanIclose = False Then
Cancel = True
MsgBox "You cannot close using tis button.You may exit the system from the main greeting screen", , "My Application"
End If
End Sub

up to here works fine would not close the Access, then I need a Close button on my form, code below:
…………….
Private Sub btn_Exit_Click()
On Error GoTo Err_quit

If MsgBox("Are you sure you wish to close the application ?", vbYesNo, "My Application") = vbYes Then
fcanIclose = False
DoCmd.Quit
End If

Exit_Quit:
Exit Sub
Err_quit:
MsgBox Err.Number & " " & Err.Description

Resume Exit_Quit
End Sub

The problem here is; when I click the form close button it give me the message:
"Are you sure you wish to close the application ?"
Answer = yes
That was supposed to close the access but does not close, instead give me the message from the form unload event
"You cannot close using tis button.You may exit the system from the main greeting screen"
Then when click ok the access is closed
I can’t understand why…..
thanks in advance
 

theDBguy

I’m here to help
Staff member
Local time
Today, 03:49
Joined
Oct 29, 2018
Messages
21,454
Try:

fcanIclose=True
DoCmd.Quit
 

KitaYama

Well-known member
Local time
Today, 19:49
Joined
Jan 6, 2022
Messages
1,540
Is this what you want to do?
 

Attachments

  • Database2.accdb
    404 KB · Views: 164

isladogs

MVP / VIP
Local time
Today, 11:49
Joined
Jan 14, 2017
Messages
18,209
The easiest way to do this is by removing the Access application interface completely.
For example, have a look at the RemoveTitleBarButtons example at
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 18:49
Joined
May 7, 2009
Messages
19,233
Is this what you want to do?
that will do.
but if the OP's "Close Button" Property is Set to Yes (in design view), he can do this also:
Code:
' Arnelgp
Private Sub Form_Close()
    DoCmd.Quit
End Sub

Private Sub Form_Unload(Cancel As Integer)
If fcanIclose = False Then
    Cancel = True
    MsgBox "You cannot close using tis button.You may exit the system from the main greeting screen", , "My Application"
End If
End Sub

Private Sub btn_Exit_Click()
On Error GoTo Err_quit

    If MsgBox("Are you sure you wish to close the application ?", vbYesNo, "My Application") = vbYes Then
        fcanIclose = True
        'arnelgp
        DoCmd.Close
    End If

Exit_Quit:
    Exit Sub
Err_quit:
    MsgBox Err.Number & " " & Err.Description

    Resume Exit_Quit
End Sub
 

Attachments

  • PreventClosing.accdb
    384 KB · Views: 160

KitaYama

Well-known member
Local time
Today, 19:49
Joined
Jan 6, 2022
Messages
1,540
but if the OP's "Close Button" Property is Set to Yes (in design view), he can do this also:
You may want to work a little bit more on this.
Right click Form1 and try to open it in design view. ;)
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 06:49
Joined
Feb 19, 2002
Messages
43,223
What problem are you actually trying to solve? Why do you feel the need to constrain the interface this way? If you have proper error checking in your BeforeUpdate events of all forms, there should never be a problem with exiting from any form. Tell us the problem you have and we will help you solve it without Draconian measures. Remember, the user has the ultimate control since he can force a reboot or use the task manager to close Access. So, you don't have the control you think you have.
 

Garcimat

Member
Local time
Today, 20:49
Joined
Jun 7, 2022
Messages
67
Well
I have a main form frm_welcome that will direct the user to other data entry forms (frm_PTI and frm_PTF) if they close using the access X button I get broken or incomplete records, I don’t want that and exiting in the main form looks more professional as well.
My problem now is: if I use this code on my data entry forms I can’t close and open the main form frm_welcome, I tried to by pass the code with another variable inside an if instruction but didn’t work. Now I am trying to swoop from Close() to visible=false
But my data entry forms when open they open as a new record always… now I am trying to solve this problem…. I love this, it is like Chess and BJJ

I just got a book to try to understand the error management….. definitely I will implement it on my coding.
thanks for all the help
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 06:49
Joined
Feb 19, 2002
Messages
43,223
if they close using the access X button I get broken or incomplete records
Then you need to rethink the process. If a power outage breaks the data, you have a bigger problem. Controlling the X will not solve this. So even if you actually get control over the close, you still have exactly the same problem. I suggest that you abandon this endeavor and remove the code from the forms so you can start clean. Then tell us exactly the logic of the process and how it gets broken if the form is closed using the X.

PS, data entry should not be a separate form from update.
 

Garcimat

Member
Local time
Today, 20:49
Joined
Jun 7, 2022
Messages
67
Thanks I am going on this way, easier to filter my records…. It was a good challenge.
 

Isaac

Lifelong Learner
Local time
Today, 03:49
Joined
Mar 14, 2017
Messages
8,774
@Garcimat

The easy way - without using hardly any code and certainly nothing complex or borrowed, is:

To enable hiding the Access Shell you must make some changes to the options of your Current Database.

Open your database and click on the Office Button the large round button in the top left of your window or in your case the File button.

Then click on the Access Options button located at the bottom of this window.

Then click on the Current Database button on left side of window should be the second one down.


The settings you need are:

Tabbed windows enabled (as opposed to overlapping windows)

Uncheck display document tabs.

Uncheck display navigation pane.

Display of status bar is optional.

And of course so is navagtion buttons on a form.

Your start up form Pop Up property has to be set to No

Now, in your startup form, execute this ONE LINE of code in the On_Load event of your start up form.

DoCmd.ShowToolbar "Ribbon", acToolbarNo

(credit glenn kruger)

I use this in most of my db's and it has the effect most people are looking for - the form appears to float, no Access shell visible.
Some people have pointed out to me that technically the Access shell could be considered "visible", but it is flush with the form, and thus, cannot be recognized as such, which to me is splitting hairs: It's invisible.

Since most of those options required in the above instructions are things you would do anyway, it works pretty well..
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 06:49
Joined
Feb 19, 2002
Messages
43,223
Although Issac's suggestion will keep the app from closing under normal circumstances, it still doesn't protect you from unnatural closes by the user. Far better to fix your validation to prevent invalid records from saving.
 

Isaac

Lifelong Learner
Local time
Today, 03:49
Joined
Mar 14, 2017
Messages
8,774
Agree Pat, but I wasn't suggesting it as a solution for the problem of bad records - just as a solution for the aesthetic value that some Access developers yearn for, i.e., "Make my app look like a professional software instead of an Access application" etc.

Definitely the issue of invalid records is totally different and is a form design issue.
 

Garcimat

Member
Local time
Today, 20:49
Joined
Jun 7, 2022
Messages
67
@Garcimat

The easy way - without using hardly any code and certainly nothing complex or borrowed, is:

To enable hiding the Access Shell you must make some changes to the options of your Current Database.

Open your database and click on the Office Button the large round button in the top left of your window or in your case the File button.

Then click on the Access Options button located at the bottom of this window.

Then click on the Current Database button on left side of window should be the second one down.


The settings you need are:

Tabbed windows enabled (as opposed to overlapping windows)

Uncheck display document tabs.

Uncheck display navigation pane.

Display of status bar is optional.

And of course so is navagtion buttons on a form.

Your start up form Pop Up property has to be set to No

Now, in your startup form, execute this ONE LINE of code in the On_Load event of your start up form.

DoCmd.ShowToolbar "Ribbon", acToolbarNo

(credit glenn kruger)

I use this in most of my db's and it has the effect most people are looking for - the form appears to float, no Access shell visible.
Some people have pointed out to me that technically the Access shell could be considered "visible", but it is flush with the form, and thus, cannot be recognized as such, which to me is splitting hairs: It's invisible.

Since most of those options required in the above instructions are things you would do anyway, it works pretty well..
Thanks I will try that
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 06:49
Joined
Feb 19, 2002
Messages
43,223
Looks like he's going to use your suggestion rather than fixing the app. Oh well. I tried.
 

Users who are viewing this thread

Top Bottom