Chapter 7 - Writing Formulas


Crystal Reports gives you the option to program formulas in either Crystal syntax or Basic syntax. This chapter teaches you how to program with both Basic syntax and Crystal syntax. Dozens of code samples show you exactly how to write code that you can put in your own reports today.

Become a Crystal Reports expert with the authoritative resource available. The tuturials and tips in this book will take your skills to the next level.
Buy at

This is an excerpt from the book Crystal Reports Encyclopedia. Click to read more chapter excerpts.

Printing Checkboxes

Displaying checkboxes on a report presents a new challenge. Checkboxes represent when a Boolean value is either true or false. But Crystal Reports doesn't have a way to print checkboxes. There are other ways to display a Boolean constant. You can display the words Yes/No or True/False by right-clicking on the field and selecting Format field. The dialog box displays a drop-down box that lets you select how you want the data to be displayed (Yes/No, True/False, etc.).

Displaying Boolean values as a checkbox requires a little more creativity. You have to trick Crystal Reports into printing checkboxes by switching to the Wingdings font. There are a variety of interesting characters in the Wingdings font. But five of them can be used for displaying checkboxes. Figure 7-10 shows some of the Wingdings characters available.

Figure 7-10. Wingdings control characters.

In the last two rows are the checkboxes. There are four types to choose from. An empty checkbox is found on the fifth row near the end. To use these characters in your report you have to create a formula that uses the ASCII code for the checkbox to display and return the appropriate character. The only problem with this idea is that it is very difficult to determine the correct ASCII code. In previous versions of MS Windows, you could open the Character Map and it showed you the ASCII code at the bottom. You could easily type this number into your formulas. But with Windows XP, they stopped showing the ASCII code and now they only show the Hexadecimal equivalent. I don't know why Microsoft implemented this “feature” because it's very difficult to convert the hexadecimal number to a decimal. So rather than make you figure out the math, I'm going to give you a cheat-sheet to use for your reports. Table 7-5 shows each checkbox and its ASCII equivalent.

Table 7-5. The Wingdings checkboxes and their ASCII equivalents.

CheckboxASCII Value
168
251
252
253
254

To display a checkbox on the report, create a new formula field and insert the following formula.

If {Table.Field} = True Then
         'Display the checkbox of your choice here
         Formula = Chr(254)
Else
         'Display empty checkbox
         Formula = Chr(168)
End If

This formula tests a field to see if it is True. If so, it returns the control character 254 (a checked box). If it is false, it returns the control character 168 (an empty checkbox). Save the formula and close the Formula Workshop.

Drag this formula from the Field Explorer onto the report. Finally, change the font to be Wingdings by right-clicking on the field and selecting Format Field. Click on the Font tab and use the dropdown box to select the Wingdings font. Save the report and preview it to see the checkbox displayed.

I recommend using a font that is two point sizes larger than the font size of the rest of the report text. The Wingdings font is slightly smaller than regular text and making it a little larger makes it easier to see. You should experiment with the font size to see what suits your tastes.

Note The Wingdings font has lots of interesting characters in it. You can use any of these characters in your report to print interesting graphics. For example, you might want to print a telephone graphic in the header of the phone number column and an envelope in the email column. You could print a time bomb on a row that requires immediate attention. Getting creative with the Wingdings font lets you have a little fun with a report and still keep a professional image.
To read all my books online, click here for the Crystal Reports ebooks.