Crystal Reports Online Training

Learn Online, Anytime, Anywhere

Step-by-step online tutorials.

20.17 Modifying Formulas

Modifying Formulas

The concepts that apply to modifying parameters are the same concepts for modifying formulas. RAS uses a similar object model for making changes to a formula. Rather than use the ParameterFieldController class, you use the FormulaFieldController. Listing 20-15 is the complete code listing.

Listing 20-15. Modifying the value of a formula.
[VB.NET]
Public Shared Sub SetFormula(ByVal FormulaName As String, ByVal FormulaText As String, _
ByVal rcd As CrystalDecisions.ReportAppServer.ClientDoc.ISCDReportClientDocument)
Dim OldFormula As CrystalDecisions.ReportAppServer.DataDefModel.FormulaField, _
NewFormula As CrystalDecisions.ReportAppServer.DataDefModel.FormulaField
'Get a reference to the existing formula
Dim FormulaIndex As Integer
FormulaIndex = rcd.DataDefinition.FormulaFields.Find(FormulaName, _
CrystalDecisions.ReportAppServer.DataDefModel.CrFieldDisplayNameTypeEnum.crFieldDisplayNameName, _
CrystalDecisions.ReportAppServer.DataDefModel.CeLocale.ceLocaleUserDefault)
OldFormula = DirectCast(rcd.DataDefinition.FormulaFields(FormulaIndex), _
CrystalDecisions.ReportAppServer.DataDefModel.FormulaField)
'Create the new formula and copy the old formula into it
NewFormula = New CrystalDecisions.ReportAppServer.DataDefModel.FormulaField()
OldFormula.CopyTo(NewFormula, True)
'Set the new formula text
NewFormula.Text = FormulaText
rcd.DataDefController.FormulaFieldController.Modify(OldFormula, NewFormula)
End Sub
[C#]
public static void SetFormula(string FormulaName, string FormulaText,
CrystalDecisions.ReportAppServer.ClientDoc.ISCDReportClientDocument rcd)
{
CrystalDecisions.ReportAppServer.DataDefModel.FormulaField OldFormula, NewFormula;
//Get a reference to the existing formula
int FormulaIndex;
FormulaIndex = rcd.DataDefinition.FormulaFields.Find(FormulaName,
CrystalDecisions.ReportAppServer.DataDefModel.CrFieldDisplayNameTypeEnum.crFieldDisplayNameName,
CrystalDecisions.ReportAppServer.DataDefModel.CeLocale.ceLocaleUserDefault);
OldFormula = (CrystalDecisions.ReportAppServer.DataDefModel.FormulaField)rcd.DataDefinition.FormulaFields[FormulaIndex];
//Create the new formula and copy the old formula into it
NewFormula = new CrystalDecisions.ReportAppServer.DataDefModel.FormulaField();
OldFormula.CopyTo(NewFormula, true);
//Set the new formula text
NewFormula.Text = FormulaText;
rcd.DataDefController.FormulaFieldController.Modify(OldFormula, NewFormula);
}