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

Customise the control X button

Status
Not open for further replies.

ahau

Programmer
Apr 19, 2006
51
AU
Hi,

I have a question on the Control Box X Button which is right on the top right hand corner of Access database. I'd like to customise it in a way that if user by accident, click it, it'd ask the user if they want to quit or not. If yes, then close the database or else, leave everything as they are.

I have successfully performed enabling and disabling the X button. However, i'd still like to be able to let it enabled while having the feature as i described above.

I was wondering if anyone might be able to give me some advice.

Thank you in advance
 
Add the following code to the form's Unload event:
Code:
Private Sub Form_Unload(Cancel As Integer)
    If MsgBox("Are you sure you wish to close this screen?", vbYesNo + vbQuestion) = vbNo Then
        Cancel = True
    End If
End Sub
 
Ooops, misread your post, you were talking about the X for Access. I don't know how to trap that off the top of my head.
 
OK, think I found a way.

Make a form with similar code as my first post, just change the message accordingly ("Are you sure you wish to quit?")

Make a macro called "Autoexec". Have it load the above form, but make Window Mode = Hidden.

When Access closes, it will start to unload the form. If you cancel the Unload event, Access will stay loaded.
 
Thank you for your reply, JoeAtWork

Yes, i meant the Control X button on the very top right hand corner that allows us to close the whole database, not the form but the actuall Access database.

I'd be very grateful if anyone might be able to help me on this.

Thank you in advance
 
Look at my 3rd post, I tested that method and it works (for catching either the database or Access closing).
 
Hi JoeAtWork,

Thank you for your help.

I know how to create a macro but unsure about what you mean by have it loaded above the form. Would you be kind to give me a bit more details of how you did it?

One question is would this work if the form is not loaded. By this i mean, what if this form you asked me to create is not loaded, would the access still prompt us when we click on the X button on the top?

Thank you in advance
 
The form needs to be loaded (so that when Access is trying to close the form it triggers the form's Unload event).

In the macro, choose "OpenForm" as the Action. In the Form Name, enter the name of the form. In Window Mode, choose Hidden. If you name the macro "Autoexec" it will automatically run when you open the database.

Or, if you already have a procedure that runs when the database opens (a splash screen or something), the same thing can be accomplished with the following code:
Code:
DoCmd.OpenForm "frmMyHiddenForm", , , , , acHidden
 
Thank you JoeAtWork for your help.

I have another question. remember you were suggesting to use a form and hide it. Well, i have a form that i use as a start-up. In that form i have a "close" button which will quit the database. I have some code in that close button that will confirm if the user wants to quit. So, after having the form_unload event in the same form, what happens after hitting the close button is i'd be asked twice, one by the close button and the other one by the form_unload event. Is there a way of getting it only once?

Here is my code
Private Sub CB_Exitbase_Click()
On Error GoTo Err_CB_Exitbase_Click
Dim msg, TITLE As String

reply = vbNo
msg = "Do you want to quit?"
TITLE = "Closing Confirmation"
reply = MsgBox(msg, vbYesNo, TITLE)

If reply = vbYes Then
Call CompactOnClose
DoCmd.Quit
End If
===========================================
Private Sub Form_Unload(Cancel As Integer)
If MsgBox("Are you sure you wish to close this screen?", vbYesNo + vbQuestion) = vbNo Then
Cancel = True
End If
End Sub

Thank you for your help in advance

 
How about a form level flag that indicates if the user pressed your button?


Private mbolExiting as Boolean '<--- NEW

Private Sub CB_Exitbase_Click()
On Error GoTo Err_CB_Exitbase_Click
Dim msg, TITLE As String

reply = vbNo
msg = "Do you want to quit?"
TITLE = "Closing Confirmation"
reply = MsgBox(msg, vbYesNo, TITLE)

If reply = vbYes Then
mbolExiting = True '<-- NEW
Call CompactOnClose
DoCmd.Quit
End If
===========================================
Private Sub Form_Unload(Cancel As Integer)
'If the flag isn't set it's not the button that's causing the unload
If mbolExiting = False Then '<-- NEW
If MsgBox("Are you sure you wish to close this screen?", vbYesNo + vbQuestion) = vbNo Then
Cancel = True
End If
End If
End Sub
 
JoeAtWork,

Thanks for a quick reply.

I have done as you instructed me to. It makes sense to me. so I tried it however, the flag isn't set as you said in your comment there. i have checked the mbolExiting's value in the exitbase close button and i'm sure the mbolExiting is set to true. Then, it went to form_unload and it went straight to the messagebox there. Somehow, it seems like the mbolExiting is still false regardless what it was set to before in the exitbase button.

But then, i can argue that we have set the mbolExiting as global variable there. but why's the mbolExiting still false?

I don't have a clue. Thanks for your help in advance
 
Hmm, I just tested it and have the same problem.

It has something to with that you are calling the Quit method, which would automatically start unloading all forms.

It's probably something like the variable is getting reset (while the app is shutting down) before it gets to the point of unloading the form. It's 1:15am here, my analytical skills are beginning to suffer.
 
Thanks JoeAtWork.

That's OK. I really appreciated your help and time you have spared solving my problem.

look forward to hearing from you again

Thanks
 
[!]Public[/!] mbolExiting As Boolean ' [!]in a standard code module[/!]

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
PHV, I tried that as well and had the same problem.

I think once the Quit method is invoked, all variables (including global) are reinitialized (if they get called again).
 
Hi JoeAtWork

Great to see you back.

The problem, i think, has been solved.

What i did was as follow

Private Sub CB_Exitbase_Click()
On Error GoTo Err_CB_Exitbase_Click
Application.Quit acQuitPrompt <--- New

And

Private Sub Form_Unload(Cancel As Integer)
If MsgBox("Are you sure you wish to close this screen?", vbYesNo + vbQuestion) = vbNo Then
Cancel = True
Else
Call CompactOnClose
End If
End Sub

And this works for me. It does what i want it to behave.

Thank you for your time and help, JoeAtWork and PHV
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top