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

Need to Disable Close Button 5

Status
Not open for further replies.

gmillerinc

IS-IT--Management
Aug 27, 2003
28
US
I would like to disable the CLOSE ("x") button on a form. I have attempted to do this by going into the Form's properties and choolse Close Button = No in the format tab.

When the form is not fully maximized this works fine. But when it is maximized, the Close Button is active.

Any way to make it disable even when maximized?

I'm working with Access 97
 
Hi gmiller,

The only way to do this that I know of is to also set the form to Popup but then you may not want your form to act as a popup form. If it will not matter to you then problem solved. Other than that you could simply not allow the user to be able to maximize the form and this will also take care of your problem.

Hope this helps.

Regards,
gkprogrammer
 
Have you tried setting the ControlBox property to No in addition to a No for the CloseButton.

Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
This question has been asked a million and now one times here. So here you go.

1.) Set the Close button property of the form to No. You can also set the form's ControlBox Property to No also. By doing so it completely removes both the "x" button and the system menu from the form.

2.) In the form's class module in the declarations area, define a public Boolean variable such as OKToClose
Public OKToClose As Boolean

3.) In the forms open or load event set the value of OKToClose to false
Private Sub Form_Load()
OKToClose = Flase
End Sub

4.) In the code attached to the click event of your close button now needed to close the form set OKToClose to True.
Private Sub cmdClose_Click()
OKToClose = True
Docmd.Close
End Sub

5.) In the form's Unload Event add.
Private Sub Form_unload(Cancel As Integer)
Cancel = Not OKToClose
End Sub

The rest is up to you....

Life's a journey enjoy the ride...

jazzz
 
None of this works for me. The close button ("x") is only disabled when the form is not fully maximized. I need it to work when the form is fully maximized. Any thoughts/suggestions?
 
gmillerinc, we market software using the above technique and it DOES work regardless if the form is maximized or not I just tested it. The X will not be disabled or hidden it just won't work. Upon a user attempting to close our forms via the X they receive a message that the only way to exit our form is through the Exit button. Maybe there is something you are not explaining clearly here?

I will gladly send you an example if you like or send me what you need controlled and I will lock it for you. If you have Access to the Developers Handbook pg.483 describes exactly the same theory on a controlled closing.

Life's a journey enjoy the ride...

jazzz
 
Ok, how will I be able to send the database to you?

I created an entirely new database with a new form. Then I made the changes to the form format as follows:

Border Style = None
Control Box = No
Min Max Buttons = None
Close Button = No

Afterwards, I'm still faced with the same problem. I can't think of anything I'm not telling you. I do have Access 97, doubt that matters though.

Can you help?
 
Hi gmiller/jazzz,

I can second that jazzz's code works great, sure you have the annoyance of the close button still appearing but well there are alot of little things that bug me about Access. Anyway good post jazzz you deserve a star.

Regards,
gkprogrammer
 
Sure jazz is obviously diligent in providing solutions. Thanks for the help.

I guess my trouble may be because I'm working with Access 97. My references are as follows:

Visual Basic for Applications
MS Access 8.0 Object Library
MS DAO 2.5/3.5 Compatibility Library (I tried 3.51 also)

Jazz (or anyone that wants to answer that is not experiencing the problem at hand) do you have other references set up?
 
I don't think that references are going to be your problem all you are doing is creating a global boolean variable and setting a value to it in different event procedures. Are you placing the code in the 4 different areas that jazzz outlined above? If you have what results are you seeing when you click on the close button(X) on the form, keeping in mind that the button will NOT be disabled, it just won't work when clicked.

Regards,
gkprogrammer
 
Yes I am placing the code in 4 different places. Also, if you notice in Jazz’s step 1, it sounds like just that is supposed to solve my problem but it isn’t.

But I am familiar with events and declaring variables. Everything is in the right spot.

What happens when I enter all the code? Well….nothing happens. It behaves as if nothing was there. If I click the close button, the form exits.

I’m at a loss. Obviously there is something your Access version has that 97 doesn’t…not sure right now. I’m still open to any help you may have. Thank you.
 
Hi Jazzz

The code you provided for our forms works great. I tried it a report module and it didn't work. I add the Cancel = Not OKToClose to the Deactivate event and Bam, my computer locked up (had to end task) after getting a compile error message highlighting the word "Cancel" - Variable not defined". Do I need to do something different when ti comes to reports???

