Wednesday, March 7, 2012

Divide by 0 error

How can I handle divide by 0 errors.
This is my report field expression:
=(Sum(Fields!A.Value)-Sum(Fields!B.Value))/Sum(Fields!B.Value)Thanks but same #Error. What would Val do?
"geissingert" wrote:
> Hi Chris,
> Try:
> =IIf(Val(Sum(Fields!B.Value))<>0,(Sum(Fields!A.Value)-Sum(Fields!B.Value))/Sum(Fields!B.Value),0)
>
> "Chris Patten" wrote:
> > Yea I had tried something similiar, it results in #Error.
> >
> > Any other ideas?
> >
> >
> >
> > "geissingert" wrote:
> >
> > > Hi Chris,
> > >
> > > Try:
> > >
> > > =IIf(Sum(Fields!B.Value)<>0,(Sum(Fields!A.Value)-Sum(Fields!B.Value))/Sum(Fields!B.Value),0)
> > >
> > > Thanks,
> > >
> > > geissingert
> > >
> > > "Chris Patten" wrote:
> > >
> > > > How can I handle divide by 0 errors.
> > > >
> > > > This is my report field expression:
> > > > =(Sum(Fields!A.Value)-Sum(Fields!B.Value))/Sum(Fields!B.Value)|||Hi Chris,
Try:
=IIf(Sum(Fields!B.Value)<>0,(Sum(Fields!A.Value)-Sum(Fields!B.Value))/Sum(Fields!B.Value),0)
Thanks,
geissingert
"Chris Patten" wrote:
> How can I handle divide by 0 errors.
> This is my report field expression:
> =(Sum(Fields!A.Value)-Sum(Fields!B.Value))/Sum(Fields!B.Value)|||Perfect!, u r the man (or woman)
Thanks for your help.
"geissingert" wrote:
> Hi Chris,
> Sorry about that. I forgot that IIf still evaluates both expressions. That
> is the reason for the error.
> Give this a try:
> In the expression of the text box that doing the division add the following
> code:
> =Code.Divide(Sum(Fields!A.Value),Sum(Fields!B.Value))
>
> Then under the Report properties, code tab enter the following:
> Function Divide(ByVal a As Object, ByVal b As Object) As Decimal
> Dim retVal As Decimal = 0
> If Val(b) > 0 Then
> retVal = (a - b) / b
> End If
> Return retVal
> End Function
>
> Give that a try and let me know.
> Thanks,
> geissingert
> "Chris Patten" wrote:
> > Still errors out.
> > I noticed it has no problem with evaluating the direct part of the expression.
> > If I change it from:
> > =IIf(Sum(Fields!B.Value)<>0,(Sum(Fields!A.Value)-Sum(Fields!B.Value))/Sum(Fields!B.Value),0)
> >
> > to:
> > =IIf(Sum(Fields!B.Value)<>0,"Not Zero",0)
> > It Returns 0 fine. Seems to not like evaluating the True part of the
> > expression for some reason?
> > (Sum(Fields!A.Value)-Sum(Fields!B.Value))/Sum(Fields!B.Value)
> >
> >
> >
> >
> > "geissingert" wrote:
> >
> > > Hi Chris,
> > >
> > > Val returns the value of an object.
> > >
> > > Try:
> > >
> > > =IIf(Sum(Val(Fields!B.Value))<>0,(Sum(Fields!A.Value)-Sum(Fields!B.Value))/Sum(Fields!B.Value),0)
> > >
> > > Thanks,
> > >
> > > geissingert
> > >
> > >
> > > "Chris Patten" wrote:
> > >
> > > > Thanks but same #Error. What would Val do?
> > > >
> > > > "geissingert" wrote:
> > > >
> > > > > Hi Chris,
> > > > >
> > > > > Try:
> > > > >
> > > > > =IIf(Val(Sum(Fields!B.Value))<>0,(Sum(Fields!A.Value)-Sum(Fields!B.Value))/Sum(Fields!B.Value),0)
> > > > >
> > > > >
> > > > > "Chris Patten" wrote:
> > > > >
> > > > > > Yea I had tried something similiar, it results in #Error.
> > > > > >
> > > > > > Any other ideas?
> > > > > >
> > > > > >
> > > > > >
> > > > > > "geissingert" wrote:
> > > > > >
> > > > > > > Hi Chris,
> > > > > > >
> > > > > > > Try:
> > > > > > >
> > > > > > > =IIf(Sum(Fields!B.Value)<>0,(Sum(Fields!A.Value)-Sum(Fields!B.Value))/Sum(Fields!B.Value),0)
> > > > > > >
> > > > > > > Thanks,
> > > > > > >
> > > > > > > geissingert
> > > > > > >
> > > > > > > "Chris Patten" wrote:
> > > > > > >
> > > > > > > > How can I handle divide by 0 errors.
> > > > > > > >
> > > > > > > > This is my report field expression:
> > > > > > > > =(Sum(Fields!A.Value)-Sum(Fields!B.Value))/Sum(Fields!B.Value)|||Hi Chris,
Try:
=IIf(Val(Sum(Fields!B.Value))<>0,(Sum(Fields!A.Value)-Sum(Fields!B.Value))/Sum(Fields!B.Value),0)
"Chris Patten" wrote:
> Yea I had tried something similiar, it results in #Error.
> Any other ideas?
>
> "geissingert" wrote:
> > Hi Chris,
> >
> > Try:
> >
> > =IIf(Sum(Fields!B.Value)<>0,(Sum(Fields!A.Value)-Sum(Fields!B.Value))/Sum(Fields!B.Value),0)
> >
> > Thanks,
> >
> > geissingert
> >
> > "Chris Patten" wrote:
> >
> > > How can I handle divide by 0 errors.
> > >
> > > This is my report field expression:
> > > =(Sum(Fields!A.Value)-Sum(Fields!B.Value))/Sum(Fields!B.Value)|||Hi Chris,
Val returns the value of an object.
Try:
=IIf(Sum(Val(Fields!B.Value))<>0,(Sum(Fields!A.Value)-Sum(Fields!B.Value))/Sum(Fields!B.Value),0)
Thanks,
geissingert
"Chris Patten" wrote:
> Thanks but same #Error. What would Val do?
> "geissingert" wrote:
> > Hi Chris,
> >
> > Try:
> >
> > =IIf(Val(Sum(Fields!B.Value))<>0,(Sum(Fields!A.Value)-Sum(Fields!B.Value))/Sum(Fields!B.Value),0)
> >
> >
> > "Chris Patten" wrote:
> >
> > > Yea I had tried something similiar, it results in #Error.
> > >
> > > Any other ideas?
> > >
> > >
> > >
> > > "geissingert" wrote:
> > >
> > > > Hi Chris,
> > > >
> > > > Try:
> > > >
> > > > =IIf(Sum(Fields!B.Value)<>0,(Sum(Fields!A.Value)-Sum(Fields!B.Value))/Sum(Fields!B.Value),0)
> > > >
> > > > Thanks,
> > > >
> > > > geissingert
> > > >
> > > > "Chris Patten" wrote:
> > > >
> > > > > How can I handle divide by 0 errors.
> > > > >
> > > > > This is my report field expression:
> > > > > =(Sum(Fields!A.Value)-Sum(Fields!B.Value))/Sum(Fields!B.Value)|||Hi Chris,
Glad to hear it is working.
Thanks,
geissingert
"Chris Patten" wrote:
> Perfect!, u r the man (or woman)
> Thanks for your help.
>
> "geissingert" wrote:
> > Hi Chris,
> >
> > Sorry about that. I forgot that IIf still evaluates both expressions. That
> > is the reason for the error.
> >
> > Give this a try:
> >
> > In the expression of the text box that doing the division add the following
> > code:
> >
> > =Code.Divide(Sum(Fields!A.Value),Sum(Fields!B.Value))
> >
> >
> >
> > Then under the Report properties, code tab enter the following:
> >
> > Function Divide(ByVal a As Object, ByVal b As Object) As Decimal
> > Dim retVal As Decimal = 0
> > If Val(b) > 0 Then
> > retVal = (a - b) / b
> > End If
> > Return retVal
> > End Function
> >
> >
> >
> > Give that a try and let me know.
> >
> > Thanks,
> >
> > geissingert
> >
> > "Chris Patten" wrote:
> >
> > > Still errors out.
> > > I noticed it has no problem with evaluating the direct part of the expression.
> > > If I change it from:
> > > =IIf(Sum(Fields!B.Value)<>0,(Sum(Fields!A.Value)-Sum(Fields!B.Value))/Sum(Fields!B.Value),0)
> > >
> > > to:
> > > =IIf(Sum(Fields!B.Value)<>0,"Not Zero",0)
> > > It Returns 0 fine. Seems to not like evaluating the True part of the
> > > expression for some reason?
> > > (Sum(Fields!A.Value)-Sum(Fields!B.Value))/Sum(Fields!B.Value)
> > >
> > >
> > >
> > >
> > > "geissingert" wrote:
> > >
> > > > Hi Chris,
> > > >
> > > > Val returns the value of an object.
> > > >
> > > > Try:
> > > >
> > > > =IIf(Sum(Val(Fields!B.Value))<>0,(Sum(Fields!A.Value)-Sum(Fields!B.Value))/Sum(Fields!B.Value),0)
> > > >
> > > > Thanks,
> > > >
> > > > geissingert
> > > >
> > > >
> > > > "Chris Patten" wrote:
> > > >
> > > > > Thanks but same #Error. What would Val do?
> > > > >
> > > > > "geissingert" wrote:
> > > > >
> > > > > > Hi Chris,
> > > > > >
> > > > > > Try:
> > > > > >
> > > > > > =IIf(Val(Sum(Fields!B.Value))<>0,(Sum(Fields!A.Value)-Sum(Fields!B.Value))/Sum(Fields!B.Value),0)
> > > > > >
> > > > > >
> > > > > > "Chris Patten" wrote:
> > > > > >
> > > > > > > Yea I had tried something similiar, it results in #Error.
> > > > > > >
> > > > > > > Any other ideas?
> > > > > > >
> > > > > > >
> > > > > > >
> > > > > > > "geissingert" wrote:
> > > > > > >
> > > > > > > > Hi Chris,
> > > > > > > >
> > > > > > > > Try:
> > > > > > > >
> > > > > > > > =IIf(Sum(Fields!B.Value)<>0,(Sum(Fields!A.Value)-Sum(Fields!B.Value))/Sum(Fields!B.Value),0)
> > > > > > > >
> > > > > > > > Thanks,
> > > > > > > >
> > > > > > > > geissingert
> > > > > > > >
> > > > > > > > "Chris Patten" wrote:
> > > > > > > >
> > > > > > > > > How can I handle divide by 0 errors.
> > > > > > > > >
> > > > > > > > > This is my report field expression:
> > > > > > > > > =(Sum(Fields!A.Value)-Sum(Fields!B.Value))/Sum(Fields!B.Value)|||Yea I had tried something similiar, it results in #Error.
Any other ideas?
"geissingert" wrote:
> Hi Chris,
> Try:
> =IIf(Sum(Fields!B.Value)<>0,(Sum(Fields!A.Value)-Sum(Fields!B.Value))/Sum(Fields!B.Value),0)
> Thanks,
> geissingert
> "Chris Patten" wrote:
> > How can I handle divide by 0 errors.
> >
> > This is my report field expression:
> > =(Sum(Fields!A.Value)-Sum(Fields!B.Value))/Sum(Fields!B.Value)|||Hi Chris,
Sorry about that. I forgot that IIf still evaluates both expressions. That
is the reason for the error.
Give this a try:
In the expression of the text box that doing the division add the following
code:
=Code.Divide(Sum(Fields!A.Value),Sum(Fields!B.Value))
Then under the Report properties, code tab enter the following:
Function Divide(ByVal a As Object, ByVal b As Object) As Decimal
Dim retVal As Decimal = 0
If Val(b) > 0 Then
retVal = (a - b) / b
End If
Return retVal
End Function
Give that a try and let me know.
Thanks,
geissingert
"Chris Patten" wrote:
> Still errors out.
> I noticed it has no problem with evaluating the direct part of the expression.
> If I change it from:
> =IIf(Sum(Fields!B.Value)<>0,(Sum(Fields!A.Value)-Sum(Fields!B.Value))/Sum(Fields!B.Value),0)
> to:
> =IIf(Sum(Fields!B.Value)<>0,"Not Zero",0)
> It Returns 0 fine. Seems to not like evaluating the True part of the
> expression for some reason?
> (Sum(Fields!A.Value)-Sum(Fields!B.Value))/Sum(Fields!B.Value)
>
>
> "geissingert" wrote:
> > Hi Chris,
> >
> > Val returns the value of an object.
> >
> > Try:
> >
> > =IIf(Sum(Val(Fields!B.Value))<>0,(Sum(Fields!A.Value)-Sum(Fields!B.Value))/Sum(Fields!B.Value),0)
> >
> > Thanks,
> >
> > geissingert
> >
> >
> > "Chris Patten" wrote:
> >
> > > Thanks but same #Error. What would Val do?
> > >
> > > "geissingert" wrote:
> > >
> > > > Hi Chris,
> > > >
> > > > Try:
> > > >
> > > > =IIf(Val(Sum(Fields!B.Value))<>0,(Sum(Fields!A.Value)-Sum(Fields!B.Value))/Sum(Fields!B.Value),0)
> > > >
> > > >
> > > > "Chris Patten" wrote:
> > > >
> > > > > Yea I had tried something similiar, it results in #Error.
> > > > >
> > > > > Any other ideas?
> > > > >
> > > > >
> > > > >
> > > > > "geissingert" wrote:
> > > > >
> > > > > > Hi Chris,
> > > > > >
> > > > > > Try:
> > > > > >
> > > > > > =IIf(Sum(Fields!B.Value)<>0,(Sum(Fields!A.Value)-Sum(Fields!B.Value))/Sum(Fields!B.Value),0)
> > > > > >
> > > > > > Thanks,
> > > > > >
> > > > > > geissingert
> > > > > >
> > > > > > "Chris Patten" wrote:
> > > > > >
> > > > > > > How can I handle divide by 0 errors.
> > > > > > >
> > > > > > > This is my report field expression:
> > > > > > > =(Sum(Fields!A.Value)-Sum(Fields!B.Value))/Sum(Fields!B.Value)|||Still errors out.
I noticed it has no problem with evaluating the direct part of the expression.
If I change it from:
=IIf(Sum(Fields!B.Value)<>0,(Sum(Fields!A.Value)-Sum(Fields!B.Value))/Sum(Fields!B.Value),0)
to:
=IIf(Sum(Fields!B.Value)<>0,"Not Zero",0)
It Returns 0 fine. Seems to not like evaluating the True part of the
expression for some reason?
(Sum(Fields!A.Value)-Sum(Fields!B.Value))/Sum(Fields!B.Value)
"geissingert" wrote:
> Hi Chris,
> Val returns the value of an object.
> Try:
> =IIf(Sum(Val(Fields!B.Value))<>0,(Sum(Fields!A.Value)-Sum(Fields!B.Value))/Sum(Fields!B.Value),0)
> Thanks,
> geissingert
>
> "Chris Patten" wrote:
> > Thanks but same #Error. What would Val do?
> >
> > "geissingert" wrote:
> >
> > > Hi Chris,
> > >
> > > Try:
> > >
> > > =IIf(Val(Sum(Fields!B.Value))<>0,(Sum(Fields!A.Value)-Sum(Fields!B.Value))/Sum(Fields!B.Value),0)
> > >
> > >
> > > "Chris Patten" wrote:
> > >
> > > > Yea I had tried something similiar, it results in #Error.
> > > >
> > > > Any other ideas?
> > > >
> > > >
> > > >
> > > > "geissingert" wrote:
> > > >
> > > > > Hi Chris,
> > > > >
> > > > > Try:
> > > > >
> > > > > =IIf(Sum(Fields!B.Value)<>0,(Sum(Fields!A.Value)-Sum(Fields!B.Value))/Sum(Fields!B.Value),0)
> > > > >
> > > > > Thanks,
> > > > >
> > > > > geissingert
> > > > >
> > > > > "Chris Patten" wrote:
> > > > >
> > > > > > How can I handle divide by 0 errors.
> > > > > >
> > > > > > This is my report field expression:
> > > > > > =(Sum(Fields!A.Value)-Sum(Fields!B.Value))/Sum(Fields!B.Value)

No comments:

Post a Comment