Protect Form from Accidental Unloading

By | 2019-08-22

When you want ot protect the form unload in accidently you can use this following code.

In this code, the action is When a form is prepared to unload The unload event was automatically raised on that event there is an parameter named “Cancel” which in the data type integer.

When the unload event is raised the “cancel” has the value as 0. If the value of “Cancel” is 0 the form is automatically unloaded. If we change the value to 1 the form was never end.

So I wrote the code as follows:

private sub form1_unload(Cancel as Integer)
   a = MsgBox "Do u want to close?",vbYesNo,"Alert Message"
   If a = vbYes Then
      Cancel = 0
   Else
      Cancel = 1
   End If
End Sub

Author: dwirch

Derek Wirch is a seasoned IT professional with an impressive career dating back to 1986. He brings a wealth of knowledge and hands-on experience that is invaluable to those embarking on their journey in the tech industry.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.