Solved

[vb2010] break out of if statement

Posted on 2013-10-25
5
255 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
[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
  • 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

756 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