Creating Custom Report Templates
Being able to use existing reports as a template is a nice feature. But you can take this one step further by creating report templates from scratch. Custom templates are more flexible because the fields can be formatted to account for any data type.
Creating a report template is very similar to creating any other report. The difference being that you want to do so in such a way that is generic so that it can be used with a variety of other reports. Crystal Reports has a new report object used just for custom templates: the Template Field Object. It is a generic object which isn’t associated with a specific data type. This object can be used to map any type of field and apply the applicable formatting. Add it to your report by selecting the menu items Insert > Template Field Object. Drop the template object on the report and repeat as necessary for all the possible fields. Format the fields using the Format Editor dialog box.
Figure 8-15. The Format Editor dialog box for template field objects.
The Format Editor has a tab for every data type available. This lets you set the formatting properties for every type of report object. When the object is mapped onto a field in the target report, only the applicable formatting properties get applied.
When you preview a custom template, the template objects don’t display any data because they aren’t associated with a field in a database. They are blank by default. If you want to have the field display default data on the report in preview mode, you can use the Formula Workshop to set a value to display in the template object.
When you add a new template object to the report, Crystal Reports automatically creates a new formula for that template object. The formula’s only purpose is to assign a value that gets displayed in preview mode. The formula is as follows:
By changing the function Space(10) to a string, the text object will be visible on the report in preview mode.
Using the menu Insert > Template Field Object to add template objects isn’t the most efficient way to create a template. Every time you do this a new formula is added to the report. This quickly results in numerous formulas to manage. If you want each one to display a non-empty string, you have to modify each formula to display dummy text. Rather than do that, you are better off adding a single template object, modifying its formula, and then copying and pasting this object throughout the report. This means that you will only have one formula to manage. It’s also very likely that many of the fields in each section will have similar formatting. By copying and pasting the template object throughout a section, you only have to modify the formatting of the first object and the rest will be an exact match. This is a big time saver.
Once you are finished creating the template, you should go to the Document Properties dialog box and enter the Title of the report. The title is what gets displayed in the Template Expert dialog box. If you want a preview of the report to be shown as well, make sure the option “Save Preview” is selected and preview the report prior to saving it.