Crystal Reports Online Training

Learn Online, Anytime, Anywhere

Step-by-step online tutorials.

5.02 Setting the Default Values

Setting the Default Values

Default values give the user a list of values to choose from, which saves the user from the chore of memorizing all the available values that could be entered. Default values also restrict what can be entered. This prevents a user from making a typographical error.

One drawback of using default values is that they create additional overhead and increase the risk of error because it goes against the methodology of Object Oriented Programming (OOP). The goal of writing applications using OOP techniques is to consolidate all the information about a business object within its respective classes. This gives you encapsulation and makes it easier to maintain your code. Setting default values within a report breaks this rule because you are storing information about an object outside of its classes. In fact, this data is being stored in a separate file entirely. This requires additional documentation stating that all rules regarding an object’s default values must also be replicated to the related reports. If there is a bug in the report, both the report and the business objects would have to be debugged. This creates additional work during testing.

The best method is to not set default values within the report; instead use the .NET application to maintain this. Since the .NET front end is used to getting input from the user before printing the report, logic can be added here to pre-populate the front end with the appropriate default values that are derived from the class.

Of course, not everyone is going to write fully compliant OOP applications, nor will every report warrant the extra time required to fully integrate it into the .NET application. That being the case, this chapter explains how to create and use default parameters within a report.

Default values are added by clicking on the Set Default Values button on the Create Parameter dialog box. This brings up the Set Default Values dialog box. As Figure 5-4 shows, this is a fairly complex dialog box. This is because it allows the entry of every possible combination of values for each parameter type. That’s a lot of combinations!



Figure 5-4. The Set Default Values dialog box for string data types.

It was mentioned earlier that default values can be designed so that the user is presented with a listbox showing all the possible values. The Set Default Values dialog box is where this is done. Within this dialog box, one can create either a single default value or a list of default values. If a single default value, the input box is pre-populated with that value. If you create a list of default values, the user, when entering a value for the particular parameter, can select one of those values from a combobox.

The middle of the dialog box is the central part of setting default values. The left side is the place to enter a default value in the textbox. Click on the right arrow to move it to the list of possible default values. There is no limit to the number of values added to the right listbox.

If entering a lot of default values, an alternative to manually typing all the default values by hand, is to pull them from existing tables. This neat trick is performed by selecting from the top two dropdown boxes, a table and a field to get the values from. Once the two items are selected, the values are pulled from the field and put into the listbox on the left. Clicking on the arrow keys allows one to select and transfer to the default value list any number of these items. Alternately, to import a list of default values from a text file, click on the Import Pick List button.

The options to pull a list of values from a table or from a text file are better served by creating your own interface using .NET. Both of these options are just a mediocre attempt at creating a data-bound combobox. In Crystal Reports .NET, there is no way to create a data-bound control for parameter values. Presenting a static list of values from a table or text file is the next best option. Using this technique results in the problem that if your database were updated, you would have to come back to this report and update the list of possible values. But with .NET report integration, you can provide the user with a true data-bound combobox on a form in your application. This is a more practical solution to implement and it insures that the user always has a current list of values to choose from.

There are two options that are unique to string values. Refer again to Figure 5-4. You can limit the length of a string size and set the edit mask in the lower left corner of the Set Default Values dialog box. Limiting the length of the string allows you to set the minimum length and the maximum length. However, these options do not exist if using parameters with a numeric data type. Instead, set a valid range by entering minimum and maximum values.

The bottom right corner of the dialog box is the place to go for setting a description for each value, as well as the sorting order. This is similar to the functionality of a data-bound combobox because of the option to show a description rather then the actual value. This is useful for having the user select a table’s primary key by clicking on the description rather than the numeric ID. The values can be sorted in ascending, descending and natural order.

Closing the Set Default Value dialog box brings back the Create Parameter Field dialog box. Now that the parameter has a stored default value, the checkbox Allow Editing of Default Values is no longer grayed out (it is checked by default). When this is checked, the user can enter a new value in addition to selecting an item in the list of default values. Un-checking this option requires the user to select an item from the list of values, which prevents him or her from entering an invalid value.