Embedded Code in SQL Server Reporting Services
Sometimes it might be necessary to use the same expression on different places throughout a report. You can of course just copy and paste the expression every time you need it, but if you’ll ever need to change this expression, you will have to change it for every field you’re using it for. Therefore it’s good practice to write this expression just once and refer to it where necessary. This can be done by implementing embedded code, that can be entered through the report’s property dialog on the Code tab.
(Click “Report”, “Report Properties” and select the Code tab)
Assume you have some calculated percentage fields and you need to set the background color depending on it’s value:
- when the value is less then 20% : background must be red
- when the value is between 20 and 80%: background must be orange
- when the value is greater then 80%: background must be green
Add the following function to the custom code textbox:
Function GetColor(ByVal percentage As Double) As String
Dim returnValue As String
Select Case percentage
Case Is < 20
returnValue = “red”
Case Is < 80
returnValue = “orange”
Case Is > 80
returnValue = “green”
End Select
Return returnValue
End Function
Now you can access this function as a member of the class called “Code”:
=Code.GetColor(Fields!CalculatedPercentage.Value)
But at this time, Embedded Code only supports Visual Basic and the Embedded Code Window is nothing more then a large textbox, without Intellisense or any debugging info.
Embedded Code can also only be reached from within the same report.