The words you are searching are inside this book. To get more targeted content, please make full-text search by clicking here.
Discover the best professional documents and content resources in AnyFlip Document Base.
Search
Published by SkillForge, 2018-11-20 21:25:59

Crystal Reports Introduction

Crystal Reports Introduction

Lesson 6: Working with Formulas 201

Add comments to formulas

If formulas become complex, they can be difficult to understand. Sometimes it can be very helpful to add
notes or comments to a formula to explain its purpose or how it works. These comments are only for you, the
developer. Crystal Reports completely ignores them.

To add comments to a formula, type // at the start of the line. The text on that line turns green, indicating it is
a comment. It is strongly recommended that you use comments whenever they may help you or others in
understanding the formula.

Figure 6-49 Formula Comments

Copyright ©2017 Pathfinder Learning, LLP

202 Lesson 6: Working with Formulas
Date and Time Functions Help Reference
There are a number of functions that allow you to work with date and time data. The help file within Crystal
Reports provides detailed information. Use the steps below to access help.
1. Press F1 OR click Help > Crystal Reports Help.
2. If needed, click the Contents tab.
3. Scroll down to Functions (at or near the bottom) and click the + to its left to expand it and display the list
of functions.
4. Click on Date and Time; note the list of functions in the right pane.

Figure 6-50 Help File

5. Click on Month (x) and review the topic. Note the examples.
6. Review other functions of your choice.

Copyright ©2017 Pathfinder Learning, LLP

Lesson 6: Working with Formulas 203

7. To bookmark a topic for future reference, click the Favorite tab on the left side of the Help screen.

Figure 6-51 Favorite Help Topics

Copyright ©2017 Pathfinder Learning, LLP

204 Lesson 6: Working with Formulas

Challenge Exercise - Create Formulas to Calculate Cost of Stock

Create a report to calculate cost of discontinued units in stock. See if you can do this without detailed
directions; refer to the bottom of the exercise for help if needed.

1. Create a new report using the Products table.
2. Save the report as Cost of discontinued products.

3. Add to the Details section ProductName and UnitsInStock.
4. Filter the data to include only include discontinued products with at least one unit in stock.

5. Create a formula to calculate the wholesale cost, which is 80% of the retail price. Name it Wholesale
cost. Add that to the report after UnitsInStock.

6. Create another formula, Cost of discontinued products, which multiplies the wholesale cost and the
number of units in stock. Display that after Wholesale cost.

7. Add a report title, Cost of Remaining Discontinued Products. Format it as you wish.
8. Save, preview and close the report.
Detailed Steps
1. Filter the data to include only include discontinued products with at least one unit in stock: Click the

Select Expert and double click Products.UnitsInStock. Click the is any value dropdown, choose

is greater than and type 0.

2. Click New and double click Products.Discontinued. Choose is true from the is any value dropdown.
Click OK.

3. Create a new formula called Wholesale cost: Right click on the Formula Fields and click New… Type
Wholesale cost and click OK.

4. Create the formula {Products.RetailPrice}*.8 then save and close the formula.

5. Create a new formula called Cost of discontinued products: right click on the Formula Fields and
click New… Type Cost of discontinued products and click OK.

6. Create the formula, in the Report Fields area, double click on Wholesale cost. Type a * and double click
on {Products.UnitsInStock}. The formula is {@Wholesale cost}*{Products.UnitsInStock}.

7. Save and close the formula.

8. Add Wholesale cost and Cost of discontinued stock to the detail section.
9. Add whatever formatting you wish.

Copyright ©2017 Pathfinder Learning, LLP

Lesson 6: Working with Formulas 205
10. Save and Preview the report, then close it.

Figure 6-52 Challenge Exercise Results

Copyright ©2017 Pathfinder Learning, LLP

206 Lesson 7: Working with Multiple Tables

Lesson 7: Working with Multiple Tables

Unit time: 75 minutes
In this lesson, you will be introduced to:
1 Understanding Database Components
2 Joining Tables and Join Types
3 Adding Multiple Tables with the Database Expert

Copyright ©2017 Pathfinder Learning, LLP

Lesson 7: Working with Multiple Tables 207

Understanding Database Components

A database is simply a warehouse for storing information. Although there are many components that make up a
traditional database system, for the purposes of this class we will focus on the following database components:

 Fields
 Tables
 Records
 Keys

Fields

A field is the smallest source of input for stored data. A field will hold a single type of information, such as
a number, a date, or a word. This is known as a data type. Examples of common fields would be last name
(to hold a word), date of birth (to hold a date), and salary (to hold a number). Fields are represented as
columns in a table and help organize the data.

Figure 7-1 Database Fields in a Table (Access)

Tables

A table is a collection of fields that have some common connection. The fields take the form of columns in
the table, similar to columns in a spreadsheet. An example of a table would be an employee address table
containing address, city, state, and ZIP code fields (columns).

Figure 7-2 Database Table (Access)

Copyright ©2017 Pathfinder Learning, LLP

208 Lesson 7: Working with Multiple Tables

Records
Records are rows in a table. Each row pertains to a single transaction. Each record contains one entry for
each field (column) in the table.

Figure 7-3 Database Record (Access)

Keys
A key is a piece of information that when referenced, leads the user to one and only one record in a table.
Key fields help reduce, and in most cases, eliminate duplication in a table. Each record of information is
represented once and the key field is used to perform lookup actions or connect a record in one table to a
record in a different table.
An example of a key field is an employee’s social security number. Each employee would be listed only
once in the employees table. Another table may list training information. As with the employees table, the
training table would have each employee listed with a separate record for each training session attended.
The two tables would be connected in a way that would allow the report designer to locate the employee by
their SSN and then use that same SSN to generate a list of every attended training session.

