Link to home
Start Free TrialLog in
Avatar of Sethi
SethiFlag for India

asked on

Select Case problem

I have a few conditions in a problem. if the user enters AA and BB in two respective textboxes then the result will be CC. Similarly there are more than 50 prefixed combination that I hve in an application. So I want to use a select statement. However something like this is not working. I am not sure whether I am missing something or is it not possible in VB:

Select Case txtCombA And txtCombB
Case "AA" And "BB"
   txtCombC = "CC
Case "AB" And "AC"
   txtCombC = "AA"
End Select

I am getting type mismatch error in the first statement itself: Select Case txtCombA And txtCombB
Avatar of Jim Horn
Jim Horn
Flag of United States of America image

>Select Case txtCombA And txtCombB
Select Case can only evaluate one expression.  They way you have it typed is a logical expression, which only evaluates to True or False.
You're going to have to write a really large If...elseif...end if statement to cover all 50 prefixed combinations.

If txtCombA = "AA" AND txtCombB = "BB" then
    txtCombC = "CC"
ElseIf  txtCombA = "AB" AND txtCombB = "AC" then
    txtCombC = "AA"
'etc. etc.
End If
ASKER CERTIFIED SOLUTION
Avatar of fds_fatboy
fds_fatboy

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Sethi

ASKER

That's what I wanted to avoid :-)

No solution with Select Case?
Avatar of fds_fatboy
fds_fatboy

Or even
Select Case True
Case  txtCombA = "AA" And  txtCombB = "BB"
   txtCombC = "CC
Case  txtCombA = "AB" And  txtCombB = "AC"
   txtCombC = "AA"
End Select
Avatar of Sethi

ASKER

Perfect..Thanks
Hi, Sethi.

If an IF/THEN statement is acceptable, you could:

IF txtCombA.text = "AA" and txtCombB.text = "BB" THEN
     txtCombC.text = "CC"
ElseIF txtCombA.text = "AB" AND txtCombB.text = "AC" THEN
     txtCombC.text = "AA"
ElseIF...

End IF
Not bad...
Or even

Dim sAB() As String
Dim sC() As String
dim i as Long

sAB = Split("AA|BB~AB|AC","~")
sAC = Split("CC~AA","~")

For i = lbound(sAB) to UBound(sAB)
    if  txtCombA & "|" & txtCombB = sAB(i) Then
        txtCombC = sAC(i)
        Exit For
    End If
Next
Avatar of Sethi

ASKER

The accepted solution was perfect :-)