Lesson 6.4. Creating a Calculated Field
Figure 6-6. Creating a field.
Figure 6-7. To enter fields in an expression, type the field name in brackets ([Order Total]).
Figure 6-8. If a field name exists in more than one table, you will need to enter the name of the table that contains the field in brackets ([Customer Tours]) followed by an exclamation mark (!). Then type the field name in brackets ([Order Total]).
Normally, when you create a database, you should only have to enter the information you need and not worry about data or values that Access calculates based on information already in the database. A calculated field performs some type of arithmetic on one or more fields in a database to come up with a completely new field. For example, if your database has an Order Total field and a Tax Rate field, Access can calculate these two fields to find out the Sales Tax for each order: [Order Total] x [Tax Rate] = [Sales Tax].
You must create an expression (or formula) to perform a calculation. To enter fields in an expression, type the field name in brackets ([Order Total]), as described in Figure 6-7. If a field name exists in more than one table, you will need to enter the name of the table that contains the field in brackets ([Customer Tours]) followed by an exclamation mark (!). Then type the field name in brackets, such as [Order Total], as described in Figure 6-8. For example if an Orders table and a Shipping table both contain a Date field, you would tell Access which of the two Date fields you want to use by typing the table name ([Orders]), an exclamation mark (!), and then the field name ([Date]) or, in other words, [Orders]![Date]. Yes, can be a little confusing at first...
This lesson will show you how to add a calculated field to a query.
If necessary, open the Lesson 6 database.
Now let's open the qryEmployeeSales query in Design view.
Click the Queries icon in the Objects bar if necessary, click the qryEmployeeSales query, and then click the Design button.
Instead of creating a query from scratch, you can modify an existing query and save it with a different name.
Click the blank Field cell of the fourth column and type Bonus:[Cost]*[Commission], as shown in Figure 6-6.
Tip: You can also use the Expression Builder to help you create your calculated fields. Click the Builder button on the toolbar, double-click the field you want to use in the calculation, click the button that corresponds to the calculation you want, and then click or type any other fields or values you want to use.
The expression you entered will create a new calculated field named "Bonus" that will display the results of the Cost field in the tblCustomerTours table multiplied by the Commission field in the tblEmployees table.
Let's see the results of our calculated field.
Click the Run button on the toolbar.
Tip: Other ways to run a query are to open the query from the Database window, or click the View button on the toolbar in Design view, or select Query
Access displays the results of the query. The "Bonus" calculated field multiplies the Cost field by the Commission field in each record and displays the results.
Save your changes in a new query with a different name. Here's how:
Select File The Save As dialog box appears.
Type qryEmployeeBonus in the Save Query 'qryEmployeeSales' To: box and click OK.
Access saves your changes in a new query named "qryEmployeeBonus."
Close the query.
You're already familiar with some of the used in expressions; including math symbols such as the plus sign (+) to perform addition between values, and the minus sign (-) to perform subtraction between values. Check out Table 6-3 below for a refresher on the various arithmetic operators.
Table 6-3. Arithmetic Operators
TO CREATE AN EXPRESSION OR CALCULATION IN A QUERY:
DISPLAY THE QUERY IN DESIGN VIEW.
CLICK THE FIELD ROW OF A BLANK COLUMN IN THE DESIGN GRID.
ENTER THE FIELD NAME FOR THE CALCULATED FIELD FOLLOWED BY A COLON (:).
ENTER THE EXPRESSION YOU WANT ACCESS TO CALCULATE, USING THE PROPER SYNTAX.
CLICK the BUILD BUTTON ON THE TOOLBAR, DOUBLE-CLICK THE FIELD YOU WANT TO USE IN THE CALCULATION, CLICK THE BUTTON THAT CORRESPONDS TO THE CALCULATION YOU WANT, AND THEN CLICK OR TYPE ANY OTHER FIELDS OR VALUES YOU WANT TO USE. WHEN YOU'RE FINISHED, CLICK OK.
CLICK THE VIEW BUTTON OR RUN BUTTON ON THE TOOLBAR TO SEE THE RESULTS.