Crystal Reports Online Training

Learn Online, Anytime, Anywhere

Step-by-step online tutorials.

15.01 Modifying the Record Selection

This chapter builds on the basic report customization shown in Chapter 14. It focuses on working with the DataDefinition and ReportDefinition classes. The DataDefinition class controls Record selection, Grouping and Sorting, and Parameters/Formulas.  The ReportDefinition class handles the functionality for report sections, grouping/sorting and running totals. With respect to the number of objects contained within it, an object variable of the ReportDefinition type is the largest report object by far. It manages the collection objects for all the areas, sections, and report objects within a report.

If you learn best by seeing sample code, this chapter is for you!

Modifying the Record Selection

There are two types of record selection formulas. The first type selects records based upon data that is available during the first-pass stage. This consists of raw data and fields/formulas that don’t use summary information. The second type of formula is a group selection formula that is based on second-pass data (e.g. summary fields and subtotals). Selecting records during the first-pass is done with the RecordsSelectionFormula property. Figure 15-1 shows the properties that modify the record selection formulas.





Figure 15-1. Properties used in selecting records.

The record selection formula is modified in two places: the ReportDocument class and the DataDefinition class. The properties are identical and you can use either class. Setting the group selection is done with the GroupSelectionFormula and it is only found in the DataDefinition class.

The CrystalReportViewer class has a SelectionFormula property that is equivalent to the RecordSelectionFormula property. The viewer doesn’t have a property for setting the group selection formula.

Modifying the record selection formulas is one of the easiest runtime changes to make to a report. The selection formula is a string and all you have to do is assign a new string to it. The formula syntax must be Crystal syntax. Basic syntax isn’t allowed for selection formulas. The following code listings set the formulas to filter on records that are within a certain date range. The listings demonstrate making the change with the DataDefinition class as well as the CrystalReportViewer class.

Listing 15-1. Changing the selection formula via the ReportDocument.
Dim myReport As New CrystalReport1()
myReport.DataDefinition.RecordSelectionFormula = _
"{Orders.Order Date} in Date (1996, 02, 19) to Date (1996, 03, 28)"
CrystalReportViewer1.ReportSource = myReport
Listing 15-2. Changing the selection formula with the viewer control.
Dim myReport As New CrystalReport1()
CrystalReportViewer1.ReportSource = myReport
CrystalReportViewer1.SelectionFormula = _
“{Orders.Order Date} in Date (1996, 02, 19) to Date (1996, 03, 28)”