ReportViewer IIF表达式返回#error

时间:2012-02-20 14:49:44

标签: vb.net reportviewer

我花了两天的时间来弄清楚这一点,但没有运气。 我有一个IIF表达式,当条件成立时显示正确的值(计算IPmt),但是else总是返回#error,尽管它应该只是0。

=IIf(Fields!PaymentNumber.Value<>0,
     IPmt(Fields!Annual_Interest_Rate.Value/1200,Fields!PaymentNumber.Value,Fields!Payment.Value,Fields!Total_Amount_Financed.Value),
     0)

所有字段都有值(不能为空) 是什么导致这种情况?

感谢

1 个答案:

答案 0 :(得分:0)

错误是因为始终评估真实条件并在PaymentNumber = 0时返回错误。根据文档,始终评估IIF表达式的两个条件。您可以通过切换顺序来测试:

=IIf(Fields!PaymentNumber.Value=0, 0, 
    IPmt(Fields!Annual_Interest_Rate.Value/1200,Fields!PaymentNumber.Value,Fields!Payment.Value,Fields!Total_Amount_Financed.Value)) 

我过去所做的是在“自定义代码”中创建一个函数。我没有使用IPmt,但我相信这应该有效:

Function CalcIpmt(Annual_Interest_Rate As Decimal, PaymentNumber As Integer, Payment As Decimal, Total_Amount_Financed As Decimal) As Decimal)

    If PaymentNumber = 0 Then
        Return 0
    Else
        Return IPmt(Annual_Interest_Rate, PaymentNumber, Payment, Total_Amount_Financed)
    End If 

End Function

然后你的表达将是:

=Code.CalcIpmt( Fields!Annual_Interest_Rate.Value/1200, Fields!PaymentNumber.Value, Fields!Payment.Value, Fields!Total_Amount_Financed.Value )

(注意:我没有对此进行测试,您需要检查数据类型)