Click here to Skip to main content
15,887,414 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How can i add a fuction in 'default value' in field properties in ms access, to calculate the average of fields in the same table after a new record?

I try this but didn't work:

(([col1]+[col2]+[col3]+[col4])/4)

Please help.
Posted
Updated 7-Apr-13 3:43am
v2

Some useful information you'll find here: http://office.microsoft.com/en-us/access-help/set-a-default-value-for-a-field-or-control-mdb-HP005188852.aspx[^]

You don't need to set default value for field in database table, because, you can always set "default value" in query:
SQL
PARAMETERS p1 INT, p2 INT, p3 INT, p4 INT;
INSERT INTO YourTable (col1, col2, col3, col4, col5)
VALUES(p1, p2, p3, p4, (p1+p2+p3+p4)/4);


But i'm wondering why do you want to store average value, if you always can calculate it (based on col1-col4 values)?
SQL
SELECT col1, col2, col3, col4, (col1+col2+col3+col4)/4 AS col5
FROM YourTable
 
Share this answer
 
AFAIK, you can't add any default value that is not known at compile time: when the data definition is saved. This means you can't use a calculated value that requires data from the row because that hasn't been set yet.

I would allow the field to be null, and work out the value using a stored procedure when I read it out - that way, the field value is also up-to-date if no value has been specified. Your way would only set the default once based on the initial values which may be well different to the actual value when you read them.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900