Crystal Reports in the Real WorldCustom Functions


As described in Chapter 4, custom functions can be prepared in advance and can be stateless so they can be used later in a variety of ways. In the next example, a name formula is created in such a way that it builds and formats names in a consistent and reusable manner.

For more detailed information on how custom functions can be prepared in advance, p. 99.


1.

Open the Employee Profile report. From the Field Explorer, select Formula Fields and click New. Give the formula the name Title and click Use Editor. Enter the following text into the code window of the formula editor:

If InStr(LowerCase({Employee.Notes}), " he ")>0 Then     "Mr." Else If InStr(LowerCase({Employee.Notes}), " she ")>0 Then     "Ms." Else     "";


2.

Click Save and Close. Next, create another new formaula named Suffix and in the code window only type two double-quotes (the string equivalent of NULL); this acts as a placeholder because the table doesn't have a suffix field. Finally, create a formula called Proper Name and add the following text into the code window:

Local StringVar strFullName; strFullName := ""; If {@Title} <> "" Then     strFullName := {@Title} & " "; strFullName := strFullName & {Employee.Last Name} & ", " & {Employee.First Name}; If {@Suffix} <> "" Then     strFullName := strFullName & " " & {@Suffix}; strFullName


3.

Click Save and Close. From the Report menu, choose Formula Workshop. Click New and give it the name ProperName and click Use Extractor. In the list of formulas, select @Proper Name. Your screen should look like Figure 13.5. By default, the Argument Names are v1, v2, v3, and v4 but this won't help users of your formula so change the names to Title, LastName, FirstName, and Suffix.

Figure 13.5. Custom function properties.


4.

Click OK to close the window. Crystal converts the formula from the way it's currently written into a generic custom function for later use. The new custom function is shown in Figure 13.6.

Figure 13.6. Custom function formula.


5.

Click Save and Close. The custom function is now available for use.

6.

Finally, create the formula that will be used in the report. Create a new formula called Custom Name. From the list of functions, double-click the ProperName function and pass in the following values:

Title: @Title LastName: {Employee.Last Name} FirstName: {Employee.First Name} Suffix: @Suffix


Your completed formula should look like Figure 13.7.



Figure 13.7. Passing values to a custom function.


Note

In the sample database there is no Title field, but a formula can be used to generate the Title that will be passed to the custom function. The sample database also doesn't contain a suffix field and because there's no way to determine if the employee name has a suffix, a null string will be passed to the custom function. The custom function can be used later with tables that have both Title and Suffix fields.

7.

Add the Custom Name formula to the report and replace the @name formula in the Group Header 2 section. Change the font to Bold and white. Save the report as CustomName.rpt. Figure 13.8 shows the completed report.

Figure 13.8. Report using a Name built using a custom function.


 




Crystal Reports XI(c) Official Guide
Crystal Reports XI Official Guide
ISBN: 0672329174
EAN: 2147483647
Year: N/A
Pages: 365

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net