Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Does the first true statement satisify the If ElseIF

Status
Not open for further replies.

Pack10

Programmer
Feb 3, 2010
495
US
I am working with an If ElseIF condition.
Does the first true statement satifify the condition.

 
I hate If, ElseIf, ElseIf, Else, End If...

Consider using Select Case which is much easier to maintain and will ignore all Case statements after the first true one. You can use:
Code:
Select Case True
    Case Me.cboType = 1
     '
    Case Me.cboType = 2
     '
    Case Else
     '
End Select
or
Code:
Select Case Me.cboType
    Case 1
     '
    Case 2
     '
    Case Else
     '
End Select

Duane
Hook'D on Access
MS Access MVP
 
Problem is that I am testing a lot of conditions...
I had issues getting it to work thats why i ended up with the IF ElseIF
 

This should answer your question:
Code:
Dim i As Integer

i = 3

If i = 0 Then
    MsgBox i
ElseIf i = 3 Then
    [highlight]MsgBox i & " Ta da"[/highlight]
ElseIf i = 100 Then
    MsgBox i
ElseIf i = 3 Then
    [highlight #FF99FF]MsgBox i & " BINGO?"[/highlight]
Else
    MsgBox i
End If
You never get BINGO out of this code, yellow line is executed and is out of the IF business.

Have fun.

---- Andy
 
Yes in both cases. In vb a select or If then short circuits. So if the condition is met then it falls out of the construct.
You can verify very simply:
Public Sub verifyShortCircuit()
Dim x As Integer
x = 10
If x > 1 Then
MsgBox "greater than 1"
ElseIf x < 20 Then
MsgBox "less than 20"
End If
End Sub

although the second condition is true it short circuits and never is realized.
 
Pack10 said:
testing a lot of conditions
I can't think of a better reason to abandon If...ElseIf...Else...End If.

I find this type of syntax much easier to write and maintain.
Code:
Select Case True
    Case Me.cboType = 1 And Me.TxtDept = "Finance"
     '
    Case Me.cboType = 2 And Me.TxtDept <> "Finance"
     '
    Case Else
     '
End Select

Duane
Hook'D on Access
MS Access MVP
 
My vote for CASE is for this advantage:
Code:
If strCode ="B" or strCode = "C" or strCode = "Q" Then
    'do this
else if strcode = "A" or strCode = "Z" Then
   'do that
else
    'do the other thing
end if

Select Case strCode
Case "B","C","Q"
    'do this
Case "A","Z"
    'do that
Case Else
    'do the other thing
End Select
--jim
 



I, too, cast in with Select Case.

Skip,
[sub]
[glasses]Just traded in my old subtlety...
for a NUANCE![tongue][/sub]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top