Link to home
Start Free TrialLog in
Avatar of fowlow
fowlow

asked on

How do I prevent a form from closing when the close button (X) is selected?

Hi,
I want to be able to prevent an application from closing if the user did not save their data correctly. e.g. they missed completing a field properly.  I am using the  form_Closing sub but when I call exit sub the application still terminates.  How can I prevent this?

Private Sub myform_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
        If mbSaveNeeded Then
            If MsgBox("Save changes to current record?", MsgBoxStyle.Question.YesNo) = MsgBoxResult.Yes Then
                If Not DBSaveEntries() Then
                    Exit Sub THIS DOES NOT HELP BECAUSE THE FORM EXITS ANYWAYS
                End If
            End If
        End If
        mWB = Nothing
        End
    End Sub
ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of fowlow
fowlow

ASKER

Exactly the answer I needed.
Thank-you very much.
Tim