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.
 
Don't quite see the wheel-reinventing thing.

Must be missing something.
 
Upvote 0

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
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".
I think you got them backwards. Cancel = True should be on vbNo and there is no need for the vbYes one because it is false by default. However, you also need to work with the form's BEFORE UPDATE event because it fires BEFORE the Unload event and it would cause a problem in that the form will update before it gets to the unload event. So, it will store the data you may not have wanted. About the only way to overcome that is to store the ID of the record in a property or global variable and in the form's Unload event you would run a delete query to delete the record if they chose to cancel the record due to the message shown in the form's Unload event. You can run the check and ask the question in the form's Before Update event instead and that should cause the form to not close if you cancel the update and don't undo the changes.
 
Upvote 0
Thanks Bob, I will change it to vbNo.

Actually, in this case, nothing would fire in the Before Update event because it is a listbox that is unbound; merely a container storing record ID's that need to be printed. The reason that I wanted the whole Cancel=vbNo was because info was not going to be saved to a table, and I wanted to warn the user of this before they closed the form and lost the printing queue.

Thanks for all your help guys!

Norie, as far as reinventing the wheel, your posts made it sound as if you wanted me to put an exit button on each form to make it uniform, whereas "reinventing the wheel" was meant to mean why would I do that when there is the Close button already provided. Hope this clears it up, if not, then I may be the one confused! :)
 
Upvote 0

Forum statistics

Threads
1,224,522
Messages
6,179,299
Members
452,904
Latest member
CodeMasterX

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