PC Review


Reply
Thread Tools Rate Thread

Adding Fields in an Access 2003 Form, including null fields

 
 
=?Utf-8?B?Qi4gTGV2aWVu?=
Guest
Posts: n/a
 
      9th Nov 2006
I have a form with four cost fields, which have a currency format:

Costs
Costs2
Costs3
Costs4

I can get them to add up using the following formula:

=Nz([Costs]+[Costs2]+[Costs3]+[Costs4])

My problem is that all 4 fields must have a currency value in order to add
up (so I have to put $0.00 in fields with no information). How to I get the
four fields to add up, if 1 to 3 of them do not have a value at all?

Any assistance would be greatly appreciated!

Brenda
 
Reply With Quote
 
 
 
 
=?Utf-8?B?QnJ1Y2UgTWVuZWdoaW4=?=
Guest
Posts: n/a
 
      9th Nov 2006
You can protect against NULLs by strining together IIF statements

IIf(IsNull([Costs]),0,[Costs])+IIf(IsNull([Costs2]),0,[Costs2])+ ...

"B. Levien" wrote:

> I have a form with four cost fields, which have a currency format:
>
> Costs
> Costs2
> Costs3
> Costs4
>
> I can get them to add up using the following formula:
>
> =Nz([Costs]+[Costs2]+[Costs3]+[Costs4])
>
> My problem is that all 4 fields must have a currency value in order to add
> up (so I have to put $0.00 in fields with no information). How to I get the
> four fields to add up, if 1 to 3 of them do not have a value at all?
>
> Any assistance would be greatly appreciated!
>
> Brenda

 
Reply With Quote
 
John Vinson
Guest
Posts: n/a
 
      9th Nov 2006
On Thu, 9 Nov 2006 13:21:02 -0800, B. Levien
<(E-Mail Removed)> wrote:

>I have a form with four cost fields, which have a currency format:
>
>Costs
>Costs2
>Costs3
>Costs4


Then your table is incorrectly designed. If you have a one (whatever)
to many (costs) relationship, a much better design would use THREE
tables:

YourTable <don't know what it represents>
SomeField <primary key>

CostTypes
CostType <your Costs, Costs2 etc. > <primary key>

Costs
SomeField <link to your table>
CostType <link to CostTypes>
Amount <currency>

Thus if you ever need a FIFTH cost, you can simply add it rather than
redesigning your table, all your queries, all your forms, and all your
calculations.

>I can get them to add up using the following formula:
>
>=Nz([Costs]+[Costs2]+[Costs3]+[Costs4])
>
>My problem is that all 4 fields must have a currency value in order to add
>up (so I have to put $0.00 in fields with no information). How to I get the
>four fields to add up, if 1 to 3 of them do not have a value at all?


Wrap EACH of the costs in NZ. What you're doing here is getting a NULL
result - anything plus null is NULL - and *then* setting that to zero.

=NZ([Costs]) + NZ([Costs2]) + NZ([Costs3]) + NZ([Costs4])

will convert each NULL to a zero *first* - and since $12.50 + 0 =
$12.50, you'll get the right sum.

But... normalize your data, and use a Totals query; simply don't ENTER
any record if there is no cost for that category.

John W. Vinson[MVP]
 
Reply With Quote
 
Jamie Collins
Guest
Posts: n/a
 
      10th Nov 2006

John Vinson wrote:
> >I have a form with four cost fields, which have a currency format:
> >
> >Costs
> >Costs2
> >Costs3
> >Costs4

>
> Then your table is incorrectly designed. If you have a one (whatever)
> to many (costs) relationship, a much better design would use THREE
> tables:
>
> Thus if you ever need a FIFTH cost, you can simply add it rather than
> redesigning your table, all your queries, all your forms, and all your
> calculations.


But what if the business rule is there must always be four, no more no
less (including zero)? Having four columns makes such a constraint easy
to write: just make all columns NOT NULL (required = true/yes). With
your proposed design, such a constraint is harder to implement. Don't
you think there is a case for 'denormalize for data integrity'?

Jamie.

--

 
Reply With Quote
 
John Vinson
Guest
Posts: n/a
 
      10th Nov 2006
On 10 Nov 2006 01:58:22 -0800, "Jamie Collins"
<(E-Mail Removed)> wrote:

>ut what if the business rule is there must always be four, no more no
>less (including zero)? Having four columns makes such a constraint easy
>to write: just make all columns NOT NULL (required = true/yes). With
>your proposed design, such a constraint is harder to implement. Don't
>you think there is a case for 'denormalize for data integrity'?


<shrug> Could happen.

In my experience, whenever there are exactly four categories... some
manager will come along within a few months with a fifth. Case in
point: "The only thing worse than three bridge players trying to find
a fourth is four bridge players drinking a fifth." <g>

John W. Vinson[MVP]
 
Reply With Quote
 
=?Utf-8?B?Qi4gTGV2aWVu?=
Guest
Posts: n/a
 
      12th Nov 2006
Thank you all very much for your responses! They were very helpful &
informative!!!

I left all the costs fields in the table, rather than separating them into
separate tables, but generated a Costs Query with a field to add up the four
costs fields I needed (the number of costs fields will not change), using the
NZ value to include any field which did not have any information. I created
a report based upon that Query to show the total amount being charged.

"John Vinson" wrote:

> On 10 Nov 2006 01:58:22 -0800, "Jamie Collins"
> <(E-Mail Removed)> wrote:
>
> >ut what if the business rule is there must always be four, no more no
> >less (including zero)? Having four columns makes such a constraint easy
> >to write: just make all columns NOT NULL (required = true/yes). With
> >your proposed design, such a constraint is harder to implement. Don't
> >you think there is a case for 'denormalize for data integrity'?

>
> <shrug> Could happen.
>
> In my experience, whenever there are exactly four categories... some
> manager will come along within a few months with a fifth. Case in
> point: "The only thing worse than three bridge players trying to find
> a fourth is four bridge players drinking a fifth." <g>
>
> John W. Vinson[MVP]
>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Including null fields in crosstab columns AND rows =?Utf-8?B?U3RldmUgVmluY2VudA==?= Microsoft Access Queries 5 27th Sep 2006 11:25 PM
Fields in recordset from Access with value Null (empty fields) =?Utf-8?B?TWF0cyBOaWxzc29u?= Microsoft Excel Programming 2 20th Sep 2006 05:51 PM
adding 2 fields including null entries =?Utf-8?B?SmVzc2U=?= Microsoft Access Queries 26 18th Jan 2005 05:31 PM
Additional fields for form based parameter query/null fields =?Utf-8?B?Z2Vla3Nkb2l0YmV0dGVy?= Microsoft Access Queries 2 7th Jan 2005 10:05 PM
Including null fields Tim Microsoft Access Queries 1 30th Jul 2003 12:48 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:21 AM.