Você está na página 1de 15

Tutorial 1-1.

Printing Your First Report

This lesson walks you through the steps of creating a new report. You will use the Report Creation Wizard to make this as easy as
possible. When you are finished, you will have created and printed out a simple report that shows employee names.

1. Open Crystal Reports and make sure you are on the Start Page.
2. Click on the Standard Report Wizard link.
3. This is the first option under the New Reports column in the middle of the page. After clicking on this link, the Standard
Report Creation Wizard opens. This wizard walks you through the steps of creating a new report. The first step is to select
the data source.
4. Double-click on the Create New Connection folder to expand it.
5. Double-click on the Access/Excel (DAO) selection. This opens the Access/Excel (DAO) dialog box that lets you find the
file to use as a data source.
6. Click on the button to the right of the Database Name textbox and navigate to the Xtreme.mdb database. It is located at:

C:\Program Files\Business Objects\Crystal Reports 11\Samples\en\Databases\xtreme.mdb.

7. Click the Ok button to select this database.

8. Click the Finish button to close the dialog box.


9. Expand the Tables selection to see the tables in the database.
10. Click on the Employee table name to select it.
11. Click on the Right arrow to move the Employee table to the Selected Tables list.
12. Click on the Employee Addresses table name to select it.
13. Click on the Right arrow to move the Employee Addresses table to the Selected Tables list.
14. Click the Next button at the bottom to save the tables. The Link dialog appears showing you the field(s) the two tables are
joined by. In this example the Employee Id field links the Employee table to the Employee Address table. You'll learn more
about linking tables in Chapter 11 on databases.

15. Click the Next button to accept the default link. The Fields dialog appears showing all the fields from each table. Moving
fields from the Available Fields list to the Fields to Display list on the right adds the fields to the report.
16. Click on the following fields and then click on the Right arrow button to move them to the Fields to Display list: Employee
Id, Last Name, First Name, City, and Region.
17. Click the Next button to save these fields. The Grouping dialog appears showing the fields that were selected in the
previous step. We will look at grouping and sorting records in Chapter 3. Click the Next button for now. The Record
Selection dialog determines which records from the database get displayed. We will cover this in more detail later in the
book and not make any changes for this tutorial.
18. Click the Next button to move forward.

The Template dialog appears so you can choose from a list of pre-determined report formats to make a report look good.
Crystal Reports comes with over a dozen different styles to choose from. As you click on each template, the preview area
to the right shows an example of what the report would look like.

19. You can select one of the templates to automatically format your report to look a certain way. In this example click on the
No Template option to keep things simple. You can go back later and play with the other templates. Crystal Reports creates
the report and automatically previews it for you.
Overall, the report doesn't look too bad. One thing you might notice right away is that the Business Objects logo is
displayed in the report header. Of course, in your own report you will customize the report header to have you own
corporate logo, but we'll discuss how to do that later. Another thing to notice is that the report title is simply called Report1.
This isn't a very descriptive name and you definitely need to change it.

20. On the application menu, select File | Summary Info. This opens the Document Properties dialog.
21. Enter Employee List as the title.

22. Click the Ok button to update the report tab.


23. On the application menu select File | Print to print the report. The report will print out on your default printer.
24. Save report as

Employee List.rpt.
25. If you want to make more changes to the report's design, click on the Design tab above the report. This shows each section
of the report and the report objects in each section. The details of modifying the report design are covered throughout the
book.

Setting Designer Defaults

When working with reports, there are certain aspects that you want to be the same for every report. For example, you might like to
have report objects snap to the grid so that it's easier to line up objects next to each other. Crystal Reports lets you set certain
properties of the report designer so that they are the same for all reports. The primary areas of the designer that you can control are the
design area and the formatting properties of certain fields. To change these defaults select the menu items File | Options. It shows the
Options dialog box in Figure 2-1.
Figure 2-1. Report designer Options dialog box.

The Options dialog box might look a little overwhelming at first glance. There are eight different tabs to choose from and each tab has
many options listed on it. To make it easier to understand, we are only going to look at the options that affect the Report Designer.
We'll look at the other options later in the book for the sections that they apply to. Table 2-1 shows a description of each tab.

Table 2-1. Option dialog box tab descriptions.

Tab Name Description


