Exiting closing function

I need to validate a form.  When I do it at closing, it will not let me exit sub and not close, the form just continues to close.  Where can I put the validation.  Thank you


Private Sub frmrecordsmanagment_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
        'update tblrecordmanagment with lngpatientid, boxnum, datearchive, description, date to be destoryed



        Dim deptnumber, todaysdate
        Dim full
        deptnumber = 1
        todaysdate = Now
        If CheckBox1.Checked = True Then
            full = 1
        Else
            full = 0
        End If

        If cancelrm = 0 Then

            'check to see if box is full

            Dim boxnumber
            If TextBox2.Text <> "" Then
                Dim cmdins As New SqlClient.SqlCommand("Select ysnboxfull from tblrecordsmanagment where boxnum = '" & TextBox2.Text & "' and ysnboxfull = 1", Connection1)
                Try
                    boxnumber = cmdins.ExecuteScalar.ToString

                Catch ex As Exception
                    MsgBox(ex.Message)
                End Try
            Else
                MsgBox("Cannot exit without entering box numnber.  Please Enter Box Number!")
                Exit Sub
            End If

            If boxnumber = True Then
                Dim ans As Int32
                ans = MsgBox("You have checked that this box is full on a prior record. Do you wish to add this record to this box number?, If so please click Yes!", MsgBoxStyle.YesNo)

                If ans = 6 Then

                Else
                    MsgBox("Please assign New Box Number!", MsgBoxStyle.OKCancel)
                    Exit Sub
                End If
            End If


 
running32Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

TimCotteeHead of Software ServicesCommented:
Hi running32,

e.cancel=True

Whereever you need to stop the closing from happening.

Tim Cottee
0
TimCotteeHead of Software ServicesCommented:
running32,

For example:

            Dim boxnumber
            If TextBox2.Text <> "" Then
                Dim cmdins As New SqlClient.SqlCommand("Select ysnboxfull from tblrecordsmanagment where boxnum = '" & TextBox2.Text & "' and ysnboxfull = 1", Connection1)
                Try
                    boxnumber = cmdins.ExecuteScalar.ToString

                Catch ex As Exception
                    MsgBox(ex.Message)
                End Try
            Else
                MsgBox("Cannot exit without entering box numnber.  Please Enter Box Number!")
                    e.Cancel = True
                Exit Sub
            End If

            If boxnumber = True Then
                Dim ans As Int32
                ans = MsgBox("You have checked that this box is full on a prior record. Do you wish to add this record to this box number?, If so please click Yes!", MsgBoxStyle.YesNo)

                If ans = 6 Then

                Else
                    MsgBox("Please assign New Box Number!", MsgBoxStyle.OKCancel)
                    e.Cancel = True
                    Exit Sub
                End If
            End If

Tim
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
running32Author Commented:
I forgot to say it was part of a tab. So it needs to pick up the tab name otherwise it continues to close.  Sorry
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.