Connecting Tables via Keys
Databases typically contain more than one table. If they did not, and all of the information was
included in a single table, there would be an extremely high level of duplication in the table.
Imagine a hardware store database with sales transactions for each customer. Every time a customer
purchased an item from the store, their account information would have to be recorded. If the
customer shops on a weekly, or perhaps daily basis, that same customer’s information would be
repeated multiple times within the table.
By separating the information into multiple tables (a customers table, a products table, a vendors
table, etc…), each bit of information is recorded once in its respective table. The tables are then
connected by means of a key field. The key field will typically be the only piece of information that
the two tables have in common.

Copyright ©2017 Pathfinder Learning, LLP

Lesson 7: Working with Multiple Tables 209

The following example will illustrate the idea of connecting tables by way of key fields.

Our hardware store records the sale of all items sold. This list takes the form of a Daily Transaction
table. Each customer’s transaction is assigned an invoice number (1001, 1002, etc…) All items sold
against that invoice are listed by their respective product IDs.

A second table listing all of the products sold by the hardware store is maintained with each
product’s name, product ID, and price.

Because a single product (like a type of screwdriver) may be sold to multiple customers, it would be
redundant to list the product’s name and price (along with other information like supplier, quantity
on hand, restocking fee, etc…) every time that product is sold. Instead, the product ID is the only
information recorded in the sale transaction.

When the time comes to generate an invoice or receipt for the customer, the database will take the
product ID of the item purchased and locate that product ID in the product inventory list. When the
product ID is located, the product’s name and price (along with any other needed information) will
be returned and added to the invoice or receipt.

Figure 7-4 Items Discovered Through Lookup of Common Field Information
Copyright ©2017 Pathfinder Learning, LLP

210 Lesson 7: Working with Multiple Tables

Joining Tables and Join Types

When two tables are connected, also known as joined, a common field contained in both tables serves as the
connection point by which the records are matched. The result is the two physical tables behave as a single logical
table. As stated in the previous example, if an item is located in the invoice table, its price can be discovered in the
related parts table.
When joining tables together, a decision must be made regarding which records are retained and which records are
rejected. This is due to the occurrence of records in one table that have no corresponding entry in the companion
table. There are many different types of strategies for joining tables. Regardless of the database system being
implemented, there are four join types that are universal in behavior. These join types are:

 Outer Join
 Inner Join
 Left Join
 Right Join
The linking and joining of tables is handled automatically by Crystal Reports, but it is still important to understand
the strategies because a more advanced designer may be able to increase the speed and efficiency of the report by
modifying the join strategy and reducing the number of records in the logical table managed by Crystal Reports.

Copyright ©2017 Pathfinder Learning, LLP

Lesson 7: Working with Multiple Tables 211

Outer Join

In an outer join, all of the records from both tables are retained. Any records that have matching keys in
both tables are combined into a single record. Any records in one table (left or right) that does not have a
matching entry in the opposing table will occupy a single row with Null values populating the fields it does
not have information for. No data is ever lost when using the Outer Join strategy.

As witnessed in the diagram below, Jane (Emp_ID 217) has information in both tables. The result is that all
of Jane’s information is combined into a single record in the resultant table.

On the other hand, Susan (Emp_ID 604) does not have a corresponding entry in the right table. Any field
that has no information for Susan will be filled with Null values. The same action occurs for Investigator
(Emp_ID 900). Since there is no entry in the left table for Investigator, all of the fields in the left table for
the Investigator record will be filled with Null values.

Figure 7-5 Outer Join Results

Copyright ©2017 Pathfinder Learning, LLP

212 Lesson 7: Working with Multiple Tables
Inner Join
In an inner join, only records that have matching entries in BOTH tables are retained in the resultant table.
The end result is that no records should have Null entries; at least none added by virtue of the join operation.
Null may have existed in the original data source.

Figure 7-6 Inner Join Results

Copyright ©2017 Pathfinder Learning, LLP

Lesson 7: Working with Multiple Tables 213

Left Join

In a left join, the table on the left is unaltered and the table on the right only provides records that have a
matching record in the left table. Any record shared by both tables are merged into a single record. Any
records possessed solely by the right table are dropped from the collection. In any record in the left table
where there is no corresponding information in the right table, Null values are used to fill the gaps.

As is evident from the following example, All of Jane’s information is retained because there is an entry for
Emp_ID 217 in both tables. The Investigator entry (Emp_ID 900) in the right table is dropped due to an
absence of any corresponding information in the left table. Also notice that Susan’s (Emp_ID 604) record in
the resultant table; Null values occupy the Title and Years fields for her record.

Figure 7-7 Left Join Results

Copyright ©2017 Pathfinder Learning, LLP

214 Lesson 7: Working with Multiple Tables
Right Join

A right join has the exact opposite logic of a left join. In a right join, the table on the right is unaltered and
the table on the left only provides records that have a matching record in the right table. Any record shared
by both tables are merged into a single record. Any records possessed solely by the left table are dropped
from the collection. In any record in the right table where there is no corresponding information in the left
table, Null values are used to fill the gaps.
As is evident from the following example, all of the Intern’s information is retained because there is an entry
for Emp_ID 100 in the right tables. The entry for Susan (Emp_ID 604) in the left table is dropped due to an
absence of any corresponding information in the right table. Notice the Investigator’s (Emp_ID 900) record
in the resultant table; Null values occupy the F_Name, L_Name, Dept, Salary, and Status fields for this
record.

