DDoS In Oceania and the Pacific, presented by Dave Phelan at NZNOG 2024
MS_Access_C_-_Calculated__Fields in IT.pptx
1. Calculated Fields
Disclaimer. This was done under the assumption that everyone:
1. Understands datatypes
2. Has completed creating their database and populating the relevant tables
3. Read the first PowerPoint on Access
2. Calculated Fields
A calculated field is a field that derives its value by performing a function on values from other table
fields.
It can also calculate values entered by hand. The field’s data only appears for the duration of the
query. It is not actually stored in the database tables.
When you create a calculated field in Access, you can perform almost any available function.
We are required now to calculate Votes Cast for Constituencies. Votes Cast = Number of Voters *
Percentage Voter Turnout.
3. To create the calculated field, you enter an expression in a blank cell in the Field row in your
query. For example, if you have a query that contains an Weekly Salary field and a number, you
can multiply the two to create a calculated field for MonthlySalary by entering the following
expression in the Field row of the query:
MonthlySalary: [Amount] * 4
Prefacing the expression with the text MonthlySalary : names the new
column MonthlySalary. This name is often called an alias. If you do not supply an alias, Access
will create one, such as Expr1.
In our case, we wish to calculate Votes Cast. As such, Votes Cast: [Number of
Voters]*[Percentage Voter Turnout]
4. In our case, we wish to calculate Votes Cast. As such, Votes Cast: [Number of
Voters]*[Percentage Voter Turnout]
PLEASE NOTE:
Votes Cast: This creates the name for the new column in our query.
[Number of Voters] … [Percentage Voter Turnout] Names of fields to be included in the
NEED to be written as they appear in the table they come from, encapsulated between square
braces [].
Arithmetic Operators (* + - /) need not be changed.
5. Look at the data in the CANDIDATES DETAILS table. We see DOB, short for Date Of Birth.
Say we wanted to calculate the AGE of a candidate. This can be done as well using calculated fields.
Though a bit more technical, it can be done.
ATTENTON:
Functions exist in Access just as they do in Excel. We will be using the YEAR() and NOW() functions.
The YEAR() function reads a date, and returns the YEAR from the supplied date. As such Age: Year(
[DOB] ) will return the year from the DOB.
The NOW() function returns the current date & time: As such Year( Now() ) the year from the current
date and time.
Note the use of the brackets, and the type of brackets being used.
6. The YEAR() function reads a date, and returns the YEAR from the supplied date. As such Age: Year(
[DOB] ) will return the year from the DOB.
produces
7. The YEAR() function reads a date, and returns the YEAR from the supplied date. As such Date & Time:
Now() will return the current Date & Time.
Date & Time: Year( Now() ) will return year from the current date & time.
8. Therefore, to calculate the age of a candidate we would have to use the following expression:
As such Age: Year( Now() )- Year( [DOB] ). This is basically saying This Year – DOB Year.
This is just the tip of calculated fields. Calculated fields can also include prompts for the user to
enter a value as well as logical functions.