Solved

[vb2010] break out of if statement

Posted on 2013-10-25
5
256 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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
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…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

734 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