Figure 7-8 Right Join Results

Copyright ©2017 Pathfinder Learning, LLP

Lesson 7: Working with Multiple Tables 215

Adding Multiple Tables with the Database Expert

When creating a new report, you will see the Database Expert dialog box. This dialog box allows you to select the
source of the data that will be used for the report. If you are currently working with a data source and need to modify
the connection settings, click the Database Expert button located on the Experts toolbar.

Figure 7-9 Databse Expert Dialog Box – Adding Tables

The dialog will show your current database connections and the options to add a new connection. Expanding the
folder Create New Connection, will list the set of available types of data connections that you can use. Click the
small plus signs next to each folder to expand them. If a connection of that type exists, it will display here. If a
connection does not exist, a dialog box will display allowing you to connect to that type of data source.

When using the Database Expert in Crystal Reports, you will then need to input any additional information in any
dialog boxes that appear to connect to your desired type of database file. This is typical when connecting to
databases that require some form of username/password authentication. After the connection has been established,
you will be presented with tables, views, stored procedures, or other data within the selected data connection. These
items are shown within the left pane of the Database Expert dialog box. Once again, these are shown in collapsible
and expandable sections within your data connections. When expanded, you can click the icons of the tables or other

data you wish to add to the report to select them and then click the arrow to add the selected data to your

report. The data will then appear within the Selected Tables: pane at the right side of the Database Expert dialog

box.

Copyright ©2017 Pathfinder Learning, LLP

216 Lesson 7: Working with Multiple Tables

Linking Multiple Tables
When more than one table is selected, from either a single database or different databases, a new tab labeled
Links will appear at the top left of the Database Expert.

Figure 7-10 Links Tab

The Links tab displays a graphical representation of how the tables are linked to one another. Each line
represents common data between the tables.

Figure 7-11 Databse Expert Dialog Box – Linked Tables

Crystal Reports uses a process known as Smart Linking to link tables together based on similar fields in the
tables. Although Crystal Reports will normally perform admirably when establishing these links, it is a good
idea to verify the links to ensure the data is connected in a manner best suited for your analysis model. Some
report designers prefer to clear all of the suggested links and establish all links manually.

Some links may be correct while others may be incorrect or unnecessary. If this is the case, a link may be

broken between two tables by clicking on the link line and clicking the button.

To establish a new link, click and hold on the field from one table and drag and drop the field on top of the
matching field in the associated table.

Copyright ©2017 Pathfinder Learning, LLP

Lesson 7: Working with Multiple Tables 217

To examine the join type, link type, and enforcement options between two tables, double-click the line
connecting two tables. This will display a Link Options dialog box. This can also be accessed by selecting

the link line and clicking the button located to the right of the Database Expert
dialog box.

Figure 7-12 Link Options Dialog Box

If Smart Linking is a feature you would rather not implement, it can be deactivated by clicking the File
dropdown menu and selecting Options… On the Database tab, uncheck the Automatic Smart Linking
option at the bottom of the dialog box.

Figure 7-13 Program Options for Smart Linking

Copyright ©2017 Pathfinder Learning, LLP

218 Lesson 7: Working with Multiple Tables

Why Smart Linking May Not Be For You
There are some compelling reasons why designers will deactivate the Smart Linking feature.

Scenario #1
Fields from different tables that contain common information are often labeled differently.
One table may identify a field containing an employee’s number as EmpNum while a
different table may identify the same information as EmployeeNumber. Crystal Reports
would not understand that these two fields are perfect candidates for linking and ignore
them. Manual linking would be required to create the required connection.
Scenario #2
Fields from different tables that contain unassociated information may share the same field
name by virtue of nothing more than coincidence. For example; a field in Table A may be
named P_ID that refers to the purchaser’s identification number, and a field in Table B may
share the same name of P_ID but refer to a part number. Assuming the data types match (for
the sake of this example), Crystal Reports would link them together and create a highly
chaotic data model. This link would need to be manually broken.
Scenario #3
Two tables may share multiple identical field names (i.e., Name, Address, City, State, ZIP).
Crystal Reports would create links between each of these fields. This would result in an
overly complex and possibly unusable data model. These links would need to be manually
broken.

Figure 7-14 Excessive Smart Linking Example

Copyright ©2017 Pathfinder Learning, LLP

Lesson 7: Working with Multiple Tables 219

Exercise 7-1 Using the Database Expert to Create Reports with Linked
Tables

In this exercise you will add multiple linked tables to a report and create a report using fields from the
different tables to create a unified view of the data.

1. From the Start Page, under the START A NEW REPORT banner, click Blank Report.

2. From within the Database Expert, in the Available Data Sources section (left) expand the My
Connections folder.

3. Expand the Northwind 2008.mdb entry.

4. Expand the category labeled Tables.

5. Select the tables named Products and OrderDetails and add the tables to the list of Selected Tables and
click OK.

6. Observe the automatically linked tables, linked by ProductID.

Figure 7-15 Linked Tables

7. Click OK to close the Database Expert dialog box.

8. Save the file with the name My Linked Tables.

9. Add the following fields from the associated tables to the Details section:

Table Name Field Name
OrderDetails ProductID
Products ProductName
Products RetailPrice
OrderDetails Quantity
OrderDetails UnitPrice

Copyright ©2017 Pathfinder Learning, LLP

220 Lesson 7: Working with Multiple Tables
10. Group the records by the OrderID field in the OrderDetails table by clicking the Group Expert button.

Figure 7-16 Group Expert Dialog Box

