Wednesday, May 29, 2013

Crystal Reports : Anticipating the "(all)" choice within a Parameter

It's very often that we see a (for example) "UserID" Parameter in a Crystal Report. This would typically allow the end user to select the proper UserID that should be used by the Select Expert.

So, our Selection Criteria would look something like this (assuming {?UserId} is the name of our Parameter:


And this is great, as long as you only want to select one UserID at a time. You could even go a step further and configure {?UserID} to "Allow Multiple Values", in which case the end user could choose any combination of UserID's for the report to Select.

However, what if you want an easy way to select "all" UserID's? I've found great success with providing an (all) item in the Parameter value list. You then test {?UserID} for the presence of the "(all)" string, and if you find it, circumvent the rest of the Selection Criteria.

This is easier than it sounds. Simply add an (all) item to your Parameter Value List, then use the following code in your Select Expert;

IF {?UserID} <> "(all)" THEN {SALES.USERID} = {?UserID} ELSE TRUE

So what we're saying here is test {?UserID} for "(all)", and if you DON'T find it, use the Parameter normally. OTHERWISE, say "TRUE", which is a trick you can use within the Select Expert to force it to "skip" a portion of itself. So if it find the "(all)", it should return all records.

Try it!

No comments: