You are on page 1of 9

Access 2007 Quick guide to queries

Contents
Creating a Query....................................................................................................................................2
Using Wildcards in a query....................................................................................................................2
Sum.......................................................................................................................................................3
Count.....................................................................................................................................................3
Not and Null Queries.............................................................................................................................4
Using Not...........................................................................................................................................4
Using Null..........................................................................................................................................4
Group By................................................................................................................................................4
Example query using Group By, Count and Sum....................................................................................5
Creating the Query............................................................................................................................5
Using Mathematical Operators.............................................................................................................6
Access operators and their meaning.................................................................................................6
Average Values......................................................................................................................................7
Maximum and Minimum Values............................................................................................................7
Calculated Fields in Queries...................................................................................................................7
Parameter Queries................................................................................................................................8
Using wildcard selection................................................................................................................9
Using range selections...................................................................................................................9

1
Access 2007 Quick guide to queries
Creating a Query

You can create a query two ways, Click on the Create Tab to
display the ribbon as above.

1. Query Wizard display a New Query popup which will


step you through the creation of a new query.
2. Query Design which takes you to the Query Design
Screen.

Using Wildcards in a query


In a query, it is often useful to select records without having an exact match on a field. For example
to select ‘Sales’ people from their job titles, which may be sales assistant, sales supervisor, etc. This
can be done using a pattern search, which is simply a text search based on wildcard characters;

1. ? – One unknown character


2. * - Any number of unknown characters

Within a database

1. Create a new query in the Design View


2. Place the required fields into the query grid
3. For a field you need to search using the Wildcard
a. Click on the Criteria Row
b. Type *sales*. This is enough of the word to find all of the required occurrences
because * denotes any amount of text may occur before or/and after.
c. Access will change the criteria to Like “*sales*”.
4. Now run the query.

2
Access 2007 Quick guide to queries
Sum
Various summary calculations can be carried out on numerical data within Access. The most
common includes finding the total and average of a set of figures, the
maximum and minimum value and counting the number of records. These
calculations are carried out on a total line within a query on the data. Instead
of displaying all records which met the criteria match, in a sum query the
calculations are applied to the selected records and only the summary result is
displayed in the query.

1. Create a new query in the design view


2. Click on the Totals button. A new row will appear on the grid called Total: with the default
entry of Group By.

3. Click in the Total: box for the field you which to total, then click the down arrow to display
the drop down list. Select Sum from this list.
4. Run the query.
5. The answer table shows only one field, SumOffieldname. This is the total value of all the
data.

Note: The Total row is removed by clicking ∑ again.

Count
The count calculation is another summary calculation, which counts how many entries in a query
have the value in a particular field, including duplicate values. It does not count null entries, I.E.
empty fields.

1. Edit above query in Design View. (Ensure Total: row is displayed)


2. Click on the down arrow in the Total: box and select count.
3. Run the query, the answer table should give you a count of the number of items in the table
that match your criteria.

Note: Take care when choosing which field to use in the count. If any records on the table did
not have an entry in the chosen field, they would not be included in the count.

3
Access 2007 Quick guide to queries
Not and Null Queries
Queries may be applied to tables to exclude certain records when the query is run and therefore to
display all the records which do not match the criteria. It is also possible to search for fields that
have no data by using a Null query. This can be useful when you want to make sure that all records
have been fully completed.

Using Not
1. Open your query in Design View
2. In the criteria for your chosen field enter “Not” and the value you wish to exclude. See the
example below;

3. Run the query and bikes will be shown apart from ‘Giant’.

Using Null
1. Open your Query in Design View
2. To find records which have no data in a selected field. Type Null in the Criteria box for the
required field.
3. Run the query and the items returned are the ones with data in your selected field.

Group By
Using a summary code of Group By in a query does not show all selected records but will show a
result row for each different value in a field. Group By is often combined with other summary codes
to produce sub totals.

1. Open your query in deign view


2. Display the Total: row by clicking the Totals button ∑.
3. Select Group By from the pull down menu.
4. Now run the query

4
Access 2007 Quick guide to queries
Example query using Group By, Count and Sum
The Example uses the Bike Sales data which has been imported into Access 2007 from a CSV file on
the Edexcel website.

This query is going to show how many bikes of each model sold, using count, and the total amount
each model sold, using Sum.

Creating the Query


1. Click on the Create Tab on the Access Ribbon
2. Use the Query Design Button
3. From the table popup select the tables you need to select the data from.
4. For this query; double click on the ‘Make’, ‘Model’, ‘Model’ (Once for the data to appear and
the second so we and count) and ‘Price’
5. Click on the Totals ∑ button to display the Total: row.
6. Click on the Total: row for Make and Select Group by from the pull down menu. Then do the
same for the first Model field in the grid.
7. Click on the Total: row for the second Make field and select Count from the menu.
8. Click on the Total: row for the Price field and select Sum from the menu.

5
Access 2007 Quick guide to queries
Using Mathematical Operators
Where values are used in tables, queries can be performed which select records based on a range of
values, e.g. quantities more that a certain amount, prices less than a certain value, or ages within an
age range. To do this requires the use of a range operators such as < (Less than) in the criteria.

1. Open a query in Design View.


2. Position the cursor in the criteria field for a numeric field of your choice and type, for
example ;
a. >500 will display all records with a value of greater than 500.
b. <=75000 will display all records with a value of 75000 or less.
3. Run the query