11. Click OK.
12. Increase the height of the Group Header #1 section.
13. Move the field headings from the Page Header section to the Group Header #1 section. Place the field

headings below the Group #1: OrderID field.
14. Format the OrderID field in the Group Header #1 section to remove the commas.

a. Right-click on the field and choose Format Field.
b. Click -1123.
c. Click OK.
15. Draw a line spanning the width of the Group #1 Footer section.
a. Click Insert from the top menu.
b. Click Line.
c. The cursor will change to a pencil. Click where you want the line to begin. While holding the

mouse button, drag the pencil to draw the line.

Copyright ©2017 Pathfinder Learning, LLP

Lesson 7: Working with Multiple Tables 221
16. Verify the Design tab is similar to the image below.

Figure 7-17 Example of Fields Added to Details Section

17. Preview the report.
18. Verify your report is similar to the image below.

Figure 7-18 Exercise 7-1 Results

19. Save the My Linked Tables file.
20. Close the file.

Copyright ©2017 Pathfinder Learning, LLP

222 Lesson 7: Working with Multiple Tables
Challenge Exercise - Create a Report Using Multiple Tables and Groups
Create a report titled Products by Supplier and Category. The Products information is grouped by Supplier
Name and Category Name (Category is the first or outer group). Include the number of products for each
supplier and the total number of suppliers (do not count a supplier more than once, even if in different
categories). Tables are Categories, Products and Suppliers.
Format the report to make it readable. An example of the report and formatting is below (first and last page),
followed by directions on creating it.
See how much you can do without looking at the directions.

Figure 7-19 Challenge Exercise Results – Page 1

Copyright ©2017 Pathfinder Learning, LLP

Lesson 7: Working with Multiple Tables 223

Figure 7-20 Challenge Exercise Results – Last Page

Detailed Steps
1. Create a new report.
2. Add the tables Categories, Products and Suppliers: double click on each of these tables in the Database

Expert.
3. Click on the Links tab; note the links between each table (no changes) and click OK.
4. Save the report as My Products by Category and Group.
5. If needed, click on the Design tab.
6. Create a group on Category Name

a. Click on Group Expert .
b. Double click CategoryName.
c. Click on the Options button.
d. Click on the Options tab and add a check to Repeat Group Header on Each Page.
e. Click OK.
f. Click OK to save the group.

7. Add another group on Supplier Name.

a. Click on Group Expert .
b. Double click SupplierName.
c. Click on the Options button.

Copyright ©2017 Pathfinder Learning, LLP

224 Lesson 7: Working with Multiple Tables
d. Click on the Options tab and add a check to Repeat Group Header on Each Page.
e. Click OK.
f. Click OK to save the group.

8. Create a formula for the Retail Price of Inventory.
a. Right click Formula Fields and click New…
b. Type Retail Price of Inventory and click OK.
c. Add the formula {Products.RetailPrice}*{Products.UnitsInStock}
d. Click Save and Close.

9. Add to the Detail section the ProductName, RetailPrice, UnitsInStock and Retail Price of Inventory.
10. Move the column headings from the Page Header to the Group Header #2 under the Group #2 Name

field. You will need to expand the section first.
11. Insert a summary for the number of Products per Supplier:

a. Click on the ProductName in the Detail section
b. Click Insert Summary .
c. In the Calculate this Summary Drop Down, choose Count.
d. In the Summary location, Choose Group #2:Suppliers.SupplierName – A.
e. Verify in the Options section it displays as Group #1: Count of ProductName.
f. Click OK.

Figure 7-21 Summary – Number of Products per Supplier

12. Move the summary field to the right, to about 3.5”.
13. Add a text object and type Number of products supplied by (include the space after by).
14. Click on Suppliers.SuppliersName in the Field Explorer and drag it to the text object.

a. Make the text object wide enough to display the full Supplier Name.

Copyright ©2017 Pathfinder Learning, LLP

Lesson 7: Working with Multiple Tables 225

b. Right click on it and choose Can Grow in the Common tab. This allows very long Supplier Names
to fully display on two lines.

15. Insert a summary to display the number of suppliers for each category:

a. Click Insert Summary .
b. In the Choose the field to summarize: Choose Suppliers.SupplierName.
c. In the Calculate this summary: drop down, choose Distinct Count.
d. In the Summary location, choose Group#1 Categories:CategoryName – A.
e. Distinct Count only counts a supplier once regardless of how many categories in which they have

products.
f. Click OK.

Figure 7-22 Summary – Number of Suppliers per Category

16. Move the summary field to the right, to about 3.5”.
17. Add a text object and type Number of suppliers for (include the space after for).
18. Click on Categories.CategoryName in the Field Explorer and drag it to the text object. Make the text

object wide enough to display the full Category Name.

Copyright ©2017 Pathfinder Learning, LLP

226 Lesson 7: Working with Multiple Tables

19. Add a summary for the grand total: click Insert summary and choose Suppliers.SupplierName,
Distinct count and Grand Total (Report Footer).

Figure 7-23 Summary – Grand Total

20. Move the summary field to the right, to about 3.5”.
21. Add a text object Total Number of Suppliers.

Important:
If you add the total number of suppliers by category, it will be larger than the grand total of suppliers.
That is because some suppliers are listed in more than one category. To avoid confusing your audience,
you may want to include a note about some suppliers being listed in more than one category.
22. Save and preview the report.

Copyright ©2017 Pathfinder Learning, LLP

Lesson 8: Conditional Formatting 227

Lesson 8: Conditional Formatting

