Secret of QueryUnload event
Determine when your app is being closed by Windows OS, or by Task Manager or by user.
This can be useful when you have to do cleaning up before your app exits.
Original Author: Chinese Guy
Code
'/////////////////////////////////////// 'Excerpt from MSDN documentation. '/////////////////////////////////////// 'vbFormControlMenu 0 : The user chose the Close command from the Control menu on the form. 'vbFormCode 1 : The Unload statement is invoked from code. 'vbAppWindows 2 : The current Microsoft Windows operating environment session is ending. 'vbAppTaskManager 3 : The Microsoft Windows Task Manager is closing the application. 'Remarks 'This event is typically used to make sure there are no unfinished tasks in the forms 'When an application closes, you can use either the QueryUnload or Unload Private Sub Command1_Click() Unload Me End Sub Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer) Select Case UnloadMode Case vbFormControlMenu 'The user clicked the Close button on the upper-right of form If MsgBox("UnloadMode : Close button. Wanna exit?", vbYesNo) = vbNo Then End If Case vbFormCode 'There's Unload statement in the code. If MsgBox("UnloadMode : Unload statement. Wanna exit?", vbYesNo) = vbNo Then End If Case vbAppWindows 'Windows OS session is ending. If MsgBox("UnloadMode : Windows OS. Wanna exit?", vbYesNo) = vbNo Then End If Case vbAppTaskManager 'Windows Task Manager is closing this app. If MsgBox("UnloadMode : Task Manager. Wanna exit?", vbYesNo) = vbNo Then End If End Select End Sub
'included in an application before that application closes.
'For example, if a user has not yet saved some new data in any form,
'your application can prompt the user to save the data.
'event procedure to set the Cancel property to True, stopping the closing process.
'However, the QueryUnload event occurs in all forms before any are unloaded,
'and the Unload event occurs as each form is unloaded.
Cancel = True
Cancel = True
Cancel = True
Cancel = True
Loading Comments ...
Comments
No comments have been added for this post.
You must be logged in to make a comment.