Home > Archive > MS SQL Server Reporting Services > May 2005 > RE: Divide by 0 error









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author RE: Divide by 0 error
geissingert

2005-05-31, 8:24 pm

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)

Chris Patten

2005-05-31, 8:24 pm

Perfect!, u r the man (or woman)

Thanks for your help.


"geissingert" wrote:
[color=darkred]
> 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:
>
geissingert

2005-05-31, 8:24 pm

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:
[color=darkred]
> Yea I had tried something similiar, it results in #Error.
>
> Any other ideas?
>
>
>
> "geissingert" wrote:
>
geissingert

2005-05-31, 8:24 pm

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:
[color=darkred]
> Thanks but same #Error. What would Val do?
>
> "geissingert" wrote:
>
geissingert

2005-05-31, 8:24 pm

Hi Chris,

Glad to hear it is working.

Thanks,

geissingert

"Chris Patten" wrote:
[color=darkred]
> Perfect!, u r the man (or woman)
>
> Thanks for your help.
>
>
> "geissingert" wrote:
>
Chris Patten

2005-05-31, 8:25 pm

Yea I had tried something similiar, it results in #Error.

Any other ideas?



"geissingert" wrote:
[color=darkred]
> 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:
>
geissingert

2005-05-31, 8:25 pm

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:
[color=darkred]
> 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:
>
Chris Patten

2005-05-31, 8:25 pm

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:
[color=darkred]
> 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:
>
Sponsored Links





Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive | Programming forum archive

Copyright 2008 droptable.com