Thanks,

Steve
 
Steve, first I have to ask why do you need a Exit button in a report? Then where are you placing the command button. Why you receive the error is the event doesn't support Cancel.

Life's a journey enjoy the ride...

jazzz
 
I meant to say a report in print preview only. Often times
my customers want to preview the report. It's so easy to accidently click the Access close button instead of the report close button (after maximizing he previewed report.)

Steve
 
Never mind folks. I guess the solution is pretty obvious.
I just keep my Report Menu open in the background with the OKToClose variable set to false while the preview print job
is opened. Thanks for the initial info.

Steve
 
Uh,

Hoping that someone (jazzz?) will be prompted to revisit this thread...

Also here to confirm the code works as advertised when using Access 2000.

I'm wanting to notify users that they have to close the database using my Exit button. However, when the following is executed, the message always appears, even when the exit button is chosen, which contains:

'SET VARIABLE TO INDICATE USER *IS* EXITING GRACEFULLY
pblnAllowClose = True

Private Sub Form_Unload(Cancel As Integer)

If pblnAllowClose = False Then
MsgBox "You cannot X out of the database, please use the CLOSE button", vbOKOnly, "CANNOT X OUT"
Cancel = True
End If

End Sub

Thanks in advance!
Bob
 
Add a debug.print above your close line
debug.print pblnAllowClose so we see what the variable is at that point.

Life's a journey enjoy the ride...

jazzz
 
jazzz,

Thanks for your reply AND your patience. This SHOULD be straight-forward...

The value is false
(I think it should be true at this point, as I brought up the form and selected the Exit button. My previous post indicated Close in error.)

I'll copy/paste all of what I think is the relevant code.

It's quitting time for me, so I won't be able to check back for a while, maybe even tomorrow.

Thanks again!
Bob

======================================================
'GLOBAL VARIABLES, OPTIONAL ROUTINES AND MISCELLANEOUS

'FOLLOWING IS TO REQUIRE CONTROLLED EXIT FROM DATABASE, NOT "X'ING OUT"
Public pblnAllowClose As Boolean
======================================================
Private Sub Form_Load()
On Error GoTo Err_Form_Load

'INITIALIZE VARIABLE THAT WILL REQUIRE USER TO EXIT GRACEFULLY
pblnAllowClose = False
====================================================
Private Sub Form_Unload(Cancel As Integer)

Debug.Print pblnAllowClose

MsgBox "Check Immediate window for value of pblnAllowClose", vbOKOnly, "CHECK PBLNALLOWCLOSE"

Cancel = Not pblnAllowClose

If pblnAllowClose = False Then
MsgBox "You cannot X out of the database, please use the EXIT button", vbOKOnly, "CANNOT X OUT"
Cancel = True
End If

End Sub
========================================================
Private Sub EXIT_Click()
On Error GoTo Err_EXIT_Click

If Me.Dirty Then
Me.Undo
End If

'SET VARIABLE TO INDICATE USER *IS* EXITING GRACEFULLY
pblnAllowClose = True

Application.Quit acExit

Exit_EXIT_Click:
Exit Sub

Err_EXIT_Click:
MsgBox Err.Description
Resume Exit_EXIT_Click

End Sub
 
Just one suggestion there are so many ways to tackle this but this should work.



Option Compare Database
Option Explicit
Public pblnAllowClose As Boolean

Private Sub cmdExit_Click()

pblnAllowClose = True
DoCmd.Close



End Sub

Private Sub Form_BeforeUpdate(Cancel As Integer)

If Me.Dirty Then
Me.Undo
End If

End Sub

Private Sub Form_Load()

pblnAllowClose = False

End Sub

Private Sub Form_Unload(Cancel As Integer)
Dim blnOKToClose As Boolean

blnOKToClose = True

If pblnAllowClose = False Then
MsgBox "You cannot X out of the database, please use the EXIT button", vbOKOnly, "CANNOT X OUT"
Cancel = True
blnOKToClose = False
End If

If blnOKToClose = True Then
DoCmd.Quit
End If

End Sub


Life's a journey enjoy the ride...

jazzz
 
jazzz,

Works like a charm!

I don't fully understand why mine didn't work, possibly the me.dirty - undo. I'll review that as an educational opportunity.

A star for a Star!
Bob
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top