Crystal Reports Online Training

Learn Online, Anytime, Anywhere

Step-by-step online tutorials.

12.09 Unlinked with Shared Variables

Unlinked with Shared Variables

Shared variables can be used to pass data between the main report and the subreport. The difference between using a parameter field and a shared variable is that shared variables can be used to pass data in both directions. When using parameter fields, data can only be passed from the main report to the subreport.

Since the subreport is not linked to the main report and no formulas are being passed to the subreport, you shouldn’t set any properties on this dialog box. The difference is that both reports have to have a formula that declares and uses the shared variable.

To illustrate how this works, let’s modify the example report shown earlier that lists the customer sales and any credits issued to the customer. The report is also going to show the net amount of adding the total sales with the total credits issued. This revised report is shown in Figure 12-5.



Figure 12-5. Calculate net sales amount using a subreport.

Since the sales amount is on the main report and the credits are listed on the subreport, it needs to use a shared variable so that the two reports can share their data. A formula is added to the subreport to calculate the total credit amount for the customer. The formula is placed in the report header of the subreport so that it gets calculated when the report is first run. Note: these formulas use Basic syntax.

Shared TotalCredits as Currency
TotalCredits = Sum({Credits.Amount})
Formula = TotalCredits

The main report is modified so that it has a formula that declares the same shared variable and uses it in the calculation. If the main report didn’t declare the variable as shared, it would always be zero. Notice in the formula that the TotalCredits variable is being added to the sum of the order amounts. It isn’t being subtracted because it is already a negative number.

Shared TotalCredits as Currency
Formula = Sum({Orders.Order Amount}, {Customer.Customer ID}) + TotalCredits

A variation of using shared variables with subreports is to use a subreport to perform a particular calculation but not show the subreport on the report. The last example used a subreport to display and calculate the total credits given to a customer. However, you could have chosen to not show the details of the credits on the report and instead just use the total amount in the formula. If you try to do this by either hiding or suppressing the subreport or the section it is in, the subreport won’t calculate the shared variable. This is because the subreport has to be printed in order for the formulas on the subreport to be calculated. One alternative is to make the subreport object very small so that it isn’t visible on the report. Depending upon how the subreport is designed, this may or may not print any extraneous graphics on the page. A better alternative is to modify the subreport object in your .NET application so that its height is set to 0 and the object isn’t allowed to grow. The designer doesn’t let you set the height to 0 but setting it via code gets around this limitation.

Dim rpt As CrystalDecisions.CrystalReports.Engine.ReportDocument
Dim rptObjects As CrystalDecisions.CrystalReports.Engine.ReportObjects
rpt = New SuppressSubreportDemo()
rptObjects = rpt.ReportDefinition.ReportObjects
rptObjects.Item("Subreport").ObjectFormat.EnableCanGrow = False
rptObjects.Item("Subreport").Height = 0
CrystalReportViewer1.ReportSource = rpt

Question: I’m using shared variables to pass data from the subreport back to the main report. But if the current record in the main report doesn’t have data in the subreport, the shared variable’s value doesn’t change. It is the same as the previous record.

Answer: This is a common problem when you don’t reset the shared variable back to zero (or whatever default value you want to use). Put the following Basic syntax formula in the Report Header section of your subreport (and remember to enter the proper variable name).

WhilePrintingRecords
Shared YourVariable As Number
YourVariable = 0
Formula = 0