Passing Parameter-Field Values


If you are importing an existing .RPT file into the RDC or using an external .RPT file, the report may contain parameter fields that are designed to prompt the user for information whenever the report is refreshed. If you simply leave the parameter fields as they are, the application will automatically prompt the user for them when the report runs. Obviously, one of the main reasons to integrate the report into a VB application is to more closely control the user interface of the report. You ll probably want to alter the mechanism for gathering this data from the user , and then have the application supply the appropriate values to the parameter fields from within the application code.

Because you can control both report record selection and report formulas at run time from within your VB application, there may not be as much necessity to use parameter fields with an integrated report as there might be for a stand-alone report created with regular Crystal Reports. However, if you are using RDC to share an external .RPT file with other Crystal Reports users who are not using your custom application, you may need to set the values of parameter fields from within your code.

In this situation, use the Report object s ParameterFieldDefinitions collection to manipulate the parameters. As with formulas, there is one ParameterFieldDefinition object in the collection for each parameter field defined in the report. And as with formulas, the proper member of the collection is retrieved with a one-based index determined by the order in which the parameter fields were added to the report. You can t retrieve a parameter field by using its name as the index (unless you use the looping technique or GetItemByName approaches discussed earlier in the chapter for use with formula fields).

The RDC approach for setting the value of a parameter field being passed to a report is to use the ParameterFieldDefinition object s AddCurrentValue method. This passes a single value to the report parameter field. Because a report can contain multiple-value parameter fields, you can execute the AddCurrentValue method as many times as necessary (for example, in a loop that looks through a list box for selected items) to populate a parameter field with several values.

Although the report included in this sample application (available at www.CrystalBook.com) uses a direct format of the details section during its Format event (discussed later in the chapter) to change section color , this is an example of where a parameter field could be used. If this were the desired approach for conditional formatting, the following code would correctly pass the value to the parameter field:

 'Set parameter value 
'Alternative method is to format Details section within VB
If txtHighlight = "" Then
Report.ParameterFields(1).AddCurrentValue (0)
Else
Report.ParameterFields(1).AddCurrentValue (Val(txtHighlight))
End If 'txtHighlight = ""

Here, the value is set to 0 if the text box is empty. This is important to note, because the actual parameter field in the report is numeric. Passing it an empty string might cause a run-time error when the report runs. Also, because the AddCurrentValue method is executed at least once, and a value has been passed to the parameter field, the user will no longer be prompted for it when the report is run.

Tip  

The RDC object model exposes all the features of Crystal Reports 10 parameter fields, such as range values, multiple values, and edit masks. Look in online developer s help to find the ParameterFieldDefinition Object entry. There, you ll find a complete description of the object, as well as all the methods available for manipulating parameter fields.




Crystal Reports 10
Crystal Reports 10: The Complete Reference
ISBN: B005DI80VA
EAN: N/A
Year: 2004
Pages: 223
Authors: George Peck

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net