Creating Queries  «Prev  Next»
Lesson 13 Creating calculated fields
Objective Add a calculated field to a query.

Creating Calculated Fields in Access

There may be times when you want to add a calculated field to your database. Perhaps you want to use two fields to calculate a third, or want to use one of Access’s functions to calculate a result. Although creating calculations in Access is not quite as simple as calculating in a spreadsheet such as Excel, they can be implemented in Microsoft Access. Your data will be more consistent and accurate if you learn how to perform calculations within your database. In order to create a calculated field you type an expression (sometimes called a formula) into the Field row in query Design view. The new field is given a name, and as long as you save the query, that new field is available for you to use in other queries as well as in forms and reports. This SlideShow shows you how to create a calculated field.
  1. This table contains information about sales.
  2. In order to create a calculated Commissions field
  3. In order to see all the fields in the Sales table, I double-click the asterisk(*) in the table box
  4. Type the expression for the new field into the Field row of the first empty column.
  5. The datasheet shows the new field, Commission

How to Create Calculated Field in Access 2022
This table contains information about sales. Salespeople receive a 10% commission, and I want to calculate that

Table Data:
| ID  | Name      | Price         | Date of Sale |
|------|-----------|--------------|--------------|
| 1    | Ronn      | $20,000.00   | 10/5/99      |
| 2    | Ronn      | $65,000.00   | 10/7/99      |
| 3    | Molinari  | $35,000.00   | 10/4/99      |
| 4    | Hudson    | $40,000.00   | 10/11/99     |
| 5    | Corey     | $60,000.00   | 10/11/99     |
| 6    | Corey     | $25,000.00   | 10/24/99     |
| 7    | Molinari  | $50,000.00   | 10/24/99     |
| 8    | Hudson    | $60,000.00   | 10/27/99     |
| 9    | Molinari  | $45,000.00   | 10/27/99     |
| 10   | Hudson    | $35,000.00   | 10/13/99     |

Observations:
  1. Data Context
    • The table is likely part of a database focused on sales transactions.
    • Key columns include ID, Name, Price, and Date of Sale.
  2. Patterns
    • Some names appear multiple times, suggesting repeat transactions (e.g., Ronn, Molinari, and Hudson).
    • The Price column shows various sales values, formatted as currency.
  3. Possible Calculations
    • The table could be used for calculating commissions, total sales by name, or other metrics.
  4. Features of Access
    • The toolbar indicates options for sorting, filtering, and performing calculations within Microsoft Access.
    • The presence of "toNumber()" in the formula bar implies a possible calculation or function applied to the data.
In order to create a calculated Commissions field I first create a new query that contains the Sales table.
2) This is a "Microsoft Access Query Design view" for a table named "Sales".
  1. Text Details:
    • The Sales table includes the following fields:
      • ID: Likely the primary key, representing a unique identifier for each sale.
      • Name: Presumably the name of the product or customer related to the sale.
      • Price: The price of the item or service sold.
      • Date of Sale: The date the sale occurred.
    • The query design view currently does not have any fields added to the Field, Table, Sort, Show, or Criteria sections.
  2. Corresponding Data (Assumed Use Case):
    • The query appears to be in the initial stage of defining the query parameters for calculating commissions or retrieving specific sales data. Based on the table structure:
      • The ID field is used for identification.
      • Name, Price, and Date of Sale will likely be used to calculate commission percentages or extract data related to specific sales.

Recommendations for Enhancing the Query:
  1. Define Criteria:
    • To calculate commissions, include the Price field and apply a formula (e.g., Commission = Price * Rate) using a calculated field.
    • If the query is to filter sales by date, use the Date of Sale field with a specific date range in the Criteria section.
  2. Add Sorting or Grouping:
    • Sort sales by the Date of Sale or group by Name to aggregate totals.

In order to create a calculated Commissions field I first create a new query that contains the Sales table.


In order to see all the fields in the Sales table, I double-click the asterisk(*) in the table box, this tells Access to display all fields from the Sales table in the query datasheet
3) In order to see all the fields in the Sales table, I double-click the asterisk(*) in the table box, this tells Access to display all fields from the Sales table in the query datasheet

Type the expression for the new field into the Field row of the first empty column. The expression is in this form: <br>
  New Field Name: expression. Notice that instead of an =(equal sign), a : (colon) is used, this is required in query expressions.
4) Type the expression for the new field into the Field row of the first empty column. The expression is in this form:
New Field Name: expression. Notice that instead of an =(equal sign), a : (colon) is used, this is required in query expressions.

The datasheet shows the new field, Commission
5) The datasheet shows the new field, Commission


Creating an expression for Access

When you are creating an expression you must tell Access what kind of terms you are using. Remember that Access encloses some kinds of data between special characters. Well, when you are creating an expression you must be sure to type those special characters in, or Access will not understand the expression. If you need a refresher on those special characters, view the table below to look at the chart again.
Special Characters
This type of data Looks like this in an expression
Text “text”
Date #1/1/00#
Time #1:00am#
Number 10
Field Name [field name]

Access also has a feature called Access Zoom Box to view your expression if it gets too long.
Access Zoom Box
Some expressions can get long, and the query grid does not give you much room to see them. Fortunately, Access has a nifty feature called a Zoom Box that displays your expression so that you can see it while you work on it. To display your expression in a Zoom Box, place the cursor somewhere in the expression and press Shift+F2.
Click OK on the Zoom Box to return to Design view.
access-zoom-box
Microsoft Access Zoom Box
  • Calculated Fields
    For many years, we have created calculated columns using an expression in a query. In many cases, calculating the value each time in a query is desired as values in a row may change. In this case, each time you run the query, the expression is reevaluated for all rows. Since the expression is evaluated every time, this may slow down the query depending on the number of rows and the complexity of the calculation. In Access 2010, a new type of field was added called a calculated field. In a calculated fi eld, the expression for the calculation is defined in the field, and the calculated values are stored in the table. These values are updated only when a field used in the expression is updated, making calculated fields nearly as fast as other fields. At first glance, it appears that storing calculated values would violate database normalization rules. This is true.
    However, there are scenarios when data may not change very often where this type of denormalization is valuable. For example, first and last name are often concatenated to form a person's full name. Names may not change very often, so this example is a good candidate for using the new calculated field type. Also, calculated fields can be indexed if desired, allowing faster retrieval and sorting of records based on the calculated value.

Creating Query-in Design View - Exercise

Try your hand at adding a calculated field to the Consulting Database in this exercise.
Creating Query-in Design View - Exercise

SEMrush Software 13 SEMrush Banner 13