Solved

[vb2010] break out of if statement

Posted on 2013-10-25
5
248 Views
Last Modified: 2013-10-30
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
Comment
Question by:peter-cooper
  • 3
  • 2
5 Comments
 
LVL 12

Expert Comment

by:Paul_Harris_Fusion
Comment Utility
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
 

Author Comment

by:peter-cooper
Comment Utility
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
 
LVL 12

Expert Comment

by:Paul_Harris_Fusion
Comment Utility
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
 

Accepted Solution

by:
peter-cooper earned 0 total points
Comment Utility
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
 

Author Closing Comment

by:peter-cooper
Comment Utility
I have supplied reason in comment. Thanks
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

763 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now