I have been able to use the following code to display results on a report. I
am trying to use this same code in a query but I keep coming up with errors
including "Division by zero". Some of the fields will be null and cannot have
a "0" for the value. The result of this calculation is an average.
=(Nz([Hour1],0)+Nz([Hour2],0)+Nz([Hour3],0)+Nz([Hour4],0)+Nz([Hour5],0)+Nz([Hour6],0)+Nz([Hour7],0)+Nz([Hour8],0)+Nz([Hour9],0)+Nz([Hour10],0)+Nz([Hour11],0)+Nz([Hour12],0)+[Factored_DT])/(IIf([Hour1]
Is Null,0,1)+IIf([Hour2] Is Null,0,1)+IIf([Hour3] Is Null,0,1)+IIf([Hour4] Is
Null,0,1)+IIf([Hour5] Is Null,0,1)+IIf([Hour6] Is Null,0,1)+IIf([Hour7] Is
Null,0,1)+IIf([Hour8] Is Null,0,1)+IIf([Hour9] Is Null,0,1)+IIf([Hour10] Is
Null,0,1)+IIf([Hour11] Is Null,0,1)+IIf([Hour12] Is Null,0,1))
I also realize this is repetative data and should be in a relating table
because each Hour is it's own field (1-12). Any help or advice on any of the
above would be greatly appreciated.
Duane Hookom - 15 Aug 2006 04:18 GMT
Normalize, normalize, normalize.
http://www.datamodel.org/NormalizationRules.html

Signature
Duane Hookom
MS Access MVP
>I have been able to use the following code to display results on a report.
>I
[quoted text clipped - 16 lines]
> the
> above would be greatly appreciated.
Ken Snell (MVP) - 15 Aug 2006 04:26 GMT
First, yes, you're right.... your table structure is not normalized, and
you're seeing just one of many problems of dealing with unnormalized data
structure -- namely, the ridiculously complex expression that you need to
use to get an average.
What you need is a child table with four fields in it:
tblHourTable
HourID (primary key -- autonumber is fine)
LinkingField (foreign key back to main table)
HourNum (long integer to hold values 1 through 12)
HourValue (field to hold the value for each hour)
The linking field should hold the value of the primary key field for the
record to which the data are related in the main table.
Then your expression becomes:
=Sum(Nz([HourValue],0))+[Factored_DT])/Sum(Abs(Not [HourValue] Is Null))
To correct for the possibility of the entire denominator being a value of
zero:
=IIf(Sum(Abs(Not [HourValue] Is
Null))<>0,Sum(Nz([HourValue],0))+[Factored_DT])/Sum(Abs(Not [HourValue] Is
Null)),"what value you want when denominator is zero")

Signature
Ken Snell
<MS ACCESS MVP>
>I have been able to use the following code to display results on a report.
>I
[quoted text clipped - 16 lines]
> the
> above would be greatly appreciated.
John Spencer - 15 Aug 2006 12:55 GMT
As has been pointed out, you really need to normalize this data. One
workaround is to write a custom VBA function and use it in your query.
You could also try something like the following untested expression.
Although the length of the expression may get close to the allowable length
for an expression in the query grid.
IIF(Hour1 is Null and Hour2 is Null and ... and Hour12 is Null, Null,
John Spencer - 15 Aug 2006 13:29 GMT
Sorry about that. I'm no quite sure what happened. So let's try this again.
You could also try something like the following untested expression.
Although the length of the expression may get close to the allowable length
for an expression in the query grid.
IIF(Hour1 is Null and Hour2 is Null and ... and Hour12 is Null, Null,