Connecting Tech Pros Worldwide Forums | Help | Site Map

Stop unloading the form

Newbie
 
Join Date: Oct 2006
Posts: 15
#1: Nov 13 '06
Hi All,

I have a form in which i have few mandatory fields. Wheneve the user tries to save, i check for these fields and alert them, if thet are empty. I have a CLOSE button in my form and whenever the user clicks the CLOSE button, i ask the user whether he wants to save before closing the form.

On the Form_Unload event, i check whether there are any new changes and if yes, i pompt the user for a save. But if they have not filled any of the mandatory fields and if they press the close button, it alerts asking for a value, but immediately after the alert it closes the form, as it is a FORM_UNLOAD event.

Is there any way to stop unloading the form, if i alert them about a mandatory field. Please note that since the code has grown huge shifting the MANDATORY check block from FORM_UNLOAD event is quite tough. So it would be of much help if you can suggest me on how to stop the FORM_UNLOAD???

Thanks a ton.

missinglinq's Avatar
Moderator
 
Join Date: Nov 2006
Location: Richmond, Virginia USA
Posts: 3,000
#2: Nov 13 '06

re: Stop unloading the form


Cancel = True in the appropriate spot in your code will will stop the unloading. The following, for example will temporarily halt the unloading, pop up a mewssagebox then continue unloading:

Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_Unload(Cancel As Integer)
  2.      Cancel = True
  3.      MsgBox "Can't Leave Yet!"
  4.      Cancel = False
  5. End Sub
Replace the messagebox above with your validation code.
Newbie
 
Join Date: Oct 2006
Posts: 15
#3: Nov 13 '06

re: Stop unloading the form


Thanks a lot dude. Works out exactly as required. Thanks for the quick response.
Reply