Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2004-08-15
2
Medium Priority
?
177 Views
Last Modified: 2010-04-23
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
0
Comment
Question by:fowlow
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 700 total points
ID: 11807242
To prevent closure of the form, set the Cancel property of the e variable passed into the Closing event to True like 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
                    e.Cancel = True ' <--------  This causes the Closing event to be cancelled                    
                End If
            End If
        End If
        mWB = Nothing
        End
    End Sub
0
 

Author Comment

by:fowlow
ID: 11809959
Exactly the answer I needed.
Thank-you very much.
Tim
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question