Layout Sets the defaults for the report designer and the report preview.
Changes how the report interfaces with the database and sets advanced optimization properties. This is discussed
Database
in Chapter 11.
Formula Editor Sets the default programming language and code formatting properties. This is discussed in Chapter 5.
Specifies how report data is treated. For example, it determines how to handle Null values, whether to save report
Reporting
data with the report, etc. This is discussed in various areas throughout the book.
Set the default formatting of each type of data. You can have strings formatted differently than numbers. This is
Fields
discussed in this chapter.
Similar to the Fields tab, the Fonts tab sets the default formatting for different types of data. For example, you can
Fonts
specify a default format for summary fields that is different than the format of group name fields.
Smart Tag & HTML Sets up the properties used by Office XP smart tags and previewing reports in HTML format. This applies to
Preview Business Objects Enterprise and is not discussed in this book.
Dependency Checker Determines which aspects of a report the dependency checker should analyze. Discussed in Chapter 5.
Chapter 3 - Sorting and Grouping

Reports can be sorted on a single field or on multiple fields. Sorting on multiple fields is only useful when the sort field has duplicate
values in it. To resolve which one should be listed first, the value in the second field is used to resolve the conflict. An example of this
type of report is an employee report that sorts by name. The first sort field is the last name. When there are duplicate last names, the
second sort field, the first name, is used. When sorting on multiple fields, you have to specify the order that the fields get sorted in.

To select a field for sorting, either drag and drop it onto the Sort Fields window on the right or click on the arrow buttons to move it
over. The order in which you add the fields determines which one gets priority in the sort order. The first field listed becomes the
primary sort field. The next field is the secondary field, and so on. When there are duplicate values in one of the fields, then the next
field on the list is used to resolve the conflict. This continues for all the sort fields whenever there are duplicates. If a field has a
duplicate value without another field after it, then the one that gets listed first isn't always predictable.

The bottom of the dialog box is where you set whether the field is sorted in ascending or descending order. There are two radio
buttons that set the sort order and only one can be selected at a time. Since there multiple fields can be shown in the Sort Fields list
and there is only one place to set the sort order, both radio buttons apply to the currently selected field. To set the sort order of a field,
first click on the field you want and then click on the sort order.

Let's look at the example sort order shown in Figure 3-c. The primary sort field is Country. The secondary field is Region and this is
followed by the Customer Name.

Figure 3-c. Sorting records on Country, Region and then Customer Name.

The report first lists all the countries that start with the letter "A". The country Australia has duplicate values, so the report performs a
secondary sort on the Region field. The region New South Wales is duplicated so the final sort order is by Customer Name.
If you later determine that you need to change the sorting order, modify it using the same steps mentioned earlier.

Tutorial 3-1. Creating a grouping report

To practice working with groups, let's modify the Employee List report created in Chapter 1. The report lists each employee on a line
and we are going to modify it so that it first groups by the region and then by the city. Within this grouping it sorts by last name.

1. Open the Employee List report you created in Tutorial 1-1.


2. Select the menu items Report | Group Expert. This opens the Group Expert dialog box.
3. In the Available Fields list click on the Employee_Addresses.Region field and drag it to the Group By list.
4. Click on the Employee_Addresses.City field and drag it to the Group By list.
5. Click the OK button. The dialog box closes and the two groups have been added to the report.
6. In the report designer, move the report object Group #2 Name a little to the right so that it is offset from Group #1 (the
Region).
7. Click the OK button to save your changes. Your report's design should look similar to Figure 4-e. The report preview is in
Figure 4-f.

Figure 4-5. Employee List with groupings.


Figure 4-6. Employee List preview.

One unusual aspect about this report is that the first few records don't have any data in the Region field and all you see are the two
cities listed (London and Nottingham). It would be nice to classify these records so that they are separate from the records that have
data in the Region field. Soon we'll look at options for customizing the grouping format as well as fixing problems such as this.

Formatting Groups

The default layout for groups is that they print one after the other. When a group footer is printed, the subsequent group's header is
printed right after it. Some report requirements require changing this default behavior. For example, an invoicing report is designed
such that it is grouped by client and each client gets their own invoice. For this report you will put the client information in the group
header section and force the group header to be printed at the top of a new page. The summary values appear at the bottom of the
page. The group footer is set to always print at the bottom of the page.

The formatting options for groups are in the Section Expert. The Section Expert is discussed in detail in Chapter 8 but for now we are
going to look at which options are beneficial for using with groups.

To get to the Section Expert dialog box, right-click on the group section and select Section Expert. This opens the Section Expert
dialog box which lists all the sections along the left side and the section properties are listed to the right. The properties of interest here
are Print at Bottom of Page, New Page Before, New Page After, Reset Page Number After, and Keep Together.

Print at Bottom of Page

