Is there a way to create a Before Close type of event?

Montez659

Well-known Member
Joined
May 4, 2005
Messages
918
I have a form with a listbox that will ahve record ID's added to it, and that is all. This listbox is not bound and is simply a temporary holding of record ID's until the user hit's the ok button, and then I will have a sort of print function (haven't figured that one out completely yet, but that's a different post).

So, the problem comes in that if the user hits the X, the form will close adn lose the info in the listbox. I want to warn them and then ask if they want to proceed. If not, then the msgbox dissapears and the form stays open. Any way to do this, since there is not a Before Close event?

I guess I could create my own exit button and get rid of the X, but just thought I would throw this out there to see if there were any suggestions.
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Isn't it easiest to just remove the X?
 
Upvote 0
Thanks Denis, I will look into that.

Norie, Lol! I know, but sometimes uniformity is better than easier. When someone always uses the X to close a Window and then it disappears one day, they tend to freak out! I don't program for the computer literate - my main goal is to program for the rookie.
 
Upvote 0
So they wouldn't be able to deal with a nice clear button with the appropriate caption on it?

Perhaps even one with a tooltip.
 
Last edited:
Upvote 0
No, I'm not saying that at all, because that is what I have done in this situation. But when I create an interface, I shoot for uniformity. Maybe a little OCD, but it frustrates me when the same controls aren't available or function differently on different windows. And sometimes, uniformity is preferred over the easy route as well. Maybe it's all a matter of personal preference; who knows.
 
Upvote 0
Open the form in Design mode, and do View / Properties. On top of the pop-up window change the combo-box to "Form", which you'll find alphabetized among the F's. Choose the Event tab and look for the "On Unload" event. Click in the bar next to it, and you'll see a drop-down arrow button and an ellipsis button. Click the ellipsis, choose "Code Builder", and you'll now be in the VBE with the form's module code open, and the cursor inside a sub called Form_Unload:

Code:
Private Sub Form_Unload(Cancel As Integer)
    Select Case MsgBox("Are you SURE?", vbYesNo + vbDefaultButton2)
    Case vbYes
        Cancel = True
    Case vbNo
        Cancel = False
    End Select
End Sub

This code is executed when the form is about to close by whatever means. The idea is: SET Cancel to True to keep the form open, or to False to actually close the form. See the VBE help topic "Unload Event".
 
Upvote 0
Montez

How would a command button change?

As far as I know there is only one type.

If you do want ensure some uniformity have you considered creating 'template' forms?

They would have the 'default' controls you would want on them.

So when you want to create a new form you copy a template and work from there, adding all other elements required.

Forgot to mention, they would have all the basic code which would be included when they are copied.
 
Upvote 0
Jasmith,

I wish it was that simple, and I appreciate the help but the form still unloads.

Norie,

You are assuming that I have an Exit button on all of my forms, which I don't. I use the standard "X" (form close button) on all of my other forms that if someone needs to close out, they just press that. Why reinvent the wheel when Access already gives you the "X" there in the first place? The only reason to reinvent it would be because it doesn't work with my one form.

As far as the template is concerned, almost none of my forms function similarly, with different controls on them. This may confuse you as I was talking of uniformity, but that was simply in the context of the Exit, Min/Max functions of a form, not the form as a whole.
 
Upvote 0
Denis, Jasmith - you guys nailed it!

Since the Cancel parameter was By Integer, J, I substitued your True/False for a 0/-1 and it worked!

And Denis, sorry to you - you mentioned that earlier and I forgot to check it out. My apologies.
 
Upvote 0

Forum statistics

Threads
1,224,522
Messages
6,179,292
Members
452,902
Latest member
Knuddeluff

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top