Step Five - Advanced Criteria for a Query
We will now look at more advanced queries using and / or conditions. We will also look at Calculation Queries.
If you are coming back from a break, create a new query with the ProductName, CostPrice and Category fields.
The OR condition allows us to select different sets of criteria for the same field. For example, we are looking for all products that have the letter 'b' or the letter 'k' anywhere in the text.
You should have the following results:
The AND condition allows us to set the criteria for the query based on multiple fields. For example, we are looking for all products that are in the Cookies category and more than $3.00.
You should have the following results:
To return to the Design
window, click the Design button or select View, Design View
from the menu.
We can also have an AND within the criteria for one field. For example, we want all the products that have the letter 'o' and the letter 'e' in them. So, in order for them to show up in the the dynaset they have to have BOTH criteria to qualify.
You should have the following results:
To return to the Design
window, click the Design button or select View, Design View
from the menu.
A calculation query allows us to perform arithmetic and string calculations on the field in the query. Now why would you want to do this? Well, sometimes there is no need to store a fields in a table as it can be calculated by other fields that are stored in the table. For example, in a Wages table for employees, there is a field "HoursWorked" and another called "WageRate". Every month we need to get what the Gross Amount will be for their paycheque. It would make no sense to create a new field in the table design for Gross Amount as we already have the two fields that make up that field value. So in order to save space in the database, we would not create a new field in the table design, but we would do a calculation query to calculated the amount of Gross Amount by multiplying HoursWorked by WageRate. Eliminating these unnecessary fields is called normalization.
Let's do an example. We will calculate the amount of tax on our products.
TotalCost:[CostPrice] * 1.14
Now, what does this mean.
Item | Description |
[Total Cost] | This is the name of the expression. This is what will appear as the column title in the dynaset. |
: | This signifies the end of the expression name and the beginning of the calculation. |
[CostPrice] | This is the field
from the table we want to select. Important items here:
|
* | Arithmetic operator. |
1.14 | Static value that can be typed in. |
To close the query window,
click the button
or select File, Close from the menu. Click the No button when it
asks to save query.