SSRS Pull Variables Or Values From Sub Report Into Main Report

徘徊边缘 提交于 2019-12-01 15:08:47

The links in the question and comments sometimes refer to non-SSRS reports: the syntax [subreport].[Report]![MyFieldName] or [Reports]![YourReportName]![YourSubReportName]![TheValueFromTheSubReportYouWantToReference] are not used in SSRS. It is, however, used in designing MS Access reports, as ojeffrey points out in the discussion you link to.

There is no common method to access data in a subreport. The SSRS model is that parent report data is processed, subreport data is processed, the subreports are rendered, results go back to the parent, then parent is rendered, including the subreport as appropriate. The only data passed between the two is parameters are passed into the the subreport, and rendered output is passed back to the parent. You'll see the that data passed in from the parent must be as report parameters here: http://technet.microsoft.com/en-us/library/ms160348(v=sql.100).aspx

All parameters that are required by the subreport must be included in the Parameters list. If a required parameter is missing, the subreport is not displayed correctly in the main report.

For citing authoritative sources: This discussion sums it up:

No, referring to a report item in a subreport is not allowed.

But that is a bit old, there is also this more recent discussion of work-arounds, provided by Microsoft employee and a MS BI MVP:

You are going to need to replace the subreport item with a data region like list, table, or matrix to be able to get the proper reference you are looking for.

[Skipping down to another post]...

Now, it seems you want to calculate the difference between main report and the subreport. Also, because they have the different data source, so you cannot use nest table/matrix/list, right? If so, one workaround I can think of is pass parameter to the sub report and calculate the total/subtotal in sub report. I mean, create several hidden/ internal parameters, pass the values from main report to sub report through parameters and then calculate the total/subtotal there.

Jeroen's answer to the linked question point towards the direction I would go: use a "Shared Dataset" and enable caching if the dataset is slow to execute. The same dataset execution can then be used for the parent and subreports. This can change the use of parameters: they usually get moved from the SQL query to the filter of the Dataset in the report.

But with the Lookup function introduced in SSRS 2008R2, you can get very flexible with report level joins between datasets.

The details of how I'd design this depend a lot on how much other data needs to get passed back and forth, and how neatly the queries for the reports can be knit together.

Create variable in main report and update it in sub report so you can get value back to main report

ex: Create formula in main report with name {@Total} place flowing in it

WhilePrintingRecords;
shared Numbervar myTotal := 0;

NOTE : placing ; will not print value and without ; will print value in above example value will not be printed if you want to print value of formula just remove ; from second line ex

WhilePrintingRecords;
shared Numbervar myTotal := 0

now place {@Total} in report header of your main report now create second formula in sub report where you want to add subtotal to main report formula with name {@addTotal} place following lines in it

WhilePrintingRecords;
shared Numbervar myTotal;
myTotal := myTotal + 200; //or any formula or field

add this formula to place in sub report where you want to add value to total

now create formula in main report to show grand total with name {@showTotal} and place following lines in it

WhilePrintingRecords;
shared Numbervar myTotal;
myTotal

place {@showTotal} in your main report where you want to show this value in report but remember one thing you should place this formula after sub-report.

NOTE : to assign value to variable use := operator

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!