Note: The £ sign is not needed when using range of value queries.

Access operators and their meaning


Operator Meaning Example Result
= Equal to =”Sales” Finds only those records with “Sales” as the
field value.
< Less than <100 Finds all the records with values less than 100
in that field.
<= Less than or Equal <=100 Finds all the records with values less than or
to equal to 100 in that field
> Greater than >100 Finds all the records with values greater than
100 in that field
>= Greater than or >=100 Finds all the records with values greater than
Equal to or equal to 100 in that field
<> Not equal to <>”Sales” Finds all the records with values other than
Sales in the field
And Both conditions Created by adding criteria Finds all records where the conditions in both
must be true on the same line of the fields are true
query design grid to more
than one field
Or Either condition “CA” or “NY” or “UT” Finds all the records with the value of “CA”,
can be true “NY”, or “UT” in the field
Like Compares a string Like “Sales” Finds all records with the value of “Sales” at
expression to a the beginning of the field
pattern
Between Finds a range of Between 5 and 10 Finds all records with the values of 5 – 10
values (Inclusive) in the field
In Determines if the In(“CA”, “NY”, “UT”) Finds all records with the value of “CA”, ”NY”,
value is in the or ”UT” in the field
string
Not Same as not Not “Sales” Finds all records with values other than Sales
equal to in the field
Is Null Finds Nulls Is Null Finds all records where no data has been
entered in the field
Is not Null Finds all records Is not Null Finds all records where data has been entered
not Null in the field.

6
Access 2007 Quick guide to queries
Average Values
Avg is another summary calculation. It calculates the average of all values in a field. It is selected
from the Total: row in the query grid, in a similar way as Count and Sum. All such summary
calculations can be combined with selection criteria to obtain statistics for certain parts of the data.

Maximum and Minimum Values


Max and Min are summary calculations that will show the Maximum and minimum values of
specified fields for all selected records. A range of the highest and lowest values in a table can be
displayed using the TopValues property.

Calculated Fields in Queries


When a calculation is required in a query, based on the contents of one or more of the available
fields, a calculated field may be used. This is a field that the user creates, to display the results of a
calculation defined with an expression. The expression may involve one or more numerical fields,
and involved and mathematical calculations. It allows, for example, two fields to be multiplied
together, or for a price field to be multiplied by a percentage, or discount applied to all values in a
particular field.

The name of the new field is entered in the Field row of a query grid, followed by a colon, :. The
expression is then entered, with field names enclosed in the square brackets.

Once a calculated field is defined, it can be used like any other field. It can be used for selection, for
sorting or appear in forms and reports.

The standard mathematical symbols are Add +, subtract -, Divide / and multiply *.

1. Ensure you are in the Design View of a query.


2. Select the field your wish to include in your query.
3. In an empty column enter an expression in to the Field: box.
For example Total Amount:[Price]*[Number Sold]
4. Total amount becomes a new field name, Price and Number Sold are the fields involved in
the calculation, and * is the mathematical operator. This is a new calculated Field that will
multiply the price of each product by the number of each product sold, and call the answer
Total Amount.
5. Run the Query

7
Access 2007 Quick guide to queries
Parameter Queries
If a query is being run frequently and all that is changing is the selection criteria for one or two fields,
then a Parameter Query can be used. A parameter Query displays a dialog box prompting for the
criteria information. Once the information is entered, the query is performed, with the entered
value being used as the selection criteria. The next time the query is run, a different value can be
entered and used, without having to change the query design.

Parameters can be used together with other types of query, such as partial queries using wildcards
and range queries.

A query can contain as many parameter values as required. Simply set a parameter value for each
required field. Every time the query is run, a dialog box will appear for each field that contains a
parameter value. The dialog boxes will appear in the order of the field names in the query grid, from
left to right.

1. Ensure you are in the design view of a query.


2. Add the fields you require to the query grid.
3. To create the parameter query in one of the fields, enter a square bracket “[“ followed by
instructions to the user which instructs them to what they need to enter, followed by a
closing square bracket “]”.
For Example: [Enter Town Required]

Note: Ensure that the prompt text between the square brackets is not the same as any field name.
This will confuse Access.

4. Run the Query.

A dialog box appears. Notice the text entered


between the [ ] in the criteria box appears as the
prompt text. The value entered in the entry box
will be used as the criteria for Town.

5. Enter the Town you wish to find and click ok. The
query will run showing all records where town
equals what you entered into the prompt box.

8
Access 2007 Quick guide to queries
Using wildcard selection
Parameters can be used with wild card selections.

1. Return to the query design view.

2. In the criteria box for the a field you can enter;

Like [Enter Letter Required]&*

3. Run the query. When the dialog box appears, enter a


letter and click ok.

When the query runs it replaces the [Enter Letter Required] with the value entered in the
prompt box as the criteria.

4. The query will show all records where the field starts with the lettered entered.

Using range selections


Parameters can be used with range selections. The user is able to select records between values.

1. Return to the query design view

2. In the criteria box for your field you can enter;

>=[From Lower Value] and <=[To Upper


Value]

3. Run the query.

This is going to create TWO prompt boxes.

a. When the first dialog box appears, enter the


lowest value you require then click ok.

b. When the second dialog box appears, enter


the highest value and click ok.

4. The query will show all records which have a value between the values you entered.

You might also like