Ms-Access Queries
In a well-designed database, the data that you want to present through a form or report is
usually located in multiple tables. A query can pull the information from various tables and
assemble it for display in the form or report. A query can either be a request for data results
from your database or for action on the data, or for both. A query can give you an answer to a
simple question, perform calculations, combine data from different tables, add, change, or
delete data from a database. Since queries are so versatile, there are many types of queries
and you would create a type of query based on the task.
Access lets us to create queries in two ways:
1. Using the query wizard.
2. Using the design view.
Major query types Use
Select To retrieve data from a table or make calculations.
Action Add, change, or delete data. Each task has a specific type of
action query. Action queries are not available in Access web apps.
Creating a Query with the Simple Query Wizard
A simple query strips away fields so that one can see only the fields that one wants to see.
To create a simple query using the Query wizard, do this :-
1. Display the Create tab on the Ribbon and click the Query Wizard button.
2. Select Simple Query Wizard from the New Query dialog box and click OK. Access
displays the first window of the Simple Query Wizard.
3. Use the Tables/Queries list box to choose the first table or query that you want to use
fields from. When you select a table or query, fields from that object appear in the
Available Fields list box.
4. Move the fields you want to use in the query from the Available Fields list to the Selected
Fields list by double-clicking a field name (or by selecting the field name and then
clicking the > button).
5. Click Next button.
6. Choose the type of query you want: Detail or Summary. Depending on your selection, do
one of the following:
If you choose a summary query, click the Summary Options button.
If you choose a detail query, click Next and jump to Step 8.
The Summary Options window displays, where you tell the wizard how to summarize
each field.
7. Choose how to summarize your data and click OK to close the Summary Options dialog
box. Then click Next to see the next window of the wizard.
8. Type a name for the query in the box at the top of the window. Choose from these
options:
Open the Query to View Information: This option shows you the query in
Datasheet view.
Modify the Query Design: This option shows you the query in Design view.
Display Help on Working With the Query: Click this check box if you want to see
the help screen that covers working with a query.
9. Click Finish to view the query.
Creating Query using the Design View
When we use the design view we have more control over the query and Access 2007 lets us
create complete queries in the design view.
To create queries using the Design View, follow these steps:
1. Click under the CREATE tab , in the Others group and then click NEW QUERY :
OBJECT.
2. Show Table dialog box appears. In this dialog box, click Simple Query Wizard and
then click OK.
3. In the Show Table dialog box, add the tables which you want in the query and then
click ADD button. After that click CLOSE button.
4. Drag the fields you want in your query from the tables to the field : row.
5. The Table : row automatically gets filled up.
6. The sort order determines how the records are ordered when the query executes.
Sort is specified in the drop down list of Sort : row. From the drop down list box
choose ascending, descending or Not sorted. The default is Not sorted.
7. In the Criteria : row, add any criteria you want in the query report.
8. To view the query, click under the DESIGN tab , in the RESULTS group, click on the
RUN button. You will see all the information from the fields you selected in a
datasheet view
9. Click the SAVE button and enter a name for the Query.
Query Criteria
In order to control which records are displayed, you must define criteria in a Query. The most
common type of Query is the Select Records Query which will be discussed below.
To Define Criteria for your Query:
1) Position your cursor in the criteria row in the field for which you wish to define the criteria for
2) Enter the criteria
Query criteria help you to retrieve specific items from an Access database. If an item matches
with all the criteria you enter, it appears in the query results. When you want to limit the results
of a query based on the values in a field, you use query criteria.
A query criterion is an expression that Access compares to query field values to
determine whether to include the record that contains each value.
Some criteria are simple, and use basic operators and constants. Others are complex,
and use functions, special operators, and include field references.
To add some criteria to a query, you must open the query in the Design View.
You then identify the fields for which you want to specify criteria.
Example
Let’s look at a simple example in which we will use criteria in a query. First open your Access
database and then go to the Create tab and click on Query Design.
In the Tables tab on Show Table dialog, double-click on the tblEmployees table and then close
the dialog box.
Let us now add some field to the query grid such as EmployeeID, FirstName, LastName,
JobTitle and Email as shown in the following screenshot.
Let us now run your query and you will see only these fields as query result.
If you want to see only those whose JobTitle are Marketing Coordinator then you will need to
add the criteria for that. Let’s go to the Query Design again and in Criteria row of JobTitle enter
Marketing Coordinator.
Let us now run your query again and you will see that only Job title of Marketing Coordinators
are retrieved.
If you want to add criteria for multiple fields, just add the criteria in multiple fields. Let us say we
want to retrieve data only for “Marketing Coordinator” and “Accounting Assistant”; we can
specify the OR row operator as shown in the following screenshot −
Let us now run your query again and you will see the following results.
If you need to use the functionality of the AND operator, then you have to specify the other
condition in the Criteria row. Let us say we want to retrieve all Accounting Assistants but only
those Marketing Coordinator titles with “Pollard” as last name.
Let us now run your query again and you will see the following results.
Ms-Access - Action Queries
In MS Access and other DBMS systems, queries can do a lot more than just displaying data, but
they can actually perform various actions on the data in your database. Action queries are
queries that can add, change, or delete multiple records at one time. When the action query is
called, the database undergoes a specific action depending on what was specified in the query
itself. This can include such things as creating new tables, deleting rows from existing ones and
updating records or creating entirely new ones.
Action queries are very popular in data management because they allow for many records to be
changed at one time instead of only single records like in a select query.
Ms- Access provides 4 different types of Action Queries −
1. Append Query – takes the set results of a query and "appends" (or adds) them to an
existing table.
2. Delete Query – deletes all records in an underlying table from the set results of a query.
3. Make Table Query – as the name suggests, it creates a table based on the set results
of a query.
4. Update Query – allows for one or more field in your table to be updated.
An action query cannot be undone. You should consider making a backup of any tables that you
will update by using an update query.
Uses of an Action Query
A Microsoft Action Query can perform the following types of tasks:
1. Update certain information in a specified group of records using an Update Query
2. Append data from one table into another using an Append Query
3. Delete specified records from one or more tables using a Delete Query
4. Create a new database table from specific records held in the database using a Make-
Table Query
Creating an Append Query in Microsoft Access
A Microsoft Access append query adds (appends) records from the database table that you are
using to another database table. If you want to append records in a database, the table that you
want to append records to must exist. Records can be appended to a table in the current
database that you are working in, or into another Microsoft Access database.
Microsoft Access append queries are good for adding data to a different table where the data is
based upon a selection criteria. However, append queries are not always the most efficient way
of adding records to another database. If you need to add all records and fields from one table
to another table, the append query is not the best way to do it. Using Copy and Paste options in
this case would be the best solution.
When you decide to work with an append query, you should ensure that you are aware of:
If you are attempting to append records to another (external) database, you will need to
know the name and location of the database.
If there is a Primary Key field in the database table that you are appending records to,
the records that you are adding will not be allowed to contain either a duplicate Primary
Key value or a Null value. If this happens, Microsoft Access will not append the data and
you will not be warned.
How to create a Microsoft Access Append Query:
1. Create a SELECT query to determine the records that will be appended. Apply any
required query criteria.
2. In the query design view, click on the drop-down arrow to the right of the Query Type
button and choose Append Query.
3. In the Append dialog box, select the table that you want to Append To from the list of
tables in the current database, or select to append this to another database and either
Browse to this, or enter the full file path including the database name.
4. If needed, modify the query further so that the correct fields will be appended with the
desired new data.
5. Click on the Run (!) button to run the action query.
6. When informed of the number of records to be appended in the Microsoft Access dialog
box, click Yes.
7. Close the query, saving if required.
Creating a Delete Query in Microsoft Access
A Microsoft Access delete query deletes records from a single database table or database
tables. Of all of the different action queries available in Microsoft Access (Append Queries,
Update Queries, Make-Table Queries and Delete Queries) the delete query is one of the most
dangerous. Unlike the others mentioned, the Microsoft Access delete query will remove records
from your tables permanently and forever.
As with the other types of action queries, the delete query will work with a group of records that
meet specified criteria that you apply. You can use the delete query to remove all records or
only records that meet the defined criteria.
If you wish to use the delete query to remove records from multiple tables rather than just a
single database table, you will need to ensure that:
You have relationships defined between related tables in your Microsoft Access
database.
You have enforced the Referential Integrity for the relationships between your chosen
tables.
You have opted to Cascade Delete Related Records for the relationship type.
If you are using the delete query to remove records from multiple tables that are related in a
One-To-Many relationship without having defined the option to Cascade Delete Related
records, Microsoft Access will only delete the records from one table at a time. If this is the
case, you must delete the records from the many side of the relationship first (to ensure against
orphaning records), and then delete the records from the one side of the relationship.
Note: Due to the permanent effects of working with a delete action query, you should always
make a backup copy of your tables, or your database before attempting this option.
How to create a Microsoft Access Delete Query:
1. Create a SELECT query to determine the records that will be deleted. Apply any
required query criteria.
2. In the query design view, click on the drop-down arrow to the right of the Query Type
button and choose Delete Query.
3. If needed, modify the query further so that the correct fields will be deleted with the
desired new data.
4. Click on the Run (!) button to run the action query.
5. When informed of the number of records to be deleted in the Microsoft Access dialog
box, click Yes.
6. Close the query, saving if required.
Create a New Table using a Make-Table Query
The Make-Table query is classed as one of the Action Queries, however this is different from
the other types. Rather than modifying the data contained in an existing database table, a
Make-Table query creates a new database table from the results of the query. We can create
the new table based upon limiting criteria using the make-table action query.
If we take a look at the following scenario, and example, we can see why and how to use this
object.
Creating the Make-Table Query
1. Create a new query, use the Customers and Orders tables.
2. From the Query Type button on the toolbar, select Make Table
button.
3. The Make Table dialog box appears where you should enter the name for the new table.
Here we can also select whether we want to create the new table in the current
database or in another database. Ensure that the current database is selected and click
OK.
4. Select the fields from the required tables.
5. Specify the required criteria in their corresponding fields.
6. To check that the results are returned that we expect, click on the datasheet button
on the toolbar. Once you have verified this, switch back to query design view.
7. In query design, deselect the Show: property for those fields, that we do not need to be
visible in our new table.
8. Click on the Run button on the toolbar. Microsoft Access now displays a message to
indicate how many records will be copied to the new table.
9. Click Yes to complete the query, and create the new table.
After completing the Make-Table query action, check your results by opening the new table that
you have created in the database window.
Note: When creating tables using the make-table query, the fields in the new table will inherit
the data types and field sizes from the queries underlying tables. No other properties will be
transferred. To define a Primary Key or other properties in the newly created table you will need
to edit this in design view.
Updating Records in Microsoft Access with an Update Query
A situation may arise where we need to update many records in a database table when certain
information changes or needs to be modified.
A Ms-Access Update Query updates specified values in a table for all records or for those
records that match a specified criteria.
It is possible to update records manually in a database table, either via a form or through the
tables datasheet, however this may take a very long time. Changing records manually is not
only time consuming, it is also inefficient, and lends itself to errors as you update the records.
The best way to handle this type of event, the updating of many records in a database table, is
to use an Update Query to make the changes to data in one operation. It will save time, and
eliminate the possibility of manual errors.
Note: Due to the permanent effects of working with an update action query, you should always
make a backup copy of your tables, or your database before attempting this option.
Steps to create a Ms-Access Update Query:
1. Create a SELECT query to determine the records that will be updated. Apply any
required query criteria, and view the data that will be updated by pressing the Datasheet
button.
2. In the query design view, click on the drop-down arrow to the right of the Query Type
button and choose Update Query.
3. After you are satisfied that the information to be updated is correct Run the query using
the Icon.
Ms-Access – Parameter Queries
When you want a query in Access to ask for input every time that you run it, you can create a
parameter query.
Parameter : A parameter is a piece of information you supply to a query right as you run it.
Parameters can be used by themselves or as part of a larger expression to form a criterion in
the query. You can add parameters to any of the following types of queries:
Select
Crosstab
Append
Make-table
Update
Criteria : Criteria are the “filters” you add to a query to specify which items are returned when
you run the query.
A parameter query is one of the simplest and most useful advanced queries you can create. It
allows you to create a query that can be updated easily to reflect a new search term. When you
open a parameter query, Access will prompt you for a search term and then show you query
results that reflect your search.
When you’re running parameter queries, search terms act as variable criteria, which are query
criteria that change each time you run the query. For instance, let's say we own a bakery and
want to create a query that will quickly look up orders that were placed on a certain date. We
could create a parameter
ameter query with variable criteria in the Date field.. This way, each time we
run the query a dialog box will appear prompting us to enter the date we want our query to
search for.
We’ll enter the date we want, then Access will run the query using the da
date
te we entered as a
search term.
To create and run a parameter query:
1. Create a query as you normally would, modifying the table joins if necessary, selecting
the fields to include in your query, and adding any non
non-variable
variable criteria to the appropriate
fields in the Criteria: row.
2. Locate the field or fields where you want the variable criteria to appear, and place your
cursor in the Criteria: row.
3. Type the phrase you want to appear in the prompt that will pop up each time you run
your query. Make sure to enclose the phrase in brackets [ ]. For example, in our
parameter query that searches for orders placed on a certain date, we might type our
criteria
ia like this: [What date?].
4. On the Query Design tab, click the Run command to run your query. A dialog box will
appear with the prompt you specified. Enter your search term, then click OK to view your
query results.
Note : To run an existing parameter qu
query, simply open it.
Specify parameter data types
You can also specify what type of data a parameter should accept. You can specify the data
type for any parameter, but it is especially important to specify the data type for numeric,
currency, or date/time data. When you specify the data type that a parameter should accept,
users see a more helpful error message if they enter the wrong type of data, such as entering
text when currency is expected.
Note: If a parameter is configured to accept text data, any input is interpreted as text, and no
error message is displayed.
To specify the data type for parameters in a query, follow these steps:
1. With the query open in Design view, on the Design tab, in the Show/Hide group, click
Parameters.
2. In the Query Parameters dialog box, in the Parameter column, type the prompt for
each parameter for which you want to specify the data type. Make sure that each
parameter matches the prompt that you use in the Criteria row of the query design grid.
3. In the Data Type column, select the data type for each parameter.
Combine parameters with wildcards for more flexibility
As with normal criteria, you can combine parameters with the Like keyword and wildcard
characters to match a wider range of items. For example, you might want your query to prompt
for a country/region of origin, but to match any value that contains the parameter string. To do
this:
1. Create a select query, and then open the query in Design view.
2. In the Criteria row of the field you want to add a parameter to, type Like "*"&[, the text
that you want to use as a prompt, and then ]&"*".
When you run the parameter query, the prompt appears in the dialog box without the square
brackets, and without the Like keyword or wildcard characters:
After you enter the parameter, the query returns values that contain the parameter string. For
example, the parameter string us returns items where the parameter field has a value of
Australia and items where the value is USA.
Return items that don’t match the parameter
Instead of having the query return items that match your parameter, you might want the query to
return items that don’t match it. For example, you might want to prompt for a year and then
return items where the year is greater than the one you entered. To do this, type a comparison
operator to the left of the first square bracket that encloses the parameter prompt, for
example,>[Enter a year:].
Tips for writing parameter queries
Ideally, the prompt you create for your query should make it clear what type of
information the search term should be, and what format it should be entered in. For
example, to guarantee that people enter a search for a date in the format we use in our
database, we could write the following in the Criteria: row of the Date field: [What
date? (mm/dd/yy)].
The simplest parameter query will give you an exact-match criteria, meaning the query
will search for the exact text you enter in the prompt. However, you can turn any type of
criteria into a variable criteria. Simply type your prompt text in brackets in the part of the
criteria where you would normally put a search term.
For example, in a normal query we could find orders that were placed between two
dates by using the criteria Between x AND y, and replacing the x and y with the first and
second dates, respectively. To turn this into a parameter criteria, we would simply
replace the x and y with the text we want to appear in the prompt. Our variable criteria
might look like this: Between [Enter the start date:] AND [Enter the ending date:].
These prompts would appear: