Divide by Zero error

  • I have an expression in my report that performs a division of two report fields.  In order to prevent the divide by zero error, I added the following logic:

    =iif(Fields!IssuedCount.Value>0, Fields!OnHandCount.Value/ Fields!IssuedCount.Value ,0)

    This doesn't trap for divide by zero errors.  What am I doing wrong?

  • This was removed by the editor as SPAM

  • I had this problem myself recently, what i had to do is create some custom code to handle it, I couldn't get the Iif statement to work properly either, here's an example of the function I created:

    Public Shared Function CheckValue(ByVal Total as Single, ByVal Price as Single, ByVal GM as Single) As String

     

     Dim intTotalGM as Single

     Dim intTotalPrice as Single

     

     If (Total*Price) = 0 Then

      intTotalGM = GM * Total

      intTotalPrice = Price * 1

      CheckValue = FormatPercent(intTotalGM / intTotalPrice)

     Else

      intTotalGM = GM * Total

      intTotalPrice = Price * Total

      CheckValue = FormatPercent(intTotalGM / intTotalPrice)

     End If

    End Function

    Then call the function:

     =Code.CheckValue(Fields!Total_QTY.Value, Fields!Price.Value, Fields!GM.Value)

    Hope this helps 

  • David-

    That works great.  Thanks.  I'm still not sure why SRS wants to evaluate my original IIF statement in that way.

  • I'm new here, but I thought I would give my input.  I had the same issue and it was all about where the parens were.  Try this:

    =iif(Fields!IssuedCount.Value>0, (Fields!OnHandCount.Value)/ (Fields!IssuedCount.Value) ,0)

     

  • The reason this problem arises is because SRS evalutes both sides of the IIF statement.

    Try this instead,

    =iif(Fields!OnHandCount.Value = 0, 1, Fields!OnHandCount.Value) / iif(Fields!IssuedCount.Value=0, 1, Fields!IssuedCount.Value)

     

    --------------------
    Colt 45 - the original point and click interface

  • Phill you're a genius! I've made a small variation on your solution to fix my problem. I've spend about six hours yesterday trying all sorts of variations... today I found your suggestion!

    I hope MS fix this one... it defies logic (at least mine)...

    Here's my version:

    =IIF(Fields!InStock.Value 0,IIF( Fields!SalesQty.Value = 0,0, Fields!SalesQty.Value)/IIF(Fields!InStock.Value = 0,1, Fields!InStock.Value),"0")

    robhob

Viewing 7 posts - 1 through 6 (of 6 total)

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