Unit time: 75 minutes
In this lesson, you will be introduced to:
1 Formatting Section Behavior
2 Formatting Sections with Conditional Logic
3 Suppressing Sections Conditionally
4 Formatting Fields Conditionally

Copyright ©2017 Pathfinder Learning, LLP

228 Lesson 8: Conditional Formatting

Formatting Section Behavior

Having the ability to format data according to font style, size, alignment, color, etc… gives a report designer great
flexibility when it comes to controlling the appearance of a report. The Format Editor provides dozens of controls
for altering the appearance of data. Formatting is not limited to the words and numbers displayed in a report.
Formatting can also be used to control the behavior of entire sections.

Imagine the following scenarios:

 Ensuring that a disclaimer appears at the bottom of the last printed page of the report.
 Have one section print in Portrait orientation and another section print in Landscape orientation.
 Making sure a small list of records does not become split by virtue of a page break.
 Split a single column of data into multiple columns.
 Hiding a section that contains no records.
 Printing sections side by side instead of one above the other.
 Hiding a series of calculations that serve a separate section.

Sections are formatted using the Section Expert. The Section Expert can be accessed by clicking the Report

dropdown men and selecting Section Expert…, or by clicking the Section Expert button on the Experts
toolbar.

Figure 8-1 Section Expert Dialog Box

The Section Expert is composed of two panels; the left panel lists all of the sections contained in the current report.
The right panel lists all of the controls and behaviors that can be exercised against the selected section in the left
panel.

Copyright ©2017 Pathfinder Learning, LLP

Lesson 8: Conditional Formatting 229

Because of the large number of controls available to the report designer, the controls are grouped into categories
represented by tabs at the top of the right panel.

Figure 8-2 Section Expert Tabs

Depending on the section selected, different controls will present themselves. Not all controls can be exercised on all
sections. Some controls are section specific. The following is a list of the controls and their responsibilities:

Common Tab

Control Purpose

Hide (Drill-Down Section will not be printed but the section contents will be available

OK) for drill-down. NOTE: This option is unavailable when selecting

either the Page Header or Page Footer sections.

Suppress (No Drill- Section will not be printed and the section contents will not be

Down) available for drill-down.

Print at Bottom of Each group total, subtotal, etc will print only at the bottom of a page.

Page Details will print in their normal positions. NOTE: This option is

unavailable when selecting either the Page Header or Page Footer

sections.

Keep Together All the lines of the section are printed together on the current page. If

the text won’t fit on the current page, it is automatically moved to the

next page. This option is unavailable when selecting either the Page

Header or Page Footer sections.

Suppress Blank If the section is blank, it is not printed.

Section

Underlay Following The selected object will appear beneath the text when it is printed. It

Sections can be used to create a watermark.

Format with The Layout Tab appears to allow you to customize your report into

Multiple Columns columns. NOTE: This option is only available for the Details

section.

Clamp Page Footer Removes any extra white space at the bottom of a report. This

minimizes scrolling for reports viewed online. NOTE: This option is

only available for the Page Footer section.

Reserve Minimum Crystal Reports reserves space at the bottom of each page for the page

Page Footer footer section. This option is only needed if you have a page footer

with multiple sections. NOTE: This option is only available for the

Page Footer section.

Read-only Locks the formatting and position of all the report objects. Changes

cannot be made.

Relative Positions Allows you to maintain the same amount of space between two

objects. It only works for horizontal placement.

Table 8-1 Formatting - Common Tab

Copyright ©2017 Pathfinder Learning, LLP

230 Lesson 8: Conditional Formatting

Paging Tab

Control Purpose

New Page Before This is used to insert a page break before either a new group prints or

a new detail record prints. NOTE: This option is not available Page

Header or Page Footer sections.

Reset Page Number After the group total is printed, the next page of the report will start at

After one. This could be helpful if you are printing multiple reports in a

single file. For example, monthly billing statements for customers.

New Page After This is used to inserts a page break after the section is printed. If this

is applied to the Details section, you can choose to insert a page break

at the end of the section or after XX records.

Orientation Allows each section to have a different orientation. NOTE: This

option is not available Page Header or Page Footer sections.

Table 8-2 Formatting - Paging Tab

Color Tab

Control Purpose

Background Color Allows color to be displayed in that section of the report.

Dropdown list List of available colors.

Table 8-3 Formatting - Color Tab

Layout Tab

Control Purpose
Detail Size Specify the height and width

Gap Between Specify the gap or white space that you want to appear between each
Details detail. Controls both horizontal and vertical space.

Printing Direction – Prints details across the columns. When all the columns have a detail,
Across then Down the printing begins on the second line of the first column.

Printing Direction – Prints details down the columns first. When the first column is
Down then Across complete, the printing begins on the second column.

Format Groups Will format groups with multiple columns using the details identified

with Multiple for the section.

Column

Table 8-4 Formatting - Layout Tab

Copyright ©2017 Pathfinder Learning, LLP

Lesson 8: Conditional Formatting 231

Exercise 8-1 Using the Section Expert to Create Page Breaks

In this exercise, you will create a report containing grouped information and will gain experience utilizing
some of the paging features.

1. From the Start Page, under the START A NEW REPORT banner, click Blank report.

2. From within the Database Expert, in the Available Data Sources section (left) expand the My
Connections folder.

3. Expand the Northwind 2008.mdb entry.

4. Expand the category labeled Tables.

5. Select the table named Orders and add the table to the list of Selected Tables.

6. Click OK.

7. Save the file with the name My Grouped Orders with Page Breaks.

8. Add the OrderID, CustomerID, and ShipAddress fields from the Orders tables to the Details section.

