Help with IIf statement in expression

I want to sum up the PDC values if the field New_Old_CC has a value of 1, else return 0 if no 1s are found.  I'm not sure what is malformed here:

=CInt(IIf(First(Fields!New_Old_CC.Value, "DataSet1") = 1, Sum(Fields!PDC.Value, "DataSet1"),0))

Error 3 [rsCompilerErrorInExpression] The Value expression for the textbox ‘textbox86’ contains an error: [BC30455] Argument not specified for parameter 'FalsePart' of 'Public Function IIf(Expression As Boolean, TruePart As Object, FalsePart As Object) As Object'. s:\vs\reportserver\..DCR.rdl 0 0 

The PDC is a money field, the New_Old_CC is a bit field




Answer this question

Help with IIf statement in expression

  • sanjayjoshi29

    You need to specify what the function is supposed to return, like this (in red):

    FransM wrote:

    Public Function CalcPercentage(field1 As Microsoft.ReportingServices.ReportProcessing.ReportObjectModel.Field, field2 As Microsoft.ReportingServices.ReportProcessing.ReportObjectModel.Field) As Integer
    <snip>

    My VB is rusty, but you might also want to look at the IsNothing(field1.Value) , IIRC when in code the syntax is If field1.Value Is Nothing Then...

    HTH

    sluggy


  • ss23

    Ok, this is weird.  When you are in the expressions editor, if your report has more than one dataset defined like mine (because I'm doing some other calcs), then you see this in the left pain:

    Fields(DataSet1)

    Datasets

    What I was doing was using the field values in my DataSets area...in it it had an additional "DataSet1" option for some reason, even though it was already showing Fields(DataSet1) outside the Datasets option on the left. 

    So when using the DataSet1 fields inside Datasets, I was in turn adding fields to my IIf statement that were actually in the report already...they were preceeded by SUM (because they're in a group) so this is not going to work obviously.

    So I realized, I had to use the Fields(DataSet1) fields which were my original fields and then changed it to this:

    =IIf(Fields!New_Old_CC.Value = 1, SUM(Fields!PDC.Value),0)

    Ok, the report rendered...some were summed but in some of the fields for that column I got an error:

    [rsRuntimeErrorInExpression] The Value expression for the textbox ‘textbox86’ contains an error: Input string was not in a correct format.

    So I realized, that there are cases where New_Old_CC is null so I need to somehow check this...maybe do an IIf inside my IIf to check IsNothing I guess



  • TedTarney

    Hi Robert,

    I want to make a function which gives back the division of two fields.
    It should contain a check for is nothing and division by zero.

    The first start is this:

    Public Function CalcPercentage(field1 As Microsoft.ReportingServices.ReportProcessing.ReportObjectModel.Field, field2 As Microsoft.ReportingServices.ReportProcessing.ReportObjectModel.Field)
    If (IsNothing(field1.Value)) then
    CalcPercentage=0
    Else
    CalcPercentage=field2/field1
    End if
    End function

    But if I go back to my layout and enter =Code.CalcPercentage it says it is not recognised.

    What Am I doing wrong Can you also help me with completing the function

    --Frans


  • Francisco Parrilla

    Rober, thanks a lot. It wasn't evident in BOL, at least the parts I found, that this syntax was possible...

  • &amp;#34411;&amp;#35960;

    In order to check for nulls, you could use: =iif(Fields!A.Value is Nothing, ...)
    Note: iif() is a function call, so it always evaluates all arguments.

    You may also want to consider adding a reuseable function in the Code section of the report (VS menu: report - report properties - code), such as

    Public Function PerformCalculation(field1 As Microsoft.ReportingServices.ReportProcessing.ReportObjectModel.Field, field2 As Microsoft.ReportingServices.ReportProcessing.ReportObjectModel.Field)
    If (IsNothing(field1.Value)) then
    -- some calculation
    Else
    -- other calculation
    End if
    End function

    There are some situations where it is easier to perform a calculation with If-Else-EndIf than trying to write nested IIF() calls so that there can never be a division by zero, etc.

    -- Robert



  • Help with IIf statement in expression