How to embed and call VB functions within a Reporting Services report
Part four of a four-part series of blogs

SQL Server Reporting Services allows you to write (and use) custom functions written in Visual Basic within a report. This blog considers the pros and cons, and shows how to do this.

  1. Embedding Code in Reporting Services
  2. Embedding Code in a Report to Show a Film's Status
  3. Truncating a Film's Description using a Function
  4. Writing VB Code in a Class Library (this blog)

Posted by Andy Brown on 13 July 2012

You need a minimum screen resolution of about 700 pixels width to see our blogs. This is because they contain diagrams and tables which would not be viewable easily on a mobile phone or small laptop. Please use a larger tablet, notebook or desktop computer, or change your screen resolution settings.

Writing VB Code in a Class Library

If you've read this far, you'll probably agree that writing custom functions in VB and pasting them into your report's embedded code dialog box can be a good idea.  You'll also probably agree that you need to find a better place to write your code!

For the method described on this page, you must have Visual Studio installed (and not just the Reporting Services component).  The screen shots for this page are taken from Visual Studio 2010.

Creating a Class Library

The easiest place to write code is in a class library (it's worth mentioning that you don't need to know what one of these is, although I will explain more when I get round to blogging about custom assemblies).  First create a new project:

Inserting a class library

Right-click on your solution in Solution Explorer, and choose to add a new project.

You can now choose what sort of project you're creating:

Creating a class library project - part 2

The numbered steps are shown in more detail below.

The steps to follow in the above dialog box are:

  1. Choose to use Visual Basic (there's no point writing code in any other language, since it won't work as embedded code!).
  2. Choose to create a class library.
  3. Choose what you want to call it and where you want to put it (or just accept the defaults - you don't much care, as all you're going to do is use this as a place to write code so that you can then select, copy and paste it).

You now have a class library!

Creating your Code

Visual Studio automatically creates (and opens) a file called Class1.vb - you can use this to start to write code:

Writing code in class in VB

You can start writing your functions here, and get the full .NET experience (comments, indentation, intellisense ...).


When you've finished, select your function and copy it to the clipboard:

Copying a VB function

Having written a function, select its text and copy it to the clipboard.

You can then paste this into the Report Properties dialog box (as described earlier in this blog):

Pasting into report properties dialog box

Initially it looks like you'll keep the colour-coding, but it vanishes when you select OK.

Recommended Next Step

If you've come this far, you might like to learn how to create a custom assembly instead, giving you more flexibility, the ability to code in C# and the ability to debug.

This blog has 0 threads Add post