9. Group the records by the ShipCountry field.

a. Click Group Expert.
b. Click ShipCountry field and click greater than button.
c. Click OK.

10. Increase the height of the Group Header #1 section.

11. Move the field headings from the Page Header section to the Group Header #1 section. Place the field
headings below the Group #1: ShipCountry field.

12. Format the OrderID and CustomerID fields in the Group Header #1 section to remove the commas and
decimal places.

a. Right-click on the field and choose Format Field.
b. Click -1123.
c. Click OK.

13. Add a textbox in the Report Header.

a. Click the Insert Text Object button.
b. Place the cross cursor in the upper left corner of the report header section. Click where you want the

box to begin. Holding the mouse button, drag the box to size it appropriately.

14. Add the following text to the box you just created: Orders Shipped by Country.

Copyright ©2017 Pathfinder Learning, LLP

232 Lesson 8: Conditional Formatting
15. Verify the Design tab is similar to the image below.

Figure 8-3 Example of Design Section

16. Preview the report.
17. Verify your report is similar to the image below. As you page through the report, you will notice that the

report will print as many countries on a page as the page will accommodate. Suppose we want the report
to print each country on a separate page.

Figure 8-4 Report Page with Multiple Countries

18. Return to the Design tab.
19. Right-click on the Group Header #1: Orders.ShipCountry – A section and select Section Expert…
20. Click the Paging tab at the top of the Section Expert dialog box.
21. Click the New Page Before option.

Copyright ©2017 Pathfinder Learning, LLP

Lesson 8: Conditional Formatting 233

22. Click OK.

23. Preview the report. You will now observe that each country starts on a new page. Unfortunately, this
behavior has also been applied to the first country in the report. This negative side-effect can be
counteracted by virtue of a conditional format. This will be covered later in this chapter.

24. Save the My Grouped Orders with Page Breaks file.

25. Leave the file open for the next exercise.

Copyright ©2017 Pathfinder Learning, LLP

234 Lesson 8: Conditional Formatting

Formatting Sections with Conditional Logic

As seen in the previous chapters, it is of great benefit to be able to format objects in a report. There are times,
however, when formatting should only be applied when a certain situation arises.

In the prior exercise, we were presented with the ability to have the beginning of each country’s transactions begin
on a new blank page. The problem with that request was that there was no discrimination between the first country
to be printed and all of the remaining countries. An unnecessary page break was added to the first country resulting
in a blank first page of the report.

When working with the Format Editor (and other similar dialog boxes) it is not uncommon to invoke a feature by
checking a box. By checking a feature’s checkbox, we are assigning that feature regardless of any external
circumstances. By clearing the check from the feature’s checkbox, we are removing that feature. If a dialog box
control is manipulated via a dropdown list, selecting an item from the list applies that feature without regard to
outside influences.

Many of the controls in Crystal Reports have a formula button placed next to them. The formula button allows
the report designer to apply decision making logic to the control, thereby applying the feature only when necessary.

When conditional logic has been applied to a feature, the formula button will change from unassigned to
assigned . When the Formula Button is in the assigned mode, the state of the checkbox for that feature is
irrelevant. It does not matter if the feature is checked or unchecked, the logic is applied.

There are two types of controls that need to be examined that will dictate the strategy used when formulating the
conditional logic for a control.

 Checkboxes: Since a checkbox is a binary control, in other words, it is either on or off. The formula written
for a checkbox does not have to be posed in an IF…THEN…ELSE format. The only necessary portion of
the statement is the test. If the test evaluates to true, the control is activated. If the test evaluates to false, the
control is not activated. For example: Sales>1000 would be all that is required to format a number whose
value is greater than one thousand. All sales less than or equal to one thousand would not receive the format
instruction.

 Dropdowns and Text boxes: Because these controls may have more than one option selected, an
IF…THEN…ELSE statement is required to decide what to base a true action against and a false action
against. For example: If a user selects red or green do one thing, but if the user selects blue or orange do a
different thing.

Copyright ©2017 Pathfinder Learning, LLP

Lesson 8: Conditional Formatting 235

For this report, we want to instruct Crystal Reports to start each country on a new page but to not apply this request
to the first printed country. Crystal Reports’ Formula Workshop contains a function called OnFirstRecord. When
this function is used, the logic only applies to the first record in the report. The problem here is that we need to have
the exact opposite behavior. The Formula Workshop does not contain a counter function (like NotOnFirstRecord) so
we will flip the logic by nesting the OnFirstRecord function inside a Not function. This will have the effect of
applying the condition to every country except the first country.

Copyright ©2017 Pathfinder Learning, LLP

236 Lesson 8: Conditional Formatting

Exercise 8-2 Conditionally Formatting Sections
This exercise will allow you to practice conditionally formatting the report.

1. Continuing with the My Grouped Orders with Page Breaks file from the previous exercise.
2. Switch to the Design tab.
3. Right-click on the Group Header #1: Orders.ShipCountry – A section and select Section Expert…
4. Click the Paging tab at the top of the Section Expert dialog box.

5. Click the formula button to the right of the New Page Before checkbox control. The Formula
Workshop will display.

6. In the Operators tree, expand the Operators category.

7. Expand the Boolean category.

8. Double click Not (not x) to add this function to the formula construction window at the bottom. (The
word Not could simply have been typed, but it’s good to know where these functions are located in the
library.)

9. In the Functions tree, expand the Functions category.

10. Expand the Print State category.

11. Double-click the OnFirstRecord function to add this function to the formula construction window at the
bottom. This will appear after the Not function added in Step 8.

