Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 259
  • Last Modified:

[vb2010] break out of if statement

Hi
I am trying to stop execution of an if statement so it doesn't reach the else statement. So, in my case, if the if statement is true then do not display the else statement and just ok the message box, Is that possible. Thanks

    Dim sql As String
        sql = "SELECT * FROM Departments where Customer = '" & customer & "'"

        ' This is our DataAdapter. This executes our SQL Statement above against the Database
        ' we defined in the Connection String

        Dim adapter As New OleDbDataAdapter(sql, con1)
        ' Gets the records from the table and fills our adapter with those.
        Dim dt As New DataTable("Departments")
        adapter.Fill(dt)
        If dt.Rows.Count = 0 Then

            MessageBox.Show("No record")

        Else
            ' Assigns our DataSource on the DataGridView
            dgv1.DataSource = dt

            Dim sql1 As String
            sql1 = "SELECT * FROM Departments"
            Dim adapter1 As New OleDbDataAdapter(sql1, con1)
            Dim cmd1 As New OleDbCommand(sql1, con1)
            'Dim dt1 As New DataTable("Departments")
            con1.Open()
            Dim myreader As OleDbDataReader = cmd1.ExecuteReader
            myreader.Read()

            con1.Close()
        End If

Open in new window

0
peter-cooper
Asked:
peter-cooper
  • 3
  • 2
1 Solution
 
Paul_Harris_FusionCommented:
I don't really understand!  
If the IF part is executed,  the ELSE part is not.     This is standard IF THEN ELSE functionality and doesn't require any other work from you.

Have I misunderstood your requirement?
0
 
peter-cooperAuthor Commented:
Hi Paul
What is happening is that when the messagebox is ok'd the grid still loads after the box closes. What I am trying to do is when the box closes just return from where it was called. Thanks
0
 
Paul_Harris_FusionCommented:
Ah OK!

You can't exit out of an IF.

But you can exit out of a subroutine or a function  (with Exit Sub,  Exit Function or Return).
That will literally 'return from where it was called.
Just add the appropriate Exit statement after your call to MessageBox.Show.

Whether that meets your needs or not depends on the routine that contains your code.

e.g. If it is in the Load handler routine of a dialog,    you will need to do a bit more work to handle the failure to load gracefully.
0
 
peter-cooperAuthor Commented:
Solved it. All I did was to create a textbox and it's default visibility = false. I changed to true in the if statement and placed in the grid and removed the messasgebox.
0
 
peter-cooperAuthor Commented:
I have supplied reason in comment. Thanks
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now