This forces a section to always print at the bottom of the page even if the detail records stop printing in the middle of the page. This is
commonly set for the Group Footer section. It is useful for reports that print summary information (sub-totals, disclaimers, logos, etc.).
It is also useful for form letters where the bottom of the page must have exact placement.

New Page Before / New Page After


These two properties are used to force the groups onto individual pages. This is so that you can split the reports apart and distribute
them independently. For example, a report grouped on Employee Id can be broken apart and each employee can be given their own
report without seeing any information about the other employees.

You can either set the New Page Before property for the Group Header or set the New Page After property for the Group Footer. Both
give you the same results of splitting the groups onto separate pages. The only problem is that sometimes these options cause a blank
page to be printed either at the beginning of the report or at the end of the report. I address this more fully in the Advanced Tutorials
section because it requires writing formulas.

Reset Page Number After

This sets the page number back to 1 after the Group has been printed and is usually associated with the Group Header. It insures that
the page number for each group starts at 1. Just like the New Page Before property, this is useful for reports that are separated and
distributed.

Keep Together

The Keep Together property forces a section to stay on the same page. For example, if the Group Header has the Keep Together
property enabled and the entire header can't fit on the page, then it will be started at the top of the next page. The important thing to
note is that this only applies to an individual section and it won't take other sections into account.

The Keep Together property may cause a little confusion because there is another property which has similar characteristics and also
has a similar name. It is the Keep Group Together property. Let's look at what each property does and how they differ.

The Keep Group Together property looks at the entire group (its header, details and footer) and tries to keep them on the same page. If
the entire group can't fit on the remainder of the current page, then the group is started at the top of the next page. This is clearly
different than the Keep Together property because it only applies to a single section and the Keep Group Together property looks at
multiple sections.

It's important to note that the Keep Group Together property is in a totally different dialog box than the Keep Together property. The
Keep Group Together property is in the Change Group Options dialog box (select Report | Group Expert | Options).

There are some rules associated with the Keep Group Together property. If a group starts in the middle of a page and it can't be fully
printed by the end of the page, then it starts printing at the top of the following page. If a group requires more than a full page to print,
then it will start printing at the top of the following page and continue onto the next page. The Group Header will not be printed on the
additional pages unless the property Repeat Group Header on Each Page is set (also in the Change Group Options dialog box).

Chapter 4 - Selecting Records with Parameters

Using the Select Expert

To open the Select Expert, select the menu items Report | Select Expert. The other method is clicking on the Select Expert button on
the Toolbar.

If this is the first time the Select Expert has been run for the report, it shows the Choose Field dialog box. As you can see in
Figure 4-1, it simply lists all the fields available. The fields already used on the report are shown first and they are followed by every
available field from the report's data source. A field from either of these groups can be selected.
Figure 4-1. The Choose Field dialog box.

Once you select a field from this dialog box and click the OK button, the dialog box is not shown again. Instead, you are always taken
to the Select Expert dialog box shown in Figure 4-2.

Figure 4-2. The Select Expert dialog box.

There are two tabs in the Select Expert dialog box. The first is titled with the field that was just selected in the Choose Field dialog
box. The second tab is titled . The tab with the field name has a dropdown box for selecting the filter criteria. This is set to is any
value. By default, reports don't have any filters turned on and every record gets selected. Click the dropdown box to view available
filtering options and select the one you want. The dialog box in Figure 4-2 shows all the options in this dropdown box. This list is the
textual equivalent of the basic comparison operators. They are described in detail in Table 4-1.

Table 4-1. Comparison operators for filtering data.

Criteria Description
Is Any Value Every record is selected. This is the same as not specifying a record selection formula.
Is Equal To The field must exactly match a specified value.
Is Not Equal To All records where the field does not match the specified value are selected.
Lets you build a list of acceptable values. As long as the field's value matches any one of the values in the list then the
Is One Of
record is selected.
Is Not One Of Similar to Is One Of except that the record is selected if the field's value doesn't match any of the values in the list.
Is Less Than The value must be less than the value specified.
Is Less Than or
The value must be equal to or less than the value specified.
Equal To
Is Greater Than The value must be greater than the value specified.
Is Greater Than or
The value must be equal or greater than the value specified.
Equal To
You specify a range of values and the field's value must be within this range. It can also be equal to the endpoints of
Is Between the range. For example, if the start and end points are 100 and 200, then the following values would be acceptable:
100, 101, 199, 200.
The value must be outside the range. For example, if the start and end points are 100 and 200, then the following
Is Not Between
values would be acceptable: 1, 99 and 201.
Selects string fields that start with one or more characters. For example, a value of "B" would match the names
Starts With
"Brian" and "Barry".
Does Not Start Selects string field that do not start with the characters specified. For example, a value of "B" would match the names
With "Lynn" and "Karen" because they do not start with the letter "B".
Is Like Let's you use wildcards for specifying the matching criteria. Using wildcards is discussed more later in the chapter.
Is Not Like The opposite of Is Like.
Formula: Lets you type in the formula directly. Useful when the formula is too complex for the available criteria.

