Crystal Report Formulas
page 1 of 1
Published: 25 Oct 2003
Unedited - Community Contributed
Abstract
How to dynamically create Formulas in crystal reports .net
by Eric Landes
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 48654/ 40

Changing Values in a formula Field in Crystal .Net

One of the great aspects of programming in the .Net world is the robustness of the objects available. In particular I have found that manipulating a Crystal .Net report can be done in many ways with the Crystal.Engine namespace.

To demonstrate one way to manipulate objects in an already created report, I want to do a quick step through of changing a formula in a formula field. I have created a report with a formula field in the detail section that returns the value "Not December", or "December", if a date field month is December. This is the only formula field in the report, but there could be more, and the following code will show how to modify it.

First, I declared a variable as a formula field definition. I named the formula testFormula with the following formula:

if Month (CurrentDate)=12 then "December" else "Not December"

Next I create the report object, and then set the formula. While this demo only has one formula, code is provided that should work to allow you to find one formula in many.

Dim oRpt As New YourCrystalReport

oRpt.Load()

oRpt.SetDataSource(myDs)

Dim thisFormulaField As CrystalDecisions.CrystalReports.Engine.FormulaFieldDefinition

For Each thisFormulaField In oRpt.DataDefinition.FormulaFields

If thisFormulaField.FormulaName = "testFormula" Then

thisFormulaField.Text = "if Month (CurrentDate)=12 then " & _

"'December, Yes!' else 'December No.' "

End If

Next

CrystalReportViewer1.ReportSource = oRpt

This example creates a new instance of a report object that is in your Visual Studio project. There are other ways to do this, but that is for another topic. The report object is then loaded, and the data source is set to a dataset that I create in other code not shown here. Then, we loop through all the formulas in this report, looking for the formula name testformula. When it is found, using the text property, we set it to a new formula.

Feel free to play around with these formulas. One thing I haven't done yet is to add a formula programmatically to a report object. That may be a future article! If you have any questions or insights on this, or other articles, you can contact me at (remove dashes) corporatecoder-@-aspalliance.com

Eric Landes, your Corporate Coder

Crystal Resources

Crystal Alliance



User Comments

Title: Micropro   
Name: Bala
Date: 2005-07-27 3:01:58 AM
Comment:
Dear Eric Landes,

Thanks for your support to developers.

I tried and made some changed to make it work..

Dim oRpt As New YourCrystalReport

oRpt.Load()

oRpt.SetDataSource(myDs)

Dim thisFormulaField As CrystalDecisions.CrystalReports.Engine.FormulaFieldDefinition

For Each thisFormulaField In oRpt.DataDefinition.FormulaFields

If thisFormulaField.FormulaName = "{@testFormulaname}" Then

thisFormulaField.Text = "if Month (CurrentDate)=12 then " & _

"'December, Yes!' else 'December No.' "

End If

Next

CrystalReportViewer1.ReportSource = oRpt



Thanks and regards
Bala
Title: Thanks!   
Name: Usha
Date: 2005-07-21 6:23:20 AM
Comment:
Hi Eric,

Have just started coding in .NET and have found these forums so helpful. The code that ou have given above for Crystal Reports formula worked at the first shot! Thanks again!

Regards,
Usha
Title: please help for crystal formula.   
Name: Amit Kumar
Date: 2005-06-29 2:39:23 AM
Comment:
This is requested to all of u that i am new to crystal reports and i uses a different db for the crystal reports but just tell me that on which programming language these CR formula are based (is it PL/SQL or some other language)
Also, if some one have tutorials to guide me properly how to use these formula plz send it to me at shokeen_oct8@yahoo.com.
Thanks
Title: SherazSoft   
Name: Sheraz Ahmad
Date: 2004-09-19 4:01:27 AM
Comment:
visual basic and other

Product Spotlight
Product Spotlight 



Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2017 ASPAlliance.com  |  Page Processed at 2017-12-17 10:26:03 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search