Wednesday, January 19, 2011

Using Switch Function in Crystal Reports

I haven't found much about crystal reports on the internet which made me wonder. It is one of the most widely used reporting tool. Here (through this blog) I intend to keep a journal of my learnings. How better can it get if someone else can utilize and learn! There's no better feeling.

So here is my first post on Crystal Reports.......
  • Want to use a formula to filter your report?
  • Don't know how to use the Switch function in crystal reports?
  • You are always using range parameters (like the one in the below screenshot) and wondering if you could use just on parameter to accomplish the filter?


Using 2 parameters (From and To) to filter the report using a range


 You are in the right place!

While range parameters do have their pros, you must be clear on what you need to accomplish. Let me get on to Switch now.

The Switch statement has a very simple crystal syntax
Switch (expr1, value1, expr2, value2, ...., exprN, valueN)

{Command.is_Facility_ID} in {?From Facility} to {?To Facility} and
{Command.is_Insurance_ID} in {?From Payer} to {?To Payer} and
Switch ({?Coding Stts} = "Coded", {@Coding Stts} = "Coded",
{?Coding Stts} = "Un-coded", {@Coding Stts} = "Un-coded",
{?Coding Stts} = "All", {@Coding Stts} in "Coded" to "Un-coded")

While I use the switch statement, I do not have to mention the range. I can achieve almost the same thing using a single parameter.

Above here I am trying to display all the records that are "Coded" if the parameter selected by the user is "Coded" or "Un-coded" in case of "Un-coded" selection. If the user selects "All", I am displaying all between "Coded and "Un-coded".

Now what if there were more options between "Coded" and "Un-Coded" like "Re-coding", "Coded but not finalized" etc. in that case the final statement could look more or less like
{?Coding Stts} = "All", {@Coding Stts} in "AA" to "ZZ")
This will pretty much display everything that's between, which literally means "All" :)

Advantages:
  • Saves development time.
  • Users don't have to enter parameter values twice while running the report. Saves their time.
  • Saves A4 real estate.
You've got something to share? Comment and let us all know.