Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SSRS code variable resetting on new page

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!

like image 749
Peter Tirrell Avatar asked May 06 '10 21:05

Peter Tirrell


2 Answers

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
like image 95
John Strickler Avatar answered Nov 14 '22 17:11

John Strickler


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 
like image 45
Scott R. Frost Avatar answered Nov 14 '22 16:11

Scott R. Frost