If you are setting a filter using a field that is a Boolean data type, you are given two new criteria: Is True and Is False. These criteria do
exactly what they imply. They select records where the field has a value of either True or False.

Selecting a DateTime field gives you two additional options listed with the comparison operators. These options let you select dates
based on sophisticated date ranges. For example, you can select fields that fall within the last 7 days or select fields that are in the first
quarter of the calendar year.1 See Figure 4-3 for a list of the available functions.

Figure 4-3. Available date range functions.


After selecting a comparison method, the right side of the dialog box changes so that you can enter the value to compare the field to.
With the majority of the comparisons, only a single dropdown box is shown. For example, comparisons such as Is Equal To or Is
Greater Than compare the field to a single value.

Selecting Records Tutorials

Tutorial 4-1. Selecting Records

Let's put what we learned about selecting records to use. In this tutorial we will set a filter on two fields: Country and Last Year's
Sales. For Country we want to select a specific set of countries to print (Canada, England, and USA). For the Last Years Sales field we
only want to see records with a sales amount over $50,000.

1. Open the Crystal Reports sample report Group.rpt. This report demonstrates grouping data on the Country and Customer
Name fields. It shows the previous year's sales by customer and their percentage of the total sales.
2. Save the report as "Select Expert Tutorial.rpt".
3. Click the menu items Report | Select Expert.
4. The Choose Field dialog box opens for you to select the field to filter on. Select the Customer.Country field.
5. The Select Expert dialog box opens with the Customer.Country tab shown. For the comparison type, select Is One Of from
the dropdown box.
6. In the second dropdown box, select each of the following values: Canada, England and USA. This selects all records that
are from one of those three countries.
7. Create a second filter field by clicking on the tab and select the field Customer.Last Year's Sales. Click the Ok button to go
back to the Select Expert dialog box.
8. For this field we want all sales that are greater than $25,000. For the condition select Is Greater Than or Equal To. In the
second dropdown box type in $25,000.
9. To make sure you've entered both conditions properly, click on the Show Formula button in the bottom right hand corner.
Compare your formula to the following:

{Customer.Country} in ["USA", "England", "Canada"] and

{Customer.Last Year's Sales} >= $25000.00

10. Make any necessary corrections to your formula if there is a mistake.


11. Preview the report and it should look like the following. Notice that each sales amount is greater than $25,000.
Creating Parameters

The first part of this chapter showed how filtering data lets you customize a report to fit a specific user's needs. The only drawback to
this approach is that you have to know in advance what that user's needs are when you are designing the report. And if different users
have different needs, then you either have to save a separate copy of the report for each variation or the user needs to know how to
customize the report themselves. Neither of these options is the most practical approach. A much better solution is to create a filter that
uses parameters to prompt the user for their input prior to printing the report. This lets you create one report that satisfies many users.

In their most basic form, report parameters are simply a means of asking the user questions about what they want printed. When the
user opens the report he or she is prompted with each question and the answers are used to carry it out. Some examples of typical
questions are, "What sales district do you want to report on?" and "What is the minimum sales amount you want to see?" Questions
such as these let the user take a report that was designed to be used by multiple people and only view the data that he or she is
interested in. The biggest benefit is that you don't need to save a separate copy of the report for each user. New parameter values can
be entered each time the report is viewed. Figure 4-5 shows a parameter prompting the user for a report date and the print range.
Figure 4-5. Parameter prompt dialog box.

In this example the user is being prompted to enter two parameters. The first being an Order Date and the second is a date range. You
can see a calendar displayed on the screen as well. Crystal Reports lets the user enter dates by typing them in directly or picking the
date from this calendar control.
Tip
This dialog box is fully customizable. You can change the fonts and colors by modifying a
single file. This is discussed at the end of the chapter in the Advanced Tutorials section.

Note New to Crystal Reports XI is the ability for parameters to present the user a list of live data values to choose from. In previous
version of Crystal Reports the user could only choose from a static list of values that you created in advance. The values weren't able
to reflect recent changes to the database. Crystal Reports XI now gives this powerful new feature that connects the value list to the
data source. This is discussed in more detail later in the chapter.

Você também pode gostar