12. Click Save and Close.

13. Observe how the formula button’s icon has changed from blue to red .
14. Click OK.
15. Preview the report.

Copyright ©2017 Pathfinder Learning, LLP

Lesson 8: Conditional Formatting 237

16. Verify your report is similar to the image below. Observe how the first country in the report begins on
page one and all subsequent countries begin on their own pages.

Figure 8-5 Exercise 8-2 Results

17. Save the file as My Grouped Orders with Conditional Page Breaks.
18. Close the file.

Copyright ©2017 Pathfinder Learning, LLP

238 Lesson 8: Conditional Formatting

Suppressing Sections Conditionally

There are times when a section needs to be suppressed. Suppressing a section means to not display the section in the
finished report. As was demonstrated in a previous lesson, the Details section was hidden, allowing for a report
containing only summary information.

Figure 8-6 Summary Report with Hidden Details Section

Hiding vs. Suppressing
There is a major functional difference between hiding and suppressing. The end result is nearly the same,
but the differences need to be understood.

 Hiding: Prevents the section from being displayed but allows the items in the section to be
displayed upon request via a drill-down operation

 Suppressing: Prevents the section from being displayed.

Copyright ©2017 Pathfinder Learning, LLP

Lesson 8: Conditional Formatting 239

When a report contains multiple groups arranged in a hierarchy, and those groups are summarized, a peculiar
effect is achieved when a parent group does not contain child group entries. Observe the following image.

Figure 8-7 Unique Totals for Each Section

Notice that the three group summaries for City, Region, and Country are different. This is because there are
multiple cities in the region (WA) and multiple regions in the country (USA).

But what happens when a country has only a single region and that region has only a single city. Observe
the following image.

Figure 8-8 Identical Totals for Each Section

Copyright ©2017 Pathfinder Learning, LLP

240 Lesson 8: Conditional Formatting
All of the summaries appear to be the same. This can also occur when a country does not possess a regional
designation but does possess a city designation. Another oddity is the presence of a blank line between the
country and the city when no region is present, as in the following image.

Figure 8-9 Blank Area for Missing Region

To resolve these issues, a conditional suppression should be applied to the group header and group footer
that hides (suppresses) the section when a region is not present for the country.

Copyright ©2017 Pathfinder Learning, LLP

Lesson 8: Conditional Formatting 241

Exercise 8-3 Conditionally Suppressing Sections

In this exercise, you will create formulas that will allow you to control when sections are hidden or displayed
in a report.

1. Open the My Completed Orders with Groups and Summaries file (from Lesson 04).

2. Switch to the Design tab if necessary.

3. Remove the Hide attribute from the Details section by right-clicking on the Details section header and
selecting Show.

4. Preview the report to ensure all of the transactional records are displayed. Observe the blank line after
countries like Argentina and Austria.

5. Return to the Design tab.

6. Click the Section Expert button on the Experts toolbar.
7. In the left panel, select the Group Header #2: Orders.ShipRegion – A section.

8. Click the formula button next to Suppress (No Drill-Down).

9. In the Functions tree, expand the Functions category.

10. Expand the Summary category.

11. Expand the DistinctCount sub-category.

12. Double-click the DistinctCount (fld, condFld) function to add this function to the formula construction
window at the bottom. This version of the DistinctCount function requires two variables; fld is the field
to count and condFld is the field used to group the values in the fld variable.

13. Construct the following formula:

DistinctCount ({Orders.ShipRegion}, {Orders.ShipCountry}) < 2

This formula counts the number of unique regions for each country. If a country has less than two
regions, the formula generates a true response which is passed to the Suppress (No Drill-Down) control.
Because multiple customers may exist for a single region, the DistinctCount function ensures that
duplicate regions are not counted in the test.

14. Highlight the entire formula and press CTRL-C to copy the formula to computer memory. This same
formula will be used to conditionally suppress the companion footer section.

15. Click the button to return to the Section Expert.

Copyright ©2017 Pathfinder Learning, LLP

242 Lesson 8: Conditional Formatting

16. Select the Group Footer #2: Orders.ShipRegion – A section and then click the formula button
next to Suppress (No Drill-Down).

17. Paste (CTRL-V) the previously created formula into the formula construction window at the bottom of
the Formula Workshop window.

18. Click the button to return to the Section Expert.

19. Click OK to return to the Design tab.

20. Preview the report.

21. Verify your report is similar to the image below. Observe that the blank lines below countries with no
regions has been removed as well as repetitive regional totals in the summary areas.

Figure 8-10 Exercise 8-3 Results

22. Save the file as My Conditional Suppress.
23. Close the file.

Copyright ©2017 Pathfinder Learning, LLP

Lesson 8: Conditional Formatting 243

Formatting Fields Conditionally

In order to draw attention to important facts of a report. it is a common practice to change the font color, background
color, font size, or any number of other visual characteristics. By visually separating these facts from the rest of the
data, the user’s attention is drawn to these items.

Imagine a scenario where any percentage in the summary report that is greater than 10% requires further
investigation. If the report contains only a few entries, the task of identifying those that meet the criteria is relatively
simple. However, suppose the list is dozens or even hundreds of lines long. The time required to locate each entry
above 10% is unacceptable.

By applying conditional logic to a font attribute, like font color, we can have the report color these values instantly.

Copyright ©2017 Pathfinder Learning, LLP

244 Lesson 8: Conditional Formatting
Exercise 8-4 Conditionally Formatting Fields
A new report requirement is to show in red any percentage that is greater than 10%.

