Monday, October 26, 2009

Crystal Reports Multi-line If - Then - Else blocks

I've been working with some inherited Crystal Reports today and came across something that I've seen a few times in the past as well. So, I suspect there might be quite a few folks out there who aren't aware of the way to write IF-THEN-ELSE blocks in Crystal Reports when more than one line is required to be executed in each of the code blocks.

The most likely way someone might try would be:

But, alas, this won't work. You'll get the following error:

Most people seem to hit this error and get around it by just building a different IF-THEN-ELSE block for each of the lines, so if there were 10 lines in each of the blocks there would be 10 almost identical IF-THEN-ELSE statements.

There is a better way. The trick is to convince Crystal that you only have a single statement in each of the code blocks - i.e. one statement for the IF block and another for the ELSE block. You can do this by enclosing each block in a set of parameters, like this:

There key thing to remember when doing this with an ELSE clause is to make sure that the semi-colon for the last line of the code for the IF block is inside the closing bracket for that block. If you don't then Crystal will think that the semi-colon is terminating the entire IF-THEN-ELSE block.

No comments:

Post a Comment