You are on page 1of 24

Creating Queries In Microsoft Access 2010

Sandra Dyke, 2012

Access 2010 Tutorial

Contents
Creating Queries ................................................................................................................... 2 Single Table Query ............................................................................................................... 3 Multi-table Query................................................................................................................... 6 Adding Calculations ............................................................................................................ 11 Joining Text Fields .............................................................................................................. 12 Using the Expression Builder .............................................................................................. 16 Creating Queries using Other Queries ................................................................................ 19 Query using Totals and Group By ....................................................................................... 22

This tutorial assumes the Microsoft Access 2010 Tutorial has been completed to create the Basic Sales System Database itself. The 16 pages of this tutorial repeats the query part of the general tutorial

Sandra Dyke, 2012

Page 1

Access 2010 Tutorial Creating Queries

Creating Queries
Now that the data has been added to the tables, it can be used to produce results related to a set of conditions given to it. This is known as queries. Queries can be from one table or from a number of tables. Queries Select Create Query Design

Steps 1. 2. 3. 4. 5. 6.

Select the required table(s) Select the fields required Include any conditions Select display requirements Run Query Save Query with a suitable name by closing it, or clicking the Save Button at the top of the Access screen

Important: When creating queries check that the results seem to be reasonable. When there is only a small dataset, look at the data and determine what the results should be before running the query and checking that the results obtained are the same.

Sandra Dyke, 2012

Page 2

Access 2010 Tutorial Creating Queries

Single Table Query


Create a Customer Phone List This query will produce a list of customer first name, last name and phone number. Select the Customer table from the Show Table Dialogue and click Add

Select the CustomerFirstName, CustomerLastName and CustomerPhoneNumber by using the dropdown arrow in the Field row, by dragging the attribute from the Customer table shown, or by double clicking the attribute in the Customer table shown.

Everything is going to be shown so nothing further is required.

Sandra Dyke, 2012

Page 3

Access 2010 Tutorial Creating Queries

Run the query by clicking Run

Result

Changing the views: Datasheet shows the results SQL shows the query written in the SQL language (covered in DAT501) Design shows the query using the GUI view we started the query with.

Sorting Lets sort this query by the customers last name. Change the query to Design View mode and select Ascending for CustomerLastName

Sandra Dyke, 2012

Page 4

Access 2010 Tutorial Creating Queries

Run the Query again

Now the results are sorted by the customers last name Close the query and give it a suitable name when prompted (eg qCustomerPhoneList).

Sandra Dyke, 2012

Page 5

Access 2010 Tutorial Creating Queries

Multi-table Query
Create a list of customer names and the dates they have purchased items. This is a query across 2 tables: Customer and Sales Click Create Query Design Add Customer and Sales Notice that this time, the relationship between the tables is also shown (a customer may have many sales, but a sale can only relate to one customer)

Select CustomerFirstName, CustomerLastName from the Customer table and the SaleDate from the Sale table. Sort by the sale date.

Result

Save this query with a suitable name (eg qCustomerSaleDate).

Sandra Dyke, 2012

Page 6

Access 2010 Tutorial Creating Queries

Using Criteria Both queries so far have returned all of the records from the tables selected. It is possible to select only some of the records by using the Criteria section. Adjust the query by showing only Bill Smiths sales by including Bill in Customer FirstName and Smith in CustomerLastName.

Result

Change the query to show only those sales which occurred in February 2012 This can be done by typing >= 1/2/12 And <= 29/2/12 in the SaleDate criteria. When the query is run, Access adds the # around each date.

Result

Sandra Dyke, 2012

Page 7

Access 2010 Tutorial Creating Queries

Multi-table Query with 3 Tables Create a query which shows the Employee Name, Date, Quantity, and Inventory Name for sales for each Employee. Sort by Employee Last Name This query is across 3 tables: Employee, Inventory and Sale.

Sandra Dyke, 2012

Page 8

Access 2010 Tutorial Creating Queries

Result

Save the query with a suitable name. Adjust the query to show only the sales which Gail Smith has made.

Result

Experiment with other conditions.

Sandra Dyke, 2012

Page 9

Access 2010 Tutorial Creating Queries

Multi-table Query with 4 Tables Create a query which shows the customer name, address, Inventory Name, Date, Quantity, Price, and Employee Name for each sale. Sort by customer last name.

Result

Save the query with a suitable name (qCustomerSale). Experiment with different conditions.

Sandra Dyke, 2012

Page 10

Access 2010 Tutorial Creating Queries

Adding Calculations
The price shown in the Sale table represents the unit price for the items sold. It is possible to add the total price for the sale to the record by using a query. Values which can be calculated from the other data in the table are generally not included within the table due to the normalisation rules and to ensure an efficient database. Open qCustomerSale and rename it by going to File Save Object As (qCustomerSaleTotal).

The format to add a calculated attribute is Column Name: Table.Attribute * Table.Attribute In the first empty cell type TotalSalePrice: Sale.SaleQuantity*Sale.SaleUnitPrice

Run the query Result

Once the query has been run, the calculation will show [ ] around the table and attribute names:

Sandra Dyke, 2012

Page 11

Access 2010 Tutorial Creating Queries

Joining Text Fields


A normalised database will usually have the customer name and address in more than 1 field eg CustomerFirstName,CustomerLastName, CustomerAddress, CustomerSuburb, CustomerCity and CustomerPostCode. However, when these attributes are used, usually the first and last name will need to be together as will the city and postcode. Because the length of text contained in these fields could be different lengths, it is easier to join the fields together as a new attribute and then use these new fields. Text fields can be joined together using the & operator and spaces can be introduced by using (double quote space double quote). Create a simple query using all of the fields from the Customer table and save it with a suitable name eg qCustomerDetails:

Switch into Design View In the first empty cell type: CustomerName: CustomerFirstName & & CustomerLastName

Run the Query Result

Try combining city and postcode into 1 field. To really see the effect of this joining text fields, create a report which shows customer addresses using the separate CustomerFirstName, CustomerLastName attributes as well as the joined attribute CustomerName and see the differences.

Sandra Dyke, 2012

Page 12

Access 2010 Tutorial Creating Queries

Activity Create the following queries. They will be used for the Report Tutorial Results are shown for each query but your results may be different depending on the data contained in your tables. qEmployeeFullName

Result:

Sandra Dyke, 2012

Page 13

Access 2010 Tutorial Creating Queries

qCustomerDetails

Result:

Sandra Dyke, 2012

Page 14

Access 2010 Tutorial Creating Queries

qCustomerSaleInvoice

Result:

Sandra Dyke, 2012

Page 15

Access 2010 Tutorial Creating Queries

Using the Expression Builder


The expression builder allows complex expressions to be constructed quickly. For example to builder the TotalSaleValue for each sale in the Sale table Click on Builder in the Query Setup section This opens a dialogue box where the expression is constructed. Some options have further options available (shown by the + symbol).

Functions include pre-built functions similar to the functions found in Excel:

The active database is included and when this is expanded other elements from the database are shown:

Sandra Dyke, 2012

Page 16

Access 2010 Tutorial Creating Queries

The Operators element gives access to the operators which can be used:

Common Expressions includes regularly used expressions:

To use the expression builder Expand the current database and select the Sales table to gain access to the attributes available

Create the query by giving it a name and then selecting the expressions required

Click Ok

Sandra Dyke, 2012

Page 17

Access 2010 Tutorial Creating Queries

Result

Run the query

Sandra Dyke, 2012

Page 18

Access 2010 Tutorial Creating Queries

Creating Queries using Other Queries


qCompleteSaleDetail To create this query, use the create query deign option but this time select from the Queries tab in the Show Tables dialogue:

Select qCustomerDetails, qCustomerSaleInvoice and qEmployeeFullName queries created above. Drag the CustomerID from qCustomersDetails to qCustomerSaleInvoice and the EmployeeID from qCustomerSaleInvoice to qEmployeeFullName to create the relationships below:

The creation of the relationships is an important step or else the query will match everything with everything (and I mean everything)!

Sandra Dyke, 2012

Page 19

Access 2010 Tutorial Creating Queries

Select the required fields as shown below:

Depending on the data contained in the various tables, the following results may be produced Result:

It is this query which will be used to produce the invoice in the Reports tutorial.

Sandra Dyke, 2012

Page 20

Access 2010 Tutorial Creating Queries

Parameter Queries
A parameter query is one where input of one or more field values is required to run the query. The request for data entry is placed in the criteria row. For example Create a query which returns the customer details only for the customer ID entered. Copy the query qCustomerDetail Select the query File Save Object As qCustomerDetailParameter Open the qCustomerDetailParameter in Design View Add the following as the Criteria for customerID: [Enter Customer ID]

Run the query The first thing which happens is a dialogue box asking for the customer ID to be entered Enter 1 and click OK Then the query is produced

A parameter query can be used as the basis for a report. This means when the report is opened, the user will be asked to input the required data and then the report will be run. It is possible to require more than 1 value to be entered just add more parameter values to the criteria line. Sometimes a dialogue box requesting the entry of a Parameter Value occurs when it is not wanted! This can happen if a textbox is used in a report or form instead of a label. A label allows text to be shown on the report or form but a textbox is used to get the data from the named database attribute. If Access cannot find the named attribute, it will ask for it from the user. These types of errors are generally related to design errors.

Sandra Dyke, 2012

Page 21

Access 2010 Tutorial Creating Queries

Query using Totals and Group By


The Totals option allows records to be grouped and summary statistics to be calculated

qTotalSaleForCustomer is a query which will calculate the total sales a customer has Create the query by using the qCompleteSaleDetail query and the CustomerID, CustomerName and TotalSalePrice attributes. Run the query:

The following result is required if the total sales for each customer from the above are calculated:

Sandra Dyke, 2012

Page 22

Access 2010 Tutorial Creating Queries

Adjust your query to show the Total row by selecting the Totals button on the Design tab. This adds a new line to bottom part of the query for Total and Group By for each field selected.

Change the Group By option for SaleTotal to Sum as shown below by clicking the down arrow.

Run your query Result

It is possible to add additional attributes or amounts to the sum calculated above. For example adding $10 to the total of a customers sales (rather than adding $10 to each sale) Copy and rename the above query to qTotalSaleForCustomerPlus10 Use the qTotalSaleForCustomer query Add a new attribute called Plus10 as shown below

Result:

Sandra Dyke, 2012

Page 23

Access 2010 Tutorial Creating Queries

You might also like