Register Blogs FAQ Members List Social Groups Calendar Search Today's Posts Mark Forums Read

Go Back   DeveloperBarn Forums > Databases > Microsoft Access > Access Database Samples

Sponsored Links

Discuss "Store Calculated Values (Updating Fields)" in the Access Database Samples forum.

Access Database Samples - The ASP Database Samples forum contains sample databases and VBA submitted by our members.


Reply
 
LinkBack Thread Tools Display Modes
  #1  
Old May 19th, 2008, 04:27 PM
sbenj69's Avatar
Moderator
 
Join Date: Mar 2008
Posts: 101
Thanks: 23
Thanked 30 Times in 22 Posts
Rep Power: 1
sbenj69 will become famous soon enough

Awards Showcase
Microsoft Windows Microsoft Access 
Total Awards: 2

Default Store Calculated Values (Updating Fields)

One of the biggest "No-No's" in database development is the storing of calculations in a field. For Example:
Field1 = 2
Field2 = 8
Field3 = 10 (The sum of Field1 and Field2)

The reason why the calculation should not be stored is simple. What happens if Field1 and Field2 need to be increased by 13.6% and 8.3% respectively?

The values would now be:
Field1 = 2.272
Field2 = 8.664
Field3 = 10 (Notice how they don't match now)

Sure you can change Field3 to equal 10.936...... but what if you had 10,000 records, with 150 calculated values stored per record? Yeah, I guess you could write a script that automatically does this, but what if the script was written wrong? Or what if you have to change the values daily, or hourly? Are you going to write a script, or run a script everytime? Keep in mind 10,000 records could take quite some time to update, especially if you have like 150 fields per record to update.

One more thing..... what if the database expanded..... now field3 must equal field1 + field2 + field4 + field5 + field6, and there were 10,000 records?

The calculation should be done at the time you want to display the value. You do this in a Query or Form using an expression, or an unbound textbox whose value is an expression.

In a query, it would be:
Expr1: [Field1]+[Field2]

In a form, in an unbound textbox, the control source under the data tab in properties of the textbox would be:
=[Field1]+[Field2]

The data needed for the calculation should be stored, not the results.....

The sample below shows both ways, and how the storing of data in a field can ruin your data. It also gives a step by step on how to configure an unbound textbox to show the calculation.
Attached Files
File Type: zip storeval.zip (41.4 KB, 23 views)

Last edited by sbenj69; May 19th, 2008 at 06:02 PM. Reason: Clearer Explanation
Reply With Quote
The Following User Says Thank You to sbenj69 For This Useful Post:
jmurrayhead (May 27th, 2008)
Sponsored Links
  #2  
Old May 22nd, 2008, 03:28 PM
don94403's Avatar
Moderator
 
Join Date: Mar 2008
Location: San Mateo, CA, USA
Posts: 69
Thanks: 3
Thanked 12 Times in 10 Posts
Blog Entries: 7
Rep Power: 1
don94403 will become famous soon enough

Awards Showcase
Microsoft Access PHP 
Total Awards: 2

Default

Hi, ben, this is califdon (aka don94403!)

Of course I agree with you completely, but I wanted to add my slant on it: the strongest reason why storing calculated values is bad is that it violates the 3rd Normal Form rule of normalization, which states that no non-key attribute should be dependent on any other non-key attribute. You will find some database practitioners who will argue that this is carrying things too far. But whose opinion means more to you, some unknown database developer, or Dr. Ted Codd and Chris Date, who developed relational database theory and wrote the primary textbook that is still used around the world, 8 editions and 40 years later?

Your explanation is a good down-to-earth explanation and is probably more useful to most beginners, but I just thought I'd go on the record with the formal explanation.

Comments on this post
jmurrayhead agrees: important facts are always helpful
Reply With Quote
The Following User Says Thank You to don94403 For This Useful Post:
sbenj69 (May 27th, 2008)
  #3  
Old May 22nd, 2008, 05:09 PM
sbenj69's Avatar
Moderator
 
Join Date: Mar 2008
Posts: 101
Thanks: 23
Thanked 30 Times in 22 Posts
Rep Power: 1
sbenj69 will become famous soon enough

Awards Showcase
Microsoft Windows Microsoft Access 
Total Awards: 2

Default

I agree completely Don. I was kinda counting on you to give the more formal explanation . I merely wanted to make simple db to show how it could fail those who try to store calculated values I tried to rep ya, but it says I need to spread it around first
Reply With Quote
  #4  
Old May 22nd, 2008, 08:07 PM
jmurrayhead's Avatar
The Barnfather
 
Join Date: Mar 2008
Location: Reston, VA, USA
Posts: 941
Thanks: 22
Thanked 93 Times in 90 Posts
Blog Entries: 5
Rep Power: 4
jmurrayhead is a jewel in the roughjmurrayhead is a jewel in the roughjmurrayhead is a jewel in the roughjmurrayhead is a jewel in the rough

Awards Showcase
Microsoft Windows Microsoft SQL Server Microsoft .Net Classic ASP 
Total Awards: 4

Default

I just want to add that there are exceptions to every rule. When it comes down to performance of a system, sometimes you have to store a calculated value in your table structure.

Take this forum, for instance. Thread, post, and view counts (among a few others) are all pre-calculated and stored in the table. Why? Take into consideration a forum that has thousands of posts and hundreds to thousands of visitors a day. It would be pretty demanding on the database, be it Access, MySQL, MS SQL or any other, to have to calculate these values every time a user browses a page. Just be sure you adequately prepare for miscalculations. This forum has a few functions that can be run to repair these counters so that they are accurate.

I know this could be argued to death, but I will say you can do things correctly and have performance issues or the other way and have miscalculations.
__________________
jmurrayhead
If you agree with me... click the icon!
If my post solved your problem, click the button in the lower right-hand corner of the post.

If you like it here...throw us a few bones to help
support us.

Join our Folding team: DeveloperBarn Folding

Reply With Quote
  #5  
Old May 25th, 2008, 10:20 PM
don94403's Avatar
Moderator
 
Join Date: Mar 2008
Location: San Mateo, CA, USA
Posts: 69
Thanks: 3
Thanked 12 Times in 10 Posts
Blog Entries: 7
Rep Power: 1
don94403 will become famous soon enough

Awards Showcase
Microsoft Access PHP 
Total Awards: 2

Default

I completely agree, jmh, when it comes to experienced designers who understand what they are doing and why. I usually don't say that up-front, because the people who usually are reading about normalizing a database haven't yet learned enough to make good decisions about when to denormalize one.
Reply With Quote
  #6  
Old May 26th, 2008, 10:07 AM
jmurrayhead's Avatar
The Barnfather
 
Join Date: Mar 2008
Location: Reston, VA, USA
Posts: 941
Thanks: 22
Thanked 93 Times in 90 Posts
Blog Entries: 5
Rep Power: 4
jmurrayhead is a jewel in the roughjmurrayhead is a jewel in the roughjmurrayhead is a jewel in the roughjmurrayhead is a jewel in the rough

Awards Showcase
Microsoft Windows Microsoft SQL Server Microsoft .Net Classic ASP 
Total Awards: 4

Default

Quote:
Originally Posted by don94403 View Post
I completely agree, jmh, when it comes to experienced designers who understand what they are doing and why. I usually don't say that up-front, because the people who usually are reading about normalizing a database haven't yet learned enough to make good decisions about when to denormalize one.
I completely agree. Denormalization is something that must be thought about thoroughly with necessary precautions and is something that probably should be reconsidered when the calculations are to do with people's money
__________________
jmurrayhead
If you agree with me... click the icon!
If my post solved your problem, click the button in the lower right-hand corner of the post.

If you like it here...throw us a few bones to help
support us.

Join our Folding team: DeveloperBarn Folding

Reply With Quote
  #7  
Old May 26th, 2008, 10:18 AM
Wolffy's Avatar
Slaprentice of Wolves
 
Join Date: Mar 2008
Location: Peoria, IL
Posts: 175
Thanks: 3
Thanked 24 Times in 21 Posts
Rep Power: 1
Wolffy is on a distinguished road

Awards Showcase
Microsoft .Net 
Total Awards: 1

Default

Yup, just to add another voice to the 'exceptions' rule. They are call exceptions becasue, well, they are 'excpetions!'. Follow the rule of normalization unless you have a VERY GOOD REASON for making the exception.
__________________
Wolffy
------------------------
Opinions expressed are my own and do not necessity reflect those of any sane person. Any code provided is intended to be an example and is provided AS IS. Rework for your specific environment may be required. Void where prohibited by law. Not valid in California. Your mileage may vary.
Reply With Quote
Reply

  DeveloperBarn Forums > Databases > Microsoft Access > Access Database Samples

Bookmarks

Thread Tools
Display Modes

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 On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Help with Updating field Rebelle Microsoft Access 5 April 1st, 2008 06:45 PM


All times are GMT -4. The time now is 06:47 AM.



Content Relevant URLs by vBSEO 3.2.0