# Visual Basic > Visual Basic FAQs >  Classic VB - How can I show a confirmation message when my form is closing?

## si_the_geek

Many programs show a confirmation message when you try to exit them (with most of them, only if you haven't saved your work), but how can you do the same?

Well there are two steps, showing the message, and (if apt) knowing whether the work has been saved.

*Showing a confirmation message*
To show the message, simply add a call to MsgBox in the _QueryUnload event of the form (if you have more than one form, this could be in each form, or just in the 'main' form).

The _QueryUnload event has a "Cancel" parameter which allows you to cancel the unloading, so setting it to True means that the form will stay open.  Note that while the _Unload event also has a "Cancel" parameter, it should not be used for this purpose, as that event can occur _after_ other forms in your project have already closed (if more than one is being closed, which is particulary likely for MDI projects).

The code you use could be like this:


```
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)

    'Show the message, with apt buttons
  If MsgBox("Are you sure you want to exit?", vbQuestion + vbYesNo) = vbNo Then
      'if "No" was pressed, cancel the unload
    Cancel = True    
    Exit Sub
  End If

End Sub
```

If you only want to show this message when the form is being closed for a particular reason (eg: only if the user pressed the X button), see the article How can I tell why my form is closing?


*Only showing the message if there is unsaved work*
In most cases you will probably only want to show a message if the user hasn't saved their changes.

Obviously what counts as 'unsaved work' depends on your program, but these guidelines are one way that you can tell if there is unsaved work or not (an alternative, which may be easier, is to check in _QueryUnload if the 'current' data is the same as the 'saved' data - but that is much more specific to each project).

The idea for this method is to have a variable which will keep track of whether there is something that needs to be saved.  This should be declared at the top of the form (in the 'General' - 'Declarations' section) so that it can be used throughout the form, eg:


```
Option Explicit  '(you may not have this line, see this article for why it is a good idea)
Private m_boo_UnsavedWork as Boolean
```

You could (and arguably should) set this variable to False in Form_Load, but Boolean variables are False by default.

Whenever the work is saved (and if apt, also when a 'Cancel' button is pressed), simply set the variable to False.  Assuming you have a Sub called Save which does the saving, the code could be like this:


```
Private Sub Save()
  ** your code to save here

    'remember that changes have been saved (so no message is shown when the form closes)
  m_boo_UnsavedWork = False
End Sub
```

Whenever a change is made, simply set the variable to True. Unfortunately this step takes a bit more time, as it needs to be repeated for each kind of change the user can make (which probably means most of the controls on your form).  For a textbox called txtFirstName, you would add code like this:


```
Private Sub txtFirstName_Change()
    'remember that these changes are not saved (so a message is shown when the form closes)
  m_boo_UnsavedWork = False
End Sub
```

Repeat this part for everything the user can do which counts as 'unsaved work' (which will usually be _Change and/or _Click events of your controls).


Finally, you can alter the _QueryUnload to check this variable, and only show the message if it is relevant:


```
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)

    'if there is unsaved work..
  If m_boo_UnsavedWork Then
      'Show the message, with apt buttons
    If MsgBox("Are you sure you want to exit?", vbQuestion + vbYesNo) = vbNo Then
        'if "No" was pressed, cancel the unload
      Cancel = True
      Exit Sub
    End If
  End If

End Sub
```

Or if you want to, you can show 3 buttons (Yes/No/Cancel) like Notepad does, eg:


```
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)

     'if there is unsaved work..
  If m_boo_UnsavedWork Then
       'Show the message, with apt buttons
    Select Case MsgBox("You have unsaved work, do you want to save your changes?", _
                       vbQuestion + vbYesNoCancel)
    Case vbYes        'the user wants to save before exiting
        ** call your save routine
        '(the form will automatically close afterwards)
    Case vbNo         'nothing to do - the user wants to exit without saving
    Case vbCancel     'cancel the unload
      Cancel = True    
      Exit Sub
    End If
  End If

End Sub
```

----------

