SSRS code variable resetting on new page

旧城冷巷雨未停 提交于 2019-12-19 00:57:28

问题


In SSRS 2008 I am trying to maintain a SUM of SUMs on a group using custom Code. The reason is that I have a table of data, grouped and returning SUMs of the data. I have a filter on the group to remove lines where group sums are zero. Everything works except I'm running into problems with the group totals - it should be summing the visible group totals but is instead summing the entire dataset. There's tons of articles about how to work around this, usually using custom code. I've made custom functions and variables to maintain a counter:

Public Dim GroupMedTotal as Integer
Public Dim GrandMedTotal as Integer

Public Function CalcMedTotal(ThisValue as Integer) as Integer
    GroupMedTotal = GroupMedTotal + ThisValue
    GrandMedTotal = GrandMedTotal + ThisValue
    Return ThisValue
End Function

Public Function ReturnMedSubtotal() as Integer
    Dim ThisValue as Integer = GroupMedTotal
    GroupMedTotal = 0
    Return ThisValue
End Function

Basically CalcMedTotal is fed a SUM of a group, and maintains a running total of that sum. Then in the group total line I output ReturnMedSubtotal which is supposed to give me the accumulated total and reset it for the next group. This actually works great, EXCEPT - it is resetting the GroupMedTotal value on each page break. I don't have page breaks explicitly set, it's just the natural break in the SSRS viewer. And if I export the results to Excel everything works and looks correctly.

If I output Code.GroupMedTotal on each group row, I see it count correctly, and then if a group spans multiple pages on the next page GroupMedTotal is reset and begins counting from zero again.

Any help in what's going on or how to work around this? Thanks!


回答1:


Finally found the solution myself. Here it is, add Shared to the variable declarations:

Public Shared Dim GroupMedTotal as Integer
Public Shared Dim GrandMedTotal as Integer



回答2:


Just changing the variables to shared won't work. If you set them to shared they'll be DOUBLED when you export to PDF / XLS / etc (because it just kept adding to the existing var). You have to do something like this:

Public Shared Dim grandTotal as Decimal
Public Shared Dim costCenterTotal as Decimal
Public Shared Dim workerTotal as Decimal

Public Shared Function Initialize() 
    grandTotal = 0
    costCenterTotal = 0
    workerTotal = 0
End Function

Public Function AddTotal(ByVal b AS Decimal) AS Decimal
    grandTotal = grandTotal + b
    costCenterTotal = costCenterTotal + b
    workerTotal = workerTotal  + b
    return b
End Function

Public Function GetWorkerTotal()
    Dim ret as Decimal = workerTotal
    workerTotal = 0
    return ret
End Function 

Public Function GetCostCenterTotal()
    Dim ret as Decimal = costCenterTotal 
    costCenterTotal = 0
    return ret
End Function 

Public Function GetGrandTotal()
    Dim ret as Decimal = grandTotal
    grandTotal= 0
    return ret
End Function 



回答3:


I don't know where do you use this. but in your case, if I were you, I just use simple expression to check visibility of SUM

for example I'd use Right Click On Sum Box \ Select Expression \ then use IIF(SUM <> 0, sum. "")

It worked on every where and wont reset, in your case you have a Region and your code will reset in every region so you willface with serios isses if you don't change your way.



来源:https://stackoverflow.com/questions/2784473/ssrs-code-variable-resetting-on-new-page

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