1. Open the My Sales Record file (from Lesson 06).
2. Open the Database Expert and add the Categories table to the Selected Tables list.
3. Click OK three times to accept the automatic linking and refresh the report.
4. Switch to the Design tab if necessary.
5. Remove the @Subtotal, @Discount, and @Total fields from the Details section.
6. In the Field Explorer, right-click on Formula Fields and choose New.
7. Enter LineTotal as the name. Click OK.
8. In the Formula Workshop, enter the following equation:

{OrderDetails.UnitPrice} * {OrderDetails.Quantity}
9. Click the Save and Close button.
10. Add the newly created LineTotal formula to the right side of the Details section.
11. Open the Group Expert and group the report by CategoryName from the Categories table.
12. Increase the height of the Group Header #1 section.
13. Move the field headings from the Page Header section to the Group Header #1 section. Place the field

headings below the Group #1: CategoryName field.
14. Click on the @LineTotal field. Click the Insert Summary button.
15. Verify the LineTotal field is in the field to summarize drop down.
16. Verify the Calculate this Summary field is set to Sum.
17. Change the Summary location to Group #1: Categories.CategoryName – A.

Copyright ©2017 Pathfinder Learning, LLP

Lesson 8: Conditional Formatting 245
18. Check the Show as a percentage of option.

Figure 8-11 Insert Summary Settings

19. Click OK.
20. Right-click the Details section and select Hide (Drill-Down OK).
21. Switch to the Preview tab to view the summarized report.
22. Right-click on any LineTotal percentage and select Format Field…

23. On the Font tab, click the formula button next to Color.

24. Construct the following function:

IF PercentOfSum ({@LineTotal}, {Categories.CategoryName}) > 10 THEN crRed

The PercentOfSum ({@LineTotal}, {Categories.CategoryName}) field can be added by double-
clicking on the corresponding entry in the Field Tree.

Because the Color control is a dropdown, an “IF…THEN” statement is required in the logic of the
conditional format. NOTE: All of the colors are located in the Function Tree in the Functions category,
Color Constants sub-category. If you type the color choice in the logic directly, you must include a “cr”
prefix in front of the desired color. If you double-click the color name from the Function Tree, the “cr”
prefix is automatically added.

25. Click the button to return to the Format Editor.

Copyright ©2017 Pathfinder Learning, LLP

246 Lesson 8: Conditional Formatting
26. Click OK to return to the Preview tab.
27. Verify your report is similar to the image below.

Figure 8-12 Exercise 8-4 Results

28. Save the file as My Sales Record with Color Summaries.
29. Leave the file open for the next exercise.

Copyright ©2017 Pathfinder Learning, LLP

Lesson 8: Conditional Formatting 247

Exercise 8-5 Conditionally Format a Background Fill Color
A new report requirement is to highlight all of the fields for any record where the LineTotal exceeds $750.

1. Continuing with the My Sales Record with Color Summaries file from the previous exercise, switch to
the Design tab.

2. Unhide the Details section (right-click on the Details section heading and choose Show.)

3. Draw a selection box surrounding the five fields in the Details section. If this is difficult to perform, an
alternative approach is to click once on one of the fields in the Details section, then while pressing the
CTRL key, select the remaining fields in the Details section.

4. Right-click on any selected field in the Details section and select .

5. On the Border tab, in the Color section click the formula button next to Background.

6. Construct the following function:

IF {@LineTotal} > 750 Then crYellow Else crNoColor

The above formula checks to see if the LineTotal for a given record is greater than 750. If the LineTotal
is greater than 750, a True is returned to the Background control and the fill color is set to Yellow.

Because the default background color is Black, an Else statement is necessary to remove the background
color from any record whose LineTotal is less than or equal to 750. This is the purpose of the crNoColor
parameter.

7. Click the button to return to the Format Editor.

8. Click OK to return to the Design tab.

9. Switch to the Preview tab to view the report with the highlighted items.

Copyright ©2017 Pathfinder Learning, LLP

248 Lesson 8: Conditional Formatting
10. Verify your report is similar to the image below.

Figure 8- 13 Exercise 8-5 Results

11. Save the file as My Sales Record with Highlighted Details.
12. Close the file.

Copyright ©2017 Pathfinder Learning, LLP

Lesson 8: Conditional Formatting 249

Conditionally Highlight a Single Field

Sometimes you may only need to highlight a single field, rather than the entire record. This can be done by
selecting the field to be conditionally highlighted and clicking the Highlighting Expert .

Using the Highlighting Expert allows you to add conditional formatting to all data types in the report. Fields
can be highlighted based on either a value in the field you are highlighting or a value in another report field.

Copyright ©2017 Pathfinder Learning, LLP

250 Lesson 8: Conditional Formatting
Exercise 8-6 Conditionally Highlighting One Field
When a supplier provides at least 3 products in a category, that number needs to be highlighted. This can be
easily done in Preview as well as in Design.

1. Browse to the location of the saved class files and open the folder named Exercise Files\Lesson 08.
2. Select the E8-6 My Highlighted Products by Category and Group and then click Open.
3. Click Design view.
4. In Group Footer 2, select the Count of Products.ProductName.
5. Click the Highlighting Expert .
6. Click the New button (bottom left side) to create a new rule.

a. The Value of drop down should remain as it is – showing this field (the correct field is selected so
no need to change this).

b. The drop down below that should be changed from is equal to to is greater than or equal to.
c. In the box below the drop downs, enter 3.
d. Change the background and font colors as you wish.
e. Click OK.

Figure 8- 14 Highlighting Expert

Copyright ©2017 Pathfinder Learning, LLP


Click to View FlipBook Version