Isolating windows "X" to close a window

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Does anyone know the syntax to check if someone tries closing a form using
the Windows "X"? I want to isolate the "X" for error checking, without
affecting my Close, Quit buttons on the form.

Thanks!
Leslie
 
Leslie,

The way I approach this is to add some code behind the form.

1. In the Declarations section, I create a variable that is public to the
form

Dim AllowClose as Boolean

2. In the forms Open event, I set this value to False

Public Sub Form_Open

AllowClose = False

End Sub

3. In your Quit/Close button, add some code to change AllowClose to True

4. In your forms Unload event, set the cancel parameter to the Opposite of
AllowClose. If AllowClose = False, then cancel will be True. If AllowClose
= True, then Cancel will be false.

Public Sub Form_Unload(Cancel as integer)

Cancel = NOT AllowClose
If Cancel = True then msgbox "Use the Close button to close this form!"

Exit Sub
 
Dale,

Thank you for the help -- your scenario makes absolute sense. Ideally, I
would like to give the user an option to Quit/Exit, Save/Exit, or Cancel and
return to the form if they chose the windows "X". I haven't figured an easy
way to do this without having conflicts with the existing form buttons for
close, cancel, quit, etc. The problem I have is I have logic for the "X" at
the form level on the BeforeUpdate, and I have the buttons also associated to
the "BeforeUpdate", so my Form logic was stomping on the button logic.

If you have any ideas on how I can do the above, that'd be great. If not, I
think your logic makes perfect sense.

Cheers,
Leslie
 
Back
Top