Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Correcting If statement looking for multiple strings
#1
Solved: 10 Years, 8 Months, 3 Weeks ago
How can I look for multiple strings in an If statement?

I have:

If GroupName <> ("CLIENT DOCUMENTS - WBS" or "COMMISSION REPORTS BY SUB / BRANCH / REP" or "BRANCH REPORT SEARCHES" or "SUB-FIRM REPORT SEARCHES" or "FIRM REPORT SEARCHES") Then
Reply
#2
Solved: 10 Years, 8 Months, 3 Weeks ago
GroupName="CLIENT DOCUMENTS"
Code:
If not strcomp(GroupName,"CLIENT DOCUMENTS - WBS",1)=0 or  strcomp(GroupName, "COMMISSION REPORTS BY SUB / BRANCH / REP",1)=0 or  strcomp(GroupName,"BRANCH REPORT SEARCHES",1)=0 or strcomp(GroupName, "SUB-FIRM REPORT SEARCHES",1)=0 or  strcomp(GroupName,  "FIRM REPORT SEARCHES",1)=0    then
    '......................... <your code>.......................................
    
End If

'*******************
'alternatively you can also go for switch case (better performance over 'the above)
GroupName="something"
Code:
Select Case GroupName
Case "CLIENT DOCUMENTS - WBS"
    'msgbox  "CLIENT DOCUMENTS - WBS"
Case  "COMMISSION REPORTS BY SUB / BRANCH / REP"
'    msgbox "COMMISSION REPORTS BY SUB / BRANCH / REP"
Case "BRANCH REPORT SEARCHES"
    'msgbox "BRANCH REPORT SEARCHES"
    Case  "SUB-FIRM REPORT SEARCHES"
        'msgbox  "SUB-FIRM REPORT SEARCHES"
        Case else
            '*******************************your code here******************************************************
    msgbox "not in list"
End Select
Reply
#3
Solved: 10 Years, 8 Months, 3 Weeks ago
Ravi,

Wow! I would have never thought to do a string compare. I thought it would be simpilar then a string compare.

I would use a Case statement if for each case I needed to do differentn testing. But these 5 all have similar testing if selected while the other string all follow a different path.

Thx a ton
Lor
Reply
#4
Solved: 10 Years, 8 Months, 3 Weeks ago
You can also use Instr function.
Code:
str="CLIENT DOCUMENTS - WBS" or "COMMISSION REPORTS BY SUB / BRANCH / REP" or "BRANCH REPORT SEARCHES" or "SUB-FIRM REPORT SEARCHES" or "FIRM REPORT SEARCHES"
GroupName="CLIENT DOCUMENTS"
if instr(1,str,GroupName) > 0 then
print "String exists
else
print "String does not exist"
end if

If your string values are dynamic, better use dictionary object.
Reply
#5
Solved: 10 Years, 8 Months, 3 Weeks ago
@anemuday

Thxc for the idea. I tried your suggestion but I get a Run Error:

Type mismatch: '[string: "CLIENT DOCUMENTS - W"]'

Function file: O:\QTP Tests\LibraryImageAccessTests-6.qfl
Line (156): " str="CLIENT DOCUMENTS - WBS" or "COMMISSION REPORTS BY SUB / BRANCH / REP" or "BRANCH REPORT SEARCHES" or "SUB-FIRM REPORT SEARCHES" or "FIRM REPORT SEARCHES"".

Not sure why. Any other ideas? ;-)

@ ravi

I tried
Code:
GroupName = DataTable.GetSheet("Action1").GetParameter("GroupName").ValueByRow(Gr)
If not strcomp(GroupName,"CLIENT DOCUMENTS - WBS",1)=0 or strcomp(GroupName, "COMMISSION REPORTS BY SUB / BRANCH / REP",1)=0 or strcomp(GroupName,"BRANCH REPORT SEARCHES",1)=0 or strcomp(GroupName, "SUB-FIRM REPORT SEARCHES",1)=0 or strcomp(GroupName, "FIRM REPORT SEARCHES",1)=0 then

but this errored also
Reply
#6
Solved: 10 Years, 8 Months, 3 Weeks ago
Hi ,
I have tried the same and is working fine

Code:
GroupName = DataTable.GetSheet("Action1").GetParameter("GroupName").ValueByRow(1)

'GroupName="CLIENT DOCUMENTS - WBS"
If not strcomp(GroupName,"CLIENT DOCUMENTS - WBS",1)=0 or strcomp(GroupName, "COMMISSION REPORTS BY SUB / BRANCH / REP",1)=0 or strcomp(GroupName,"BRANCH REPORT SEARCHES",1)=0 or strcomp(GroupName, "SUB-FIRM REPORT SEARCHES",1)=0 or strcomp(GroupName, "FIRM REPORT SEARCHES",1)=0 then

msgbox "no  match"
else
msgbox "match"
end if
Alternatively like anemuday said ,try the below code....
The mistake in your code is string declaration
str=str="CLIENT DOCUMENTS - WBS" or "COMMISSION REPORTS BY SUB / BRANCH / REP" or "BRANCH REPORT SEARCHES" or "SUB-FIRM REPORT SEARCHES" or "FIRM REPORT SEARCHES"
and the correction is
str="CLIENT DOCUMENTS - WBS,COMMISSION REPORTS BY SUB / BRANCH / REP,BRANCH REPORT SEARCHES,SUB-FIRM REPORT SEARCHES,FIRM REPORT SEARCHES"
copy & paste the below code and run its working fine.
Code:
str="CLIENT DOCUMENTS - WBS,COMMISSION REPORTS BY SUB / BRANCH / REP,BRANCH REPORT SEARCHES,SUB-FIRM REPORT SEARCHES,FIRM REPORT SEARCHES"
GroupName="CLIENT DOCUMENTS"
if instr(1,str,GroupName) > 0 then
msgbox  "String exists"
else
msgbox  "String does not exist"
end if
If you still have issues with the code. Please post the error message

Regards,
Ravi
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Wink compare strings and perform action Forrest Gump 4 26,452 09-06-2011, 11:55 AM
Last Post: Rohan
  If statement with multiple conditions mv8167 4 13,230 08-20-2011, 08:34 PM
Last Post: Arul

Forum Jump:


Users browsing this thread: 1 Guest(s)