Using Template Field Objects

During the process of designing a report template, a need might arise to provide some specific formatting for a field not based on its position in the report, but instead based on the type of field it is. For example, a company might require that all date/time fields be displayed in military time regardless of operating system defaults. For example, "6:02 p.m. on March 31, 2002" would have to look like "3/31/02 18:02". Another requirement could be a space as the thousand separator for all numbers (instead of the usual comma).

These requirements could easily be corporate or industry standard requirements, such as the ISO 9000 standard. At the time the template is created, it's unknown where these fields will be located in the report or how many of them there will be. A report designer would have to find another way of handling special formatting requirements. Template Field Objects help in this endeavor.

When designing a report specifically as a template, Template Field Objects take the place of regular database fields in a report. They can be placed anywhere that a database field would normally be placed.

These fields are a special type of formula field that contain no data but allow formats to be applied to them as if they were of any data type. Template Field Objects have a special dialog associated with them that exposes all the Formatting tabs of the Format Editor regardless of type. This provides a one-stop shop for all of your formatting needs regardless of the data type for a given position of a field in a report.

The best way to explain this is by actually performing it, so let's implement the examples given previously in this section:

  • Military Time: "6:02 p.m. on March 31, 2002" to appear as "3/31/02 18:02"

  • Thousand Separator as a space: 1,000 to appear as 1 000

Starting with a new report, the steps are as follows:

  1. Create a new report. After opening Crystal Reports, click the New button. Then within the Crystal Reports Gallery dialog, choose As a Blank Report and click OK.

  2. Skip the data source step. Because this report is going to be a template, there is no need to have a data source associated with it. Click the Cancel button in the Database Expert to close this dialog.

  3. Insert a Template Field Object. To insert the first Template Field Object, select Template Field Object from the Insert menu. Place the resulting field into the left-most area of the Details section (see Figure 19.4).

    Figure 19.4. The Design tab with the first template object added to the report.

    graphics/19fig04.jpg

  4. Add five more template objects. Repeat the previous step five more times and place each new field to the right of the last one. Once completed, the Design tab will look like Figure 19.5.

    Figure 19.5. The Design tab with six template objects added to the report.

    graphics/19fig05.jpg

  5. Select all template objects to format. To select all template objects, hold down the Ctrl Key while single-clicking on each template object in the Details section. After all six objects are selected, right-click on the last object you chose and select Format Template Fields from the pop-up menu.

  6. Format Date/Time to military time (3/31/02 18:02). After the Format Editor appears, select the Date and Time tab. Choose the third option in the Style list box that represents date/time as 3/1/99 13:23 because this is the option required, as shown in Figure 19.6.

    Figure 19.6. The Date and Time tab of the Format Editor with Military Date/Time selected.

    graphics/19fig06.jpg

  7. Format Number with a space as the thousand separator (1 000). Now select the Number tab in the Format Editor dialog box. Because our style does not appear in the Style list box, select Customize. In the Custom Style dialog box, change the Symbol to ,. Select OK to return to the Format Editor. Select OK again to return to the Design tab with the changes applied (see Figure 19.7).

    Figure 19.7. The Custom Style dialog box with the space set as the Thousand Separator symbol.

    graphics/19fig07.jpg

  8. Give the template a name and a preview picture. When the report appears in the Template Expert, it will have a name associated with it. The name is saved as the Report Title. To change the Report Title, select File, Summary Info. Input the name Military Time & Thousands in to the Title property field and select the Save Preview Picture check box as shown in Figure 19.8. Select OK to continue.

    Figure 19.8. The Report Title set to describe the Template report.

    graphics/19fig08.jpg

  9. Save the report to the template folder. Choose File, Save As to save the report. Call the report TemplateObjects.RPT and place it in the Template folder. The Template folder is usually found at c:\program files\crystal decisions\crystal reports 9\templates. When it is saved, close the report.

  10. Open the CHAP5.rpt report. This is the same report we used earlier as our base report. Notice that all the date fields and numbers are in the standard format, as shown in Figure 19.9.

    Figure 19.9. The Preview tab showing how the report looked when Hour 5's lesson was completed.

    graphics/19fig09.jpg

  11. Select the template. To select the template that was saved earlier, select Report, Template Expert. Select Military Time & Thousands from the Available Templates list, as shown in Figure 19.10.

    Figure 19.10. The Template Expert dialog box with a template selected.

    graphics/19fig10.jpg

  12. To apply the template, simply click the OK button. The report will appear as shown in Figure 19.11.

    Figure 19.11. The Preview tab showing how the report looked after the template was applied.

    graphics/19fig11.jpg

    graphics/alarmclock_icon.gif

    You will notice that the report appears to close. Do not be alarmed because this is standard behavior. Crystal Reports saves a temporary file with the old look of the report and then applies the template during the new open command.


    graphics/lightbulb_icon.gif

    Undoing a template is always an option. If for some reason, you do not like the look that the applied template has given to your report, just return to the Template Expert and choose the Undo command at the bottom of the dialog box. Selecting this option and then clicking OK lets Crystal Reports revert to the original report before the template was applied.

    Crystal Reports accomplishes this by opening up the temporary backup RPT that it saved before applying the template file.


  13. Save the report. Choose File, Save As, call the report CHAP5_templateapplied.rpt, and click OK.

Notice that there was no need to know where the date and numeric fields were located in the report because the Template Field Objects were all formatted to handle the different requirements for the different fields. Using Template Field Objects along with the other template tips mentioned in the hour will make report design quick and easy.

Preview Pictures

During the previous exercise, you might have noticed that the intended template did not show a preview picture on the right of the dialog window. This can be caused by one of two situations. The Save Preview Picture option in the Document Properties dialog box was not selected. If that option had been checked and the thumbnail still did not appear, it is because the template report was not saved with a Preview.

In the example that was just completed, the template report was not previewed before the report was saved.

Preview Pictures, or thumbnails as they are commonly called, are just that Pictures of the Preview of the report. If a report has not been previewed, it will not have the thumbnail to save.

Another key point to notice on Preview Pictures is that if changes are made in the design of the template and then a save is done, the changes will not be reflected in the thumbnail because the Preview tab was not updated with the changes.

Preview Pictures are very useful in the Template Expert because these images provide the report designer with a visualization of what the template will do to the existing report. In order to have them saved as a default with all reports, select the Save Preview Picture option in File, Options under the Reporting tab.



Sams Teach Yourself Crystal Reports 9 in 24 Hours
Sams Teach Yourself Crystal Reports 9 in 24 Hours
ISBN: B003D7JUVW
EAN: N/A
Year: 2005
Pages: 230

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