Home > How-To Library > Forms
How to find the user is trying to close the application using task manager or by pressing the X in Title bar, etc. How to restrict/refuse that
**************************************************************** * © 2007 CodeItBetter http://www.codeitbetter.com * * This notice MUST stay intact for legal use * **************************************************************** 'When the form is about to be unloaded, the form object receives a QueryUnload 'event. You can learn why a form is unloading by examining the UnloadMode 'parameter. I have created this code skeleton, which I reuse in my 'applications as necessary: Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer) Select Case UnloadMode Case vbFormControlMenu ' = 0 ' Form is being closed by user. Case vbFormCode ' = 1 ' Form is being closed by code. Case vbAppWindows ' = 2 ' The current Windows session is ending. Case vbAppTaskManager ' = 3 ' Task Manager is closing this application. Case vbFormMDIForm ' = 4 ' MDI parent is closing this form. Case vbFormOwner ' = 5 ' The owner form is closing. End Select End Sub 'You can refuse to unload by setting the Cancel parameter to True, as in the following code: Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer) ' Don't let the user close this form. Select Case UnloadMode Case vbFormControlMenu, vbAppTaskManager Cancel = True End Select End Sub
If you would like to submit your code here please us. Do not forget to mention your name. We are always thankful to each and everyone of you who submitted their code here.