?
Solved

Crystal Reports-Need way of specifying multiple unnested IFs in formula

Posted on 2009-07-15
5
Medium Priority
?
412 Views
Last Modified: 2012-08-13
I am filtering out substrings in a database comment field with the following example code:

if InStr({MyTable.Comment},"Red") = 1 then
    replace(replace({MyTable.Comment}, "Red",""),"  "," ")
else if InStr({MyTable.Comment},"Blue") = 1 then
    replace(replace({MyTable.Comment}, "Blue",""),"  "," ")
else
    {MyTable.Comment}

This works perfectly if "Red" is in the comment in one row and "Blue" is in the comment in a different row, but what if "Red" and "Blue" are in the same comment?  Plus, there may be other substrings in the comment that should not be filtered:

DATABASE COMMENT       PRINT ON REPORT
Green                                 Green
Red
Blue
RedYellow Green              Yellow Green
RedBlue                          
BlueYellowRed                  Yellow
Yellow Cyan Green           Yellow Cyan Green

I really need the ability to have independent, unnested IF statements in the formula:

if InStr({MyTable.Comment},"Red") = 1 then
    replace(replace({MyTable.Comment}, "Red",""),"  "," ")
If InStr({MyTable.Comment},"Blue") = 1 then
    replace(replace({MyTable.Comment}, "Blue",""),"  "," ")
If none of the above   <-- pseudocode... unsure of how to specify this in exact code
    {MyTable.Comment}

or (in pseudocode, kind of)....

If substring of comment contains "Red" or "Blue"
       if InStr({MyTable.Comment},"Red") = 1 then
          replace(replace({MyTable.Comment}, "Red",""),"  "," ")
       If InStr({MyTable.Comment},"Blue") = 1 then
          replace(replace({MyTable.Comment}, "Blue",""),"  "," ")
else
    {MyTable.Comment}

When I do this, however, I get the error, "The remaining text does not appear to be part of the formula".  How can I filter on multiple, independent conditions?  I can't specify all the permutations of color combinations that could be in the comment - there would be way too many.

Important Note: There will never be the condition when the same color appears twice in the comment string:
RedGreenRed  <-- won't happen
BlueBlue Blue   <-- won't happen

In lieu of sugestions, can you please respond with the exact code and an explanation (if any) of why it needs to be done a certain way.  Thanks in advance.

0
Comment
Question by:bshelby777
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 101

Accepted Solution

by:
mlmcc earned 1840 total points
ID: 24865496
Try it this way
Use a local variable to store the string and work on it.
Remove all REDs
Remove all BLUEs
Remove the ,s

Local StringVar FixedString;
FixedString := {MyTable.Comment};
FixedString  := Replace(FixedString ."Red","");
FixedString  := Replace(FixedString ."Blue","");
FixedString  := Replace(FixedString .",","");
FixedString

mlmcc
0
 
LVL 101

Expert Comment

by:mlmcc
ID: 24865504
There is no real reason to test for RED or BLUE just remove it.  Replace will replace all occurances of the word so if you ever do get RedBlueRedGreen it will result in Green

mlmcc
0
 
LVL 35

Assisted Solution

by:James0628
James0628 earned 160 total points
ID: 24866012
 As mlmcc pointed out, you don't need to test for the target strings before you use Replace to remove them.  With that in mind, if you just have a few strings to remove, you can just use nested Replace functions.

Replace (Replace (Replace ({MyTable.Comment}, "Red", ""), "Blue", ""), "  "," ")


 If you have more than a few strings to remove, using a variable and separate Replace functions as in mlmcc's suggestion would probably be easier to follow (nested function calls like the one above can get hard to follow pretty quickly :-).

 James
0
 

Author Closing Comment

by:bshelby777
ID: 31604043
mlmcc - Excellent!  That code was exactly what I needed. 460 points.
James0628 - Very helpful example when there are only a few strings. 40 points.
Thank you both for your responses - they were very helpful.  I wish there were more than 500 points to give!
0
 
LVL 35

Expert Comment

by:James0628
ID: 24875225
You're welcome.  Glad I could help.

 James
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Crystal Reports: 5 Tests for Top Performance It is complete, your masterpiece report.  Not only does it meet your customer’s expectations, it blows them out the water, all they want is beautifully summarised and displayed in a myriad of ways. …
Hot fix for .Net Crystal Reports 10.2.3600.0 to fix problems with sub reports running on 64 bit operating systems ISSUE: Reports which contain subreports fail with error "Missing Parameter Value" DEPLOYMENT SERVER OS: Windows 2008 with 64 bi…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Suggested Courses

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question