Simplifying a code

  • Thread starter Thread starter Ron V
  • Start date Start date
R

Ron V

I have this IF, Then...code:

=IIf([sumofupQTC-scd] Or [CountupQTC-scd]=0,0,[sumofupQTC-
scd]/[CountupQTC-scd])

Basically it's saying that if those first 2 fields are =
0, then put a 0 into those fields, if not then divide the
value of those fields.

Can anyone see a better, cleaner way to write it or is it
fine as is?

Thanks for you input.

Ron
 
Ron said:
I have this IF, Then...code:

=IIf([sumofupQTC-scd] Or [CountupQTC-scd]=0,0,[sumofupQTC-
scd]/[CountupQTC-scd])

Basically it's saying that if those first 2 fields are =
0, then put a 0 into those fields, if not then divide the
value of those fields.

Can anyone see a better, cleaner way to write it or is it
fine as is?

You should specify each term's comparison:

=IIf([sumofupQTC-scd] = 0 Or [CountupQTC-scd] = 0, 0,
[sumofupQTC-scd] / [CountupQTC-scd])
 
Ron,

If you allow that the denominator can be zero, you'll likely end up with an
error.
The IIf construct evaluates both TruePart and FalsePart expressions.
Consequently, you have a built-in divide by zero situation.

To verify, try typing in the IDE Immediate command window:
?IIf(True, 0, 1/0)
Conclusion, don't use the IIf construct if either 'part' can throw an
error - even if logically that part is not supposed to be involved.

A straightforward If ... Else, or possibly a Select Case would be more
appropriate.

CD
 
Back
Top