Solved

# Access query syntax - division by zero in aggregate function

Posted on 2014-02-25
780 Views
Dear experts -
hopefully an easy one.
I am calculate an average using the formula below - which works well, however i want to avoid division by zero, which is giving me a #Num! error for certain results.

Any suggestions?
Thanks!

``````TR_Reg: Sum(IIf([e].[billunit]=1 And [e].[volumeunit]=1 And [accounts].[regulatory]=True And [e].[action_ID]=38,[e].[billrate],0))/Sum(IIf([e].[billunit]=1 And [e].[volumeunit]=1 And [accounts].[regulatory]=True And [e].[action_ID]=38,1,0))
``````
0
Question by:terpsichore

LVL 48

Accepted Solution

Rgonzo1971 earned 500 total points
ID: 39885125
Hi,

Maybe

``````TR_Reg: Sum(IIf([e].[billunit]=1 And [e].[volumeunit]=1 And [accounts].[regulatory]=True And [e].[action_ID]=38,[e].[billrate],0))/IIF(Sum(IIf([e].[billunit]=1 And [e].[volumeunit]=1 And [accounts].[regulatory]=True And [e].[action_ID]=38,1,0))=0,null,Sum(IIf([e].[billunit]=1 And [e].[volumeunit]=1 And [accounts].[regulatory]=True And [e].[action_ID]=38,1,0)))
``````
Regards
0

LVL 49

Expert Comment

ID: 39885126
This is one method, to first check if the sum is zero:

TR_Reg:
IIf(Sum(IIf([e].[billunit]=1 And [e].[volumeunit]=1 And [accounts].[regulatory]=True And [e].[action_ID]=38,1,0))=0, 0,
Sum(IIf([e].[billunit]=1 And [e].[volumeunit]=1 And [accounts].[regulatory]=True And [e].[action_ID]=38,[e].[billrate],0))/Sum(IIf([e].[billunit]=1 And [e].[volumeunit]=1 And [accounts].[regulatory]=True And [e].[action_ID]=38,1,0)))

/gustav
0

Author Closing Comment

ID: 39885144
This worked - thank you.
0

## Featured Post

Introduction The Visual Basic for Applications (VBA) language is at the heart of every application that you write. It is your key to taking Access beyond the world of wizards into a world where anything is possible. This article introduces you to…
In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…