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 biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Is this valid for a VBscript IF Statement? The result is wrong. 2

Status
Not open for further replies.

Coder7

Programmer
Oct 29, 2002
224
US
If ((bHasChild = True) And (objRs.Fields("Comment_Type") = "R") And (objRs.Fields("RebuttalStatus") = "")) Then
strHTML = strHTML & &quot;<td>&quot; & strViewApproveDeny & &quot;</td>&quot;
ElseIf objRs.Fields(&quot;Comment_Type&quot;) = &quot;C&quot; Then
strHTML = strHTML & &quot;<td>&quot; & strDelEditComment & &quot;</td>&quot;
ElseIf objRs.Fields(&quot;Rebuttal_Status&quot;) > &quot;&quot; Then
strHTML = strHTML & &quot;<td>&quot; & strActionOnly & &quot;</td>&quot;
ElseIf objRs.Fields(&quot;Comment_Type&quot;) = &quot;A&quot; Then
strHTML = strHTML & &quot;<td>&quot; & strDelEditAction & &quot;</td>&quot;
Else
strHTML = strHTML & &quot;<td>&quot; & strDelEditRebuttal & &quot;</td>&quot;
End if

It's the 1st line in the statement that concerns me because I'm checking 'If A AND B AND C Then...'

Is it legit to check >1 condition in 1 if statement? If it is legit, is my syntax correct?

I couldn't find any information about this.

As always, thank you for your help!!














 
thats fine, the checks will return either true or false, its the whole idea of an if statement. Obviously if all 3 checks equate to true it will step into your if block.
 
Obviously, it is up to the indivdiual programmer, but the more complex my IF-THEN-ELSE's get, sometimes the answer lies in a a CASE statement to reduce confusion. However, you statement should return either a T-F response so it should work. <== Some people say they are afraid of heights. With me its table widths. ==>
 
I'm all for simplicity.

My first ? was can I check multiple conditions in one if 'clause' and the answers so far are 'yes'.

How would you check multiple conditions using Case, please? Again, I have been unable to find any examples.

How would you use Case to replace:

If (A and B and C) Then.......

Thanks!!
 
One point i forgot to mention is that you should first check the recordset fields for nulls before putting it into an if statement, since when you test it for a string value and the recordset fild valie is null it will blow.

if not isnull(objRs.Fields(&quot;Comment_Type&quot;)) then
strComment = objRs.Fields(&quot;Comment_Type&quot;)
else
strComment = &quot;&quot;
end if

if not isnull(objRs.Fields(&quot;RebuttalStatus&quot;)) then
strStatus = objRs.Fields(&quot;RebuttalStatus&quot;)
else
strStatus = &quot;&quot;
end if

if (bHasChild And strComment = &quot;R&quot; And strStatus=&quot;&quot;) then

...

end if
 
You wouldn't want to replace the above if stmt with a select case statement. Select case statements are often useful when you wqill have many possible cases you want to check, like so:
Code:
Dim myVar
myVar = &quot;a&quot;

Select Case myVar
   Case &quot;a&quot;
      Response.Write &quot;A!&quot;
   Case &quot;b&quot;
      Response.Write &quot;B!&quot;
   Case &quot;c&quot;
      Response.Write &quot;C!&quot;
   Case &quot;d&quot;
      Response.Write &quot;D!&quot;
   Case Else
      Response.Write &quot;No Matches!&quot;
End Select

The above would replace an if statement like this:
if myVar = &quot;a&quot; Then
      Response.Write &quot;A!&quot;
elseif myVar = &quot;b&quot; Then
      Response.Write &quot;B!&quot;
elseif myVar = &quot;c&quot; Then
      Response.Write &quot;C!&quot;
elseif myVar = &quot;d&quot; Then
      Response.Write &quot;D!&quot;
else
      Response.Write &quot;No Matches!&quot;
end if

Basically anytime you want to compare a single variable against multiple possible values to cause seperate pieces of code to process, it is better to use a case statement than an if..elseif..else..end if structure. If elseif structures are less easy to read and poorer programming practice than case statements.

As I said before, however, your above statement is fine as an if statement.
The classic structure for an if statement is:
If boolean expression Then
processing instructions
Else 'optional
processing instructions
End If

The boolean expression is any expression that returns a true or false value, this includes everything from evaluating a single variable:
dim myBool
myBool = true
If myBool Then
Response.Write &quot;True!&quot;
Else
Response.Write &quot;False!&quot;
End If

This boolean expression can also be as comlicated as you would like it, from a simple equivalency check:
if myInteger > 0 then
to a complex expression with AND or OR
if myInteger > 0 AND myInteger < 10 then

You can use parantheses to create sub expressions for the evaluation as well. Say we want to run a function only if myInteger is between 0 and 10 or it's equal to 20:
if (myInteger > 0 AND myInteger < 10) OR myInteger = 20 then

This expression will be evaluated similar to a mathematical expression:
expression: (myInteger > 0 AND myInteger < 10) OR myInteger = 20
value: myInteger = 8
evalute step 1: evaluate((myInteger > 0 AND myInteger < 10) OR myInteger = 20)
evaluate step 2:evaluate(myInteger > 0 AND myInteger < 10)
evaluate step 3:evaluate(myInteger > 0)
expression: (TRUE AND myInteger < 10) OR myInteger = 20
evaluate step 4:evaluate(TRUE AND myInteger < 10)
evaluate step 5:evaluate(myInteger < 10)
expression: (TRUE AND TRUE) OR myInteger = 20
evaluate step 6: evaluate (TRUE AND TRUE)
expression: TRUE OR myInteger = 20
evaluate step 7: evaluate(myInteger = 20)
expression TRUE OR FALSE
evaluate step 8: evalute(TRUE OR FALSE)
expression: TRUE
evaluate step 9: evaluate(TRUE)

since it has now reached a single boolean representation for the expression, it commences to process the first part of the if statement.


Unfortunatly, despite the fact that the basis of this past statement was an OR statement, the processing does not finish when one side is True (minimum requirement for an or statement to be true) but continues to process the right side of the equation as well. If the right side were quite complicated than you can see how splitting the or statement into two if statements could speed up the transaction. If it was split into a case statement it would also speed up the transaction, but would require you to write the same processing code twice (or place into a function).

As a sidenote, not all languages will process the full expression for an or statement, some languages only process enough to prove the statment must be true(or false in the case of an and) and then ignore the rest of the statement.
So in the case:
If (a = b AND b = c) OR (d = e AND e = f) then

if ASP finds a = b to be true and b = c to be true, it will still calculate the right side, despite the fact that there is no way for the overall statement to be false.

along the same vein:
If (a=b or b=c) and (d=e or e=f)

if ASP finds a <> b and b <> c it will continue to evaluate the right side as well, despite the fact that there is no way the overall statement can be true with the left side = false.

As a reference, the following table details the return results of AND and OR
If x AND y
x y x AND y
T T T
T F F
F T F
F F F

If x OR y
x y x OR y
T T T
T F T
F T T
F F F

It is also possible to use the NOT keyword to invert the value of a boolean

assume x = y
If x = y returns TRUE
If NOT x = y returns FALSE

There are also other comparisons such as XOR, NOR, NAND, but the above are the basics.

I hope this hasn't been confusiong, feel free to ask questions, I promise I won't type as much :)

-Tarwn --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---
For my next trick I will pull a hat out of a rabbit (if you think thats bad you should see how the pigeon feels...) :p
 
It's working great....thanks Tarwn!

Once I found out this is called the logical AND operator I found some documentation but your explanation was by far the best!!

Also, I asked for the CASE example because I didn't see how you'd use CASE to do what I needed to do and I can't use CASE in this instance so you cleared that up as well.

Have a great day!!!
 
you got yourself I nice (long) tutorial there Tarwn [lol]

worthy of a star it is A language that doesn't affect the way you think about programming is not worth knowing.
admin@onpntwebdesigns.com
 
Might be time to paint that chair before you rust to it onpnt ;)

--- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---
For my next trick I will pull a hat out of a rabbit (if you think thats bad you should see how the pigeon feels...) :p
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top