Crystal Reports Online Training

Learn Online, Anytime, Anywhere

Step-by-step online tutorials.

20.13 Adding Formulas and Parameters

Adding Formulas and Parameters

After going through the examples for adding database fields and special fields to a report, you should be an expert at how the code works to add objects to a report. Of course, you will most likely want to add formulas and parameters to your report. So let’s just look at the general rules for adding formulas and parameters to the report and leave it to you to make the necessary changes.

Adding formulas and parameters to a report uses the similar code as the AddDatabaseField() method, but with two exceptions. The two properties, FieldValueType and DataSource have to be customized.

The FieldValueType has to be set so that Crystal Reports knows what type of data to display. For example, you need to tell it whether it is a string, number, date, etc. It uses the enumerated data type CrystalDecisions.ReportAppServer.DataDefModel.CrFieldValueTypeEnum to specify the field type. You can use intellisense to browse the complete list of data types available.

The DataSource property is assigned the name of the formula or parameter. It requires that you use proper Crystal syntax and include the curly brackets and full name. The following code snippet shows how to set the properties for a string formula and a numeric parameter.

'Properties for a string formula
myFieldObject.FieldValueType = CrystalDecisions.ReportAppServer.DataDefModel.CrFieldValueTypeEnum.crFieldValueTypeStringField
myFieldObject.DataSource = "{@SampleFormula}"
'Properties for a number parameter
myFieldObject.FieldValueType = CrystalDecisions.ReportAppServer.DataDefModel.CrFieldValueTypeEnum.crFieldValueTypeNumberField
myFieldObject.DataSource = "{?SampleParameter{"

You can use the code for adding a database field to a report and replace it with either of the two samples above to add a formula or parameter.