Micro Focus QTP (UFT) Forums
Correcting If statement looking for multiple strings - Printable Version

+- Micro Focus QTP (UFT) Forums (https://www.learnqtp.com/forums)
+-- Forum: Micro Focus UFT (earlier known as QTP) (https://www.learnqtp.com/forums/Forum-Micro-Focus-UFT-earlier-known-as-QTP)
+--- Forum: UFT / QTP Beginners (https://www.learnqtp.com/forums/Forum-UFT-QTP-Beginners)
+--- Thread: Correcting If statement looking for multiple strings (/Thread-Correcting-If-statement-looking-for-multiple-strings)



Correcting If statement looking for multiple strings - mv8167 - 10-27-2011

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



RE: Correcting If statement looking for multiple strings - ravi.gajul - 10-27-2011

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



RE: Correcting If statement looking for multiple strings - mv8167 - 10-28-2011

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


RE: Correcting If statement looking for multiple strings - anemuday - 10-31-2011

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.


RE: Correcting If statement looking for multiple strings - mv8167 - 11-03-2011

@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


RE: Correcting If statement looking for multiple strings - ravi.gajul - 11-03-2011

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