Solved

[vb2010] break out of if statement

Posted on 2013-10-25
5
252 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
ID: 39599903
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
ID: 39599909
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
ID: 39599931
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
ID: 39599935
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
ID: 39610997
I have supplied reason in comment. Thanks
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

810 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