Embedded Code: what''s the trick?

  • I am trying to put some embedded Visual Basic code into my report, using the "Code" section in the report properties.

    Here's the function I'm trying to use:

    Public Shared Function ReportTotal(ByVal IsUnitCost As Boolean,ByVal  TotalDirectCosts As Double, ByVal SalaryBenefitsTotal As Double, ByVal IndirectRate As Double, ByVal SS_Screened As Integer, ByVal UnitRate As Double) As Double

        If IsUnitCost Then

            Return SS_Screened * UnitRate

        Else

            Return TotalDirectCosts + SalaryBenefitsTotal * IndirectRate

        End If

    End Function

    Supposedly, after entering this you can access it in the expression for a field by typing the following: =Code.ReportTotal(... parameters ...)

    However, when I try this, "ReportTalk" does not show up in the intellisense after I type "Code.".  If I type it in anyway, a red squiggly line shows up under it, and if I attempt to preview the report, I get the following error: "The definition of report [report name] is invalid. Exception of type 'Microsoft.ReportingServices.ReportProcessing.ReportProcessingException' was thrown."

    This is not a very helpful error message!  I tried with and without "Public" and "Shared" and got the same thing.

    What am I doing wrong?

    Cynthia

  • Just a stab in the dark, but try removing 'Public Shared' and just go with 'Function ReportTotal....

  • I already tried that -- that's how I originally had it, and I added the Public and Shared to see if it made a difference (it didn't).

  • Have you tried adding the ".Value" to the end of the expressions?

  • I copied and pasted you function into the code window and tested it using RS 2000 and 2005.  Worked in both.  I just called the function using =Code.ReportTotal(true,1,1,1,2,2) to verify it worked.

    Are you still having problems with this?

  • Generally, when I have seen this issue in the past, it turns out to be an incorrect data type being passed to the function or a missing parameter. If David's suggestion works, I would venture to guess that this is the issue. If this is the issue, you have a couple of options. Pass everything as a string and convert in the function or convert each parameter on the way in. Although sloppy, both ways works.

  • Is this embedded Visual Basic (eVB)? Not VB6 or VB.NET? Are you attempting to run it on a Pocket PC device?

    eVB is for programming on handheld devices or PDA's. I'm not aware of any ability for it to run in any other environment, except emulators of course.

  • I discovered the problem -- actually, someone on the MSDN forums enlightened me to it.

    Apparently, the code does not get compiled until the report is deployed.  I had not deployed it -- I was just testing it out in "preview" mode.  Since it failed there, I assumed something was wrong, and I never bothered to deploy it.

    When I deploy it, the code does work correctly.

    Sheesh -- you'd think that might be mentioned somewhere in the documentation!

    Thank everyone for your input!

  • I'm having a similar problem. I can get values to return from my function, but when I add logic for an array, I'm either not getting anything, or getting an error saying the index is out of range. Here is the function and the call I'm using in the Report Properties/ Code tab:

    (I've tried deploying to see if it was the issue with not being compiled, but I'm getting the same responese). Anyone have some ideas??

    =Code.GetLotCode(Fields!gpitm.Value,Split(Parameters!item.Value,","),Split(Parameters!LotCode.Value,","))

    Public Shared Function GetLotCode(ByVal Item as string, ByVal ItemArray() as string, ByVal LotCodeArray() as String) as String

    Dim J as integer

    J = 0

    do while ItemArray(J) <> Item

    J += 1

    loop

    return LotCodeArray(J)

    End Function

    Thanks for any help!!!!

  • Well, one problem with your code is that if Item is not present in the array, it will continue past the end of the array. Then you would get that array error.

    I would try a For/Next loop instead.

  • I see what you are saying, however, the field is an item code input, so it has to be entered. There will always be a match because rows only get generated if there are rows back from the data.

    I did figure out that my problem was because I wasn't trimming the item code before I compared it, so they were never equal! I have it working now. Thanks for your help!

Viewing 11 posts - 1 through 10 (of 10 total)

You must be logged in to reply to this topic. Login to reply