Stof Ef/Else Routine

The Title of this question was supposed to read: "Stop If/Else Statements"
I am new to VB and this is my problem:
I have nested If/Else statements that are verifying data that has been entered in text boxes.  If an error is found by the If/Else statements, I have it call a Procedure that pops up an error message with just an OK button.
Once the user clicks OK I want it to STOP running the If/Else nested statements and set focus on the main form again so the user can correct the error.  How do I go about making it STOP.
ahelgesonAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

DrDelphiCommented:
Here's a quick, simple example:

Dim i As Integer
For i = 0 To 10
 Label1.Caption = i
 If i = 5 Then
   MsgBox ("let's get out here")
   Exit For
 Else
 End If
 
Next
0
VbmasterCommented:
There is no 'Exit If' statement, what you can do is use a jump to jump to the last line of the sub where the If..EndIf block is, like this..

Sub Test()

   If (1 = 1) Then
     If (2 = 2) Then
       If (3 = 3) Then
       End If
       If Error Then
         Call MsgBox("Error")
         Goto errorencountered
       End if
       If (4 = 4) Then
       End If
     End If
   End If

:errorencountered
   'no code here

End Sub
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mark2150Commented:
You really shouldn't code nested IF/ELSE/IF like VBMaster has shown in his example:

   If (X = 1) Then
     If (X = 2) Then
       If (X = 3) Then
       End If
       If Error Then
         Call MsgBox("Error")
         Goto errorencountered
       End if
       If (X = 4) Then
       End If
     End If
   End If

It is much clearer to use the SELECT CASE statement:

SELECT CASE X
  CASE 1
   ...
  CASE 2
   ...
  CASE 3
   If Error then
     Call MsgBox("Error")
     Goto errorencountered
   End If
   ...
  CASE 4
   ...
  CASE ELSE
   ...
END SELECT

The logic is much clearer and faster. The existence of the CASE ELSE makes it clear how exceptions are handled. ELSE should not be used to more than one level and generally should be avoided whereever possible.  I've seen code:

IF condition THEN
  Var = "Something"
ELSE
  Var = "Something else"
END IF

This can be simplified:

Var = "Something"
If NOT condition THEN
  Var = "Something Else"
END IF

This is "safer" in that it assures that Var always receives a value. Additionally, it can often be simplified to the single line version of IF..THEN as in:

Var = "Something Else"
If condition THEN Var = "Something"

This tends to generate tighter code as the compiler "knows" that there are no additional clauses on the conditional. Notice that the NOT has been eliminated by resequencing the assignments.

Coding efficiency generally dictates that "less is more". Fewer statements, simpler statements, are coded, debugged, executed, and documented faster and since time = money, this reduces development cost. While the savings on this one item are small, when scaled over an entire project the cost savings can be significant esp. when the costs of modifications are considered.

The large nested IF's are tough to clearly understand and see the logic flow to. This raises maintenance costs as the coding/debugging time increases with the complexity of the nesting. The SELECT CASE is clearer in all cases and should be used.

M
0
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

ahelgesonAuthor Commented:
The solution that worked for me was provided by VBMaster.  VBMaster, please propose an answer so I can give you the points.

The Solution proposed by DrDelphi's was good, but I wasn't using For statements. Sorry.

Andy
Thanks
0
ahelgesonAuthor Commented:
Nevermind, I figured it out.

Thanks
0
mark2150Commented:
Again VBMasters solution works, but it is not the "preferred" form. Try to use SELECT CASE instead of large nested IF/ELSE's

M
0
VbmasterCommented:
mark2150: you are aware that the code I submitted is probably NOT anything like the actual code that ahelgeson's code looks like? It was just a way to show how the goto statement works. I guess I should have wrote 'expression' instead of x=1, x=2 etc but I thought the purpose was kinda obvious.
0
mark2150Commented:
Agreed. I'm just anti the IF/ELSEIF type of construct as being hard to document, hard to analyze and hard to maintain vs the SELECT CASE option.

Right now at work I'm having to untangle a piece of undocumented legacy code that has IF/ELSEIF's nested to 7, 8, or even 10 levels. I've got IF THEN ELSE statements with *nothing* between the THEN and ELSE! Naturally it's not *indented* to show the relationships. (sigh) A real pile of *crap*. A few strategically placed SELECT CASE statements would eliminate 90% of the code and greatly simplify debugging that abortion.

One tip that I picked up here that's worth repeating is that you can use SELECT CASE to operate on completly independent clauses. Normally people code:

SELECT CASE X
  CASE 1
  ...
  CASE 2
....
END SELECT

In this form all of the clauses are based on X. Now if you needed to check if, say X=1, Y=3, or Z="Snarf" then you can code it as:

SELECT CASE TRUE
  CASE (X=1)
  ...
  CASE (Y=3)
  ...
  CASE (Z="Snarf")
....
END SELECT

The parens are to convert the contents into a logical result (True/False).

M



M
0
ahelgesonAuthor Commented:
I already have an answer that worked.  I am in an Intro to VB class.  The GoTo answer got my program to work.  My teacher helped explain more details.  Thanks for everyone's help.

Please close this issue.

Andy
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.