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

AND and THROUGH

Status
Not open for further replies.

MrMoocow

Programmer
May 19, 2001
380
US
Okay I have 9 variables ca1,ca2,ca3,cb,cb2,cb3,cc1,c2,cc3 now that through each holds either "Pone" or "ptwo" I have alot of if and then if and then if ...... I was wondering if ther was an and command example:

If a = b and b = c and c =a then
Msgbox "Moo"
End If

instead of
if a = b then
if .......


And is there any to set something like this up:
if ca1 through ca3 = "Pone"
.....

Thanks,

I'm aware that there is an command called Xand I think, but I 'm not sure.


Thanks for anyhelp.
 


If Im right then I think you have answered your own question:

If you have
a = 2
b = 2

IsThisTrueAB = a = b

IsThisTrueAB is in fact True (not 2 as you might have expected (it could only be true, false or null))

so you can use the expression

if a = b and d = e then
msgbox ""
endif

ie
if IsThisTrueAB and IsThisTrueDE then
msgbox ""
endif
Where both IsThisTrueAB and IsThisTrueDE must be true.

There is an operator precedence - but its irrelevant here i guess so use as many as you wish.

For the second part - just use a loop

For x = 1 To 9
If x = 9 Then
'do stuff
End If
If ConditionNotMet Then Exit For
x = x + 1
Next x

Stew
 
Be aware of one little caveat if you use the:

if a=b and c=d and (etc)

construct.

Unlike in C, VB will evaluate ALL the test conditions, whether they are true or false. This can sometimes lead to potentially unexpected behaviour.

 
John,

What Am I doing wrong?

Code:
    'Test/ Results.
    '? basAllEqual
    'True


    Dim Ca(9) As Variant
    Dim blnTemp As Boolean

    Ca(0) = 0

    For Idx = 1 To UBound(Ca)
        Ca(Idx) = "this is a String"
    Next Idx

    Ca(4) = "This is the other string"

    Select Case Ca(1)

        Case Ca(2), Ca(3), Ca(4), Ca(5), Ca(6), Ca(7), Ca(8), Ca(9)
            'Equal
            blnTemp = True
            MsgBox "All Equal", vbOKOnly, "All Equal"

        Case Else
            'NOT EQUAL
            blnTemp = False
            MsgBox "NOT All Equal", vbOKOnly, "All Equal"

    End Select

    basAllEqual = blnTemp

End Function
MichaelRed
mred@att.net

There is never time to do it right but there is always time to do it over
 
Thanks. You are right. I slipped, slipped, slipped a neuron there at 4:00 AM.
Suppose you had 9 variables that must be equal.
Code:
    Select Case CA1
    Case Is <> CA2, Is <> CA3, Is <> CA4, Is <> CA5, _
         Is <> CA6, Is <> CA7, Is <> CA8, Is <> CA9
        MsgBox &quot;not equal&quot;
    End Select
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top