Crystal Reports Online Training

Learn Online, Anytime, Anywhere

Step-by-step online tutorials.

4.23 Printing Paramter Values

Printing Parameter Values

The second method of using parameters is displaying their value on a report. There are various reasons for displaying parameters on a report. A very common reporting requirement is that when filtering data, it is helpful to print the filter criteria so that anyone reading the report knows that only a subset of all the available data was printed. For example, if a report prints records within a certain date, then you could show the date range within the report header. You can also use a parameter strictly for the purpose of displaying it on the report. For example, printing an invoice could use a parameter to let the user enter a note at the bottom of the invoice to alert the reader of new shipping requirements or tell them about an upcoming price increase.

The way you print the parameter on the report is dependent on the type of parameter it is. Printing a discrete parameter on a report is simple because a parameter is treated the same as any other field. Simply drag and drop it from the Field Explorer onto the report and it will be printed. Printing range and multi-value parameters is more complex because they are represented internally by more than one value. You have to use formulas to tell the report how to display the data. Each parameter type requires a different formula for displaying its information. The rest of this section looks at each parameter type and shows you how to print its value on the report.

Range parameters have a start value and end value. Crystal Reports has two functions for working with range parameters: Minimum() and Maximum(). When you pass a range parameter to these two functions, they return the beginning and ending values respectively. Here is an example of using these functions in a formula:

“This report covers the dates from ” & Minimum({?DateRange}) & ” to ” & Maximum({?DateRange})

This formula concatenates the functions Minimum() and Maximum()within a string telling the user the selected date range.

If the parameter is a String data type and can have multiple values, you can use the Join() function to combine them into a single string with a separator. The following code uses the {?Names} parameter to create a comma separated list of user names. Note that the Join() function only works if the parameter is a string data type, not numbers or dates.

Join({?Names}, “,”);

If you have a more complex parameter that can include multiple values or a range, then more complex programming logic is required. This is because each element in the parameter can have different printing requirements. To print a multi-value parameter, you need to loop through each item in the array and print it out. I built a sample formula for displaying all the multi-value parameter values and separate each one with a comma. This Crystal syntax formula prints the output in a very basic format and you will probably want to modify it to make it specific to your needs.

NumberVar Index;
StringVar Output;
StringVar LowerValue; StringVar UpperValue;
for Index := 1 to UBound({?Country Range}) Do
(
//Add a comma to separate values
If Output <> “” Then
Output := Output & “, “;
LowerValue := “”;
UpperValue := “”;
//Get the upper and lower values
If HasLowerBound ({?Country Range}[Index]) Then
LowerValue := Minimum({?Country Range}[Index]);
If HasUpperBound ({?Country Range}[Index]) Then
UpperValue := Maximum({?Country Range}[Index]);
//Discrete values have the same upper and lower bound
If LowerValue = UpperValue Then
Output := Output & LowerValue
Else
//Print the range values
(If LowerValue <> “” Then
Output := Output + ” From ” & LowerValue;
If UpperValue <> “” Then
Output := Output + ” To ” & UpperValue;)
);
//Clean up the Output string
Output := “The valid values are: ” & Output;

The formula starts out by declaring the necessary variables. Then it uses a For Loop to cycle through each item in the array. Within the For Loop, it builds the Output string. The first task is to check if the Output string has a value from a previous iteration of the loop and if so append a comma to the end.

The LowerValue and UpperValue variables are populated based upon whether the parameter value has an upper and lower bound. It uses the HasLowerBound() and HasUpperBound() functions to determine this.

Lastly, it concatenates the values to the Output string. If the lower and upper bound values are the same, then this is a discrete parameter value. If they are different, then it is a range parameter value and we want to display the minimum and maximum values.

The For Loop performs this logic for all values in the parameter. An example of the final output is:

The valid values are: Argentena To Australia, Canada

Using Parameters in Formulas

The third method of using parameters is within formulas. You’ve already seen examples of this throughout the chapter by modifying the records selection formulas. This can also be used with formulas for conditional formatting or for performing calculations.