VB.NET Crystal Reports Formula Fields

All Crystal Reports programming samples in this tutorials is based on the following database (crystaldb) . Please take a look at the database structure before you start this tutorial - Click here to see Database Structure .

In this tutorial we are adding a Formula Field in existing Crystal Reports .

SITUATIONS :

If you have a Crystal Reports with Qty and Price , you need an additional field in your Crystal Reports for the Total of QTY X PRICE . In this situation you have to use the |BLBS^ |Formula Field|BLBE^ | in Crystal Reports.

In this tutorial we are showing the all orders with qty and price and the total of each row , that means each in each row we are showing the total of qty and price. Before starting this tutorial.

Create a new Crystal Reports with fields CustomerName , Order Date , Product Name and Product Price . If you do not know how to create this report , just look the previous tutorial |HREF^Crystal Report from multiple tables| . In that report selecting only four fields , here we need one more field Prodcut->Price .

After you create the Crystal Reports you screen is look like the following picture :

vb.net_crystal_report_formula_field_1.GIF

Next is to create the a Formula Field for showing the total of Qty and Price .

Right Click Formula Field in the Field Explorer and click New. Then you will get an Input Message Box , type Total in textbox and click Use Editor.

vb.net_crystal_report_formula_field_2.GIF Now you can see Formula Editor screen . Now you can enter which formula you want . Here we want the result of Qty X Price . For that we select OrderDetails.Qty , the multiplication operator and Product.Price . Double click each field for selection. vb.net_crystal_report_formula_field_3.GIF

Now you can see Total Under the Formula Field . Drag the field in to the Crystal Reports where ever you want.

vb.net_crystal_report_formula_field_4.GIF Now the designing part is over . Select the default form (Form1.vb) you created in VB.NET and drag a button and CrystalReportViewer control to your form.

Select Form's source code view and import the following :

Imports CrystalDecisions.CrystalReports.Engine

Put the following source code in the button click event






Imports CrystalDecisions.CrystalReports.Engine
Imports CrystalDecisions.Shared
Public Class Form1
    Private Sub Button1_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles Button1.Click
        Dim cryRpt As New ReportDocument
        cryRpt.Load("PUT CRYSTAL REPORT PATH HERE\CrystalReport1.rpt")
        CrystalReportViewer1.ReportSource = cryRpt
        CrystalReportViewer1.Refresh()
    End Sub
End Class

NOTES:
cryRpt.Load("PUT CRYSTAL REPORT PATH HERE\CrystalReport1.rpt")

The Crystal Report is in your project location, there you can see CrystalReport1.rpt . So give the full path name of report here.

When you run this program you will get the following screen.

vb.net_crystal_report_formula_field_5.GIF