Share via


Creating a Report with Parameters

To begin, create a report that draws its information from the sample Microsoft Access database that ships with Crystal Reports.

Note

Xtreme.mdb is the sample database that is provided with Crystal Reports. To locate xtreme.mdb on your hard drive for your version of Crystal Reports, see Location of Xtreme Sample Database. You need to connect to the database through its ODBC DSN entry. To learn the name of this entry for your version of Crystal Reports, see ODBC DSN Entry for Xtreme Sample Database.

To create a report with parameters

  1. In Solution Explorer, right-click the project name that is in bold type, point to Add, and then click Add New Item.

  2. In the Add New Item dialog box, in the Templates view, select the Crystal Report template.

  3. In the Name field, enter the name "CustomersByCity.rpt" and click Add.

  4. In the Create New Crystal Report Document panel of the Crystal Reports Gallery dialog box, select Using a Report Wizard.

  5. In the Choose an Expert panel, select Standard, and then click OK.

  6. In the Available Data Sources panel of the Standard Report Creation Wizard window, expand the Create New Connection folder.

  7. From the subfolder that opens, expand the ODBC (RDO) folder.

  8. In the ODBC (RDO) window, select the correct ODBC DSN entry for your version of Crystal Reports as explained in ODBC DSN Entry for Xtreme Sample Database, and then click Finish.

  9. Expand the Tables node, double-click the Customer table to move the table to the Selected Tables panel, and then click Next.

  10. Expand the Customer table, then CTRL-click Customer Name, Contact Title, Address1, Contact Last Name and City.

  11. Click the > symbol to move these fields into the Fields to Display panel, then click the Next button.

  12. In the Available Fields panel, under Report Fields, double-click Customer.City to move the field into the Group By panel, and then click the Finish button.

The CustomersByCity report is created and loaded into the main window of Visual Studio.

To add a City parameter

You are now ready to add a parameter named City and populate it with default values.

The Field Explorer must be visible, because it provides access to the various features of the report, including parameters. To make the Field Explorer visible, from the Crystal Reports menu, click Field Explorer.

  1. If the Field Explorer is not visible, on the Crystal Reports toolbar, click Toggle Field View.
> [!NOTE]
> <P>Another way to display the Field Explorer is to go to the Crystal Reports menu, and then click Field Explorer.</P>
  1. In the Field Explorer, right-click Parameter Fields and select New...

  2. In the Create Parameter Field dialog box:

1.  Set the Name to "City."
2.  Set the Prompting Text to "Select one or more cities."
3.  Set Value Type to String.
4.  Select Allow multiple values.
5.  Select Discrete value(s).
6.  Click Default Values...
  1. In the Set Default Values dialog box:
1.  Set the Browse table to "Customer."
2.  Set the Browse field to "City."
3.  Click \>\> (the double right arrow) to move the entire cities list into the Default Values list.
  1. Click OK to close the Set Default Values dialog box.

  2. Click OK to close the Create Parameter Field dialog box.

To connect the City parameter to the City database column

You have just set the Default Values to contain a large list of cities. Later in this tutorial, you access this same list of default values programmatically, through the ParameterFieldInfo property of the CrystalReportViewer class.

You now use the Select Expert to set a formula that connects the City database column to your newly created City parameter field.

  1. On the Crystal Reports toolbar, click Select Expert.

  2. In the Choose Field dialog box, under Report Fields, select Customer.City, and then click OK.

  3. In the Select Expert dialog box, within the Customer.City tab, set the dropdown list to "is equal to."

  4. In the new dropdown list that appears to the right, select the first choice in the list, {?City}, and then click OK.

> [!NOTE]
> <P>This selection, {?City}, is the City parameter that you created earlier.</P>
  1. From the File menu, select Save All.

You are now ready to bind the report to the CrystalReportViewer control and set the city parameter with two initial values, Paris and Tokyo.