Sie sind auf Seite 1von 17

Using MS Access Queries

Les Coover

i
Table of Contents

Query Overview ...................................................................................................................... 1


Select Queries, Specifying Criteria...................................................................................... 1
Sorting ............................................................................................................................. 1
Criteria (Number Field) 154.01 ..................................................................................... 2
Criteria (Date Field) April 20, 2001 and January 15, 2004 ......................................... 2
Criteria (City Field) Iona OR Cypress Lake...................................................................... 3
Criteria (City Field) Iona OR Cypress Lake, different method.......................................... 3
Criteria Is Null and Is Not Null.......................................................................................... 4
Criteria (First Name) Like and wildcards.......................................................................... 4
Select Queries, Specifying Criteria in Multiple Fields .......................................................... 4
Criteria (Last Name AND City) Gordon AND Fort Myers ................................................. 4
Criteria (Last Name AND City) [Gordon AND Fort Myers] OR [Iona] ............................... 5
Calculated Fields in Queries ............................................................................................... 5
Crosstab Queries ................................................................................................................ 7
Parameter Queries .............................................................................................................. 7
Action Queries..................................................................................................................... 8
Creating a Delete Query .................................................................................................. 8
Create an Update Query.................................................................................................. 9
Create a Make-Table Query (I) ...................................................................................... 10
Create a Make-Table Query (II) ..................................................................................... 10
Creating an Append Query ............................................................................................ 11
Distinct Record and SQL................................................................................................... 11
Using Modules with Queries.............................................................................................. 12
Appendix............................................................................................................................... 14
Car Pool ............................................................................................................................ 14
Work.................................................................................................................................. 14
Mutual Fund ...................................................................................................................... 14
XYZ ................................................................................................................................... 15
Names01 and Names02.................................................................................................... 15
Distinct Records ................................................................................................................ 15
Color.................................................................................................................................. 15

ii
Using MS Access Queries and Filters

Query Overview
One of the most powerful features of Microsoft Access is the query. A query is a
request for specific data contained in a database. There are five types of queries: Select,
Crosstab, Parameter, Action and SQL.1, 2

Select Queries, Specifying Criteria


A select query retrieves specific data from one or more tables based on the criteria you
enter; this is the most common type of query. Criteria are restrictions you place on a query to
identify certain records.3 All examples used in this section are based on the Car Pool
recordset found in the Appendix on page 14. Use this recordset to build a MS database table
and name it Car Pool. Then create a query based on this table and name it Car Pool Query.

Sorting
To sort on one or more fields, arrange the fields in the design grid in the order you want
to perform the sort. Access sorts on the leftmost field first, then on the next field to the right,
and so on.4

Car Pool Query


Last Name First Name City Date Number
Birch Brian Fort Myers 1 /5 /1999 20.1
Coner Chad Fort Myers 1 /5 /1999 1.1
Gordon Anna Fort Myers 4 /21/2005 21.58
Gordon Georgia Cypress Lake 2 /15/2000 54.8
Newton Danial Forest Island 1 /5 /1998 12548
Orion Jill Iona 1 /15/2004 25486
Rig Karen Fort Myers 4 /20/2001 253.45
Smith Kirk Fort Myers 4 /4 /2002 879.25

1
H. Albert Napier and P.J. Judd, Microsoft Access 2000 Comprehensive Course (Boston: Thompson
Learning, 2001), p. AB 11. ISBN: 9-780619-05150-1
2
Timothy J. OLeary and L.I. OLeary, Microsoft Access 2002 (New York: McGraw-Hill, 2002), p. AC3.11.
ISBN: 0-07-247244-8
3
Napier, p. AI 40.
4
Napier, p. AI 36.

1
Using MS Access Queries and Filters

Criteria (Number Field) 154.01


List all records that have a value in their Number field greater than or equal to 253.45.
Arrange these records in alphabetical order by First Name, Last Name. Type >=253.45 in the
Criteria field for Number, click in a vacant cell then run the query.

Car Pool Query


Last Name First Name City Date Number
Newton Danial Forest Island 1 /5 /1998 12548
Orion Jill Iona 1 /15/2004 25486
Rig Karen Fort Myers 4 /20/2001 253.45
Smith Kirk Fort Myers 4 /4 /2002 879.25

Criteria (Date Field) April 20, 2001 and January 15, 2004
List all records that have a value in their Date field April 20, 2001 and January 15,
2004. Arrange these records in alphabetical order by First Name, Last Name. In the Date
Criteria Field, type between 4/20/01 and 1/15/04, click in an empty cell, then run the query.

Car Pool Query


Last Name First Name City Date Number
Orion Jill Iona 1 /15/2004 25486
Rig Karen Fort Myers 4 /20/2001 253.45
Smith Kirk Fort Myers 4 /4 /2002 879.25

2
Using MS Access Queries and Filters

Criteria (City Field) Iona OR Cypress Lake


List all records that have a value in their City field of Iona or Cypress Lake. Arrange
these records in alphabetical order by First Name, Last Name. In the City Criteria Field, type
In(Iona,Cypress Lake), click in an empty cell, then run the query .5

Car Pool Query


Last Name First Name City Date Number
Gordon Georgia Cypress Lake 2 /15/2000 54.8
Orion Jill Iona 1 /15/2004 25486

Criteria (City Field) Iona OR Cypress Lake, different method


This method accomplishes the same thing. Type Iona in the first City Criteria field, then
click in the cell below it and type Cypress Lake. Click in a vacant cell and run the query.

Car Pool Query


Last Name First Name City Date Number
Gordon Georgia Cypress Lake 2 /15/2000 54.8
Orion Jill Iona 1 /15/2004 25486

5
When you want to quickly pull a list that contains quite a few specific names, right-click on the appropriate
Criteria field and select Zoom. Then type In(name01,name02,name03 etc.) .
3
Using MS Access Queries and Filters

Criteria Is Null and Is Not Null


Sometimes you may want to retrieve records that contain blanks in one or more of their
record fields. To do this use the Null criteria by typing null in one of the criteria fields.
Suppose you want the set that does not contain any blanks, in this case type the words not
null in one or more of the criteria fields.

Criteria (First Name) Like and wildcards


Suppose you know the first name of a member of the car pool begins with G, but you
cant remember anything else. Type like G* in the First Name Criteria field (use wildcards to
search for text patterns, the asterisk is the wildcard symbol), click in a blank field, then run the
query.

Car Pool Query


Last Name First Name City Date Number
Gordon Georgia Cypress Lake 2 /15/2000 54.8

Select Queries, Specifying Criteria in Multiple Fields


Use the OR condition when you want to select records where either condition can apply.
Use the AND criteria when you want to select records where both conditions apply. All
examples used in this section are based on the Car Pool recordset found in the Appendix on
page 14.

Criteria (Last Name AND City) Gordon AND Fort Myers

Car Pool Query Always read Criteria in


the Design Grid from left
Last Name First Name City Date Number to right, one row at a
Gordon Anna Fort Myers 4 /21/2005 21.58 time, from top to bottom.

4
Using MS Access Queries and Filters

Criteria (Last Name AND City) [Gordon AND Fort Myers] OR [Iona]

Car Pool Query


Last Name First Name City Date Number
Gordon Anna Fort Myers 4 /21/2005 21.58
Orion Jill Iona 1 /15/2004 25486

Calculated Fields in Queries


A calculated field gives you the results of a calculation in a query. Your can create your
own calculation or use one of Accesss predefined functions6, 7. The example in this section is
based on the Work recordset, found in the Appendix on page 14.

1. Build a query based on the Work recordset and name it Work Query.
2. In Query Design View, in the Field box enter Gross Pay:
3. Right-click and choose Build8. Use Functions, Built-In Functions to locate the IIF
function and double-click it.
4. Now use the Queries, Work Query list to complete the function as follows:

6
Napier, pp. AA 50.AA 52
7
OLeary, p. AC4.33.
8
You can also right-click and choose Zoom, then type the function in, if you know the exact syntax.
5
Using MS Access Queries and Filters

5. Click OK, then click somewhere outside the Field box. The design grid should look like
this

6. Now run the query.

Work Query
Last Name First Name Hours Pay Rate Gross Pay
Argon Andrew 20 $7.00 140
Birch Cindy Salary Salary
Burke Erin Salary Salary
Delucchi Bill 30 $14.00 420
Ernster Barbara 29 $9.00 261
Gapasin Ken Salary Salary
Helfand Eric 18 $9.50 171

6
Using MS Access Queries and Filters

Crosstab Queries
Crosstab queries summarize table data. The ability to see table data grouped both
horizontally and vertically is particularly helpful for comparing data in tables where there are
multiple entries for one item, such as the price changes of specific mutual funds over a time
period.9, 10 The example in this section is based on the Mutual Fund recordset, found in the
Appendix on page 14. Build a table which includes all data and fields in the Mutual Fund
recordset, and name it Mutual Fund.

1. In the Queries window, click New, Crosstab Query Wizard, OK.


2. Select Table: Mutual Fund, Next
3. Place Mutual_Fund in Selected Fields, then click Next.
4. Select Date and click Next.
5. Select Date again, and click Next.
6. Select Avg in Functions: section, then click Next.
7. Rename the Crosstab query Mutual Fund_Crosstab, and then click Finish.
8. Select the Total Of Price column and drag it to the right of the date columns.
9. Click Design View and change Total Of Price to Avg Of Price.
10. Right-click on the Avg Of Price column and choose properties. Set Format to Fixed and
Decimal Places to 2. Copy the Avg of Price column and paste it into a blank column.
11. Copy the Avg of Price column and paste it into a blank column Change the Total field to
Min and Avg Of Price to Min Of Price.
12. Do Step 11 again but use Max.
13. Run query

Mutual Fund_Crosstab
Mutual_Fund 5/24/2005 5/25/2005 5/26/2005 Avg Of Price Min Of Price Max Of Price
AALP 50 50.5 49 49.83 49.00 50.50
BRBT 39 40 39.5 39.50 39.00 40.00
CHGA 40 40 42.5 40.83 40.00 42.50

Parameter Queries
A parameter query is an advanced select query that asks you to enter search criteria. A
dialog box is displayed that prompts you for information. For example, a parameter query
might ask for beginning and ending dates, then display records that contain dates between the
two specified values.11, 12, 13 All examples used in this section are based on the Car Pool
recordset found in the Appendix on page 14.

9
Napier, pp. AA 46.AA 50.
10
OLeary, pp. AC4.35AC4.41.
11
Napier, pp. AA 24AA 28.
12
OLeary, p. AC3.11.
13
http://office.microsoft.com/en-us/assistance/HA011170771033.aspx
7
Using MS Access Queries and Filters

1. Build a query based on the Car Pool recordset, and open it in Design View
2. Type >=[Enter Date:] in the Date Criteria field and click in an empty cell.

3. Click Query in the menu and choose Parameters.


4. In the Parameters Dialog box in the Parameter column type [Enter Date:] (the brackets
and information enclosed must match the contents of the Criteria cell.
5. Change the Data Type to Date/Time.
6. Click OK and run the query.
7. Enter 1/1/2003 and click OK. Your query should look as follows:

Car Pool Query


Last Name First Name City Date Number
Gordon Anna Fort Myers 4 /21/2005 21.58
Orion Jill Iona 1 /15/2004 25486

Action Queries
From the previous examples, it can be seen, that select queries retrieve records, now
we turn to action queries. An action queries makes changes to many records in one
operation. There are four types of action queries: delete, update, append, and
make-table.14, 15

Creating a Delete Query


The example used for delete query is based on the Car Pool recordset found in the
Appendix on page 14. A delete query removes a group of records from one or more tables.

1. Copy the Car Pool table and name it Car Pool Modified.
2. In Query View:
Click Create query by using a wizard
Select the Car Pool Modified table
Select all available fields
Create the query and name it Car Pool Modified Query
3. Open the Car Pool Modified Query in Design View.
4. In the Date Criteria field type <2/1/2000 and click in an area outside the criteria cell.
5. On the menu bar choose:
Query
Delete Query

14
Napier, pp. AA 29.AA 36.
15
OLeary, pp. AC3.11.
8
Using MS Access Queries and Filters

6. Run the query


7. The following prompt appears:

Click Yes

Records older than 2/1/2000 are deleted from the Car Pool Modified table.

Car Pool Modified


Last Name First Name City Date Number
Gordon Anna Fort Myers 4 /21/2005 21.58
Gordon Georgia Cypress Lake 2 /15/2000 54.8
Rig Karen Fort Myers 4 /20/2001 253.45
Smith Kirk Fort Myers 4 /4 /2002 879.25
Orion Jill Iona 1 /15/2004 25486

Create an Update Query


The example used for update query is based on XYZ recordset found in the Appendix
on page 15. An update query makes global changes to a group of records in one or more
tables.

1. Create a query based on a table that contains the XYZ recordset, name it XYZ Query.
2. Switch to Design View
3. On the menu bar choose:
Query
Update Query
4. Key 10 in the Update To field for Z.
5. Run the query and click Yes to accept the update.

The values in the Z column of the XYX table are now 10 instead of 8.

9
Using MS Access Queries and Filters

Create a Make-Table Query (I)


The example is based on Car Pool recordset found in the Appendix on page 14. A
make-table query creates a new table from all or part of the data in one or more tables.

1. Use Create query by using wizard and build a query based on all the fields in the Car
Pool table. Name the query Car Pool Update Query.
2. Switch to Design View and enter >1/1/2000 in the Date Criteria field.
3. On the menu bar choose:
Query
Make-Table Query
4. In the Make Table dialog box, enter Car Pool Update Table and click OK.
5. Run the query
6. At the prompt, You are about to paste 19 rows into a new table, click Yes.

The Car Pool Update Table appears in Table View and contains the records matching
the criteria specified in the Make-Table query.

Create a Make-Table Query (II)


The example used is based on the XYZ table that contains the recordset found in the
Appendix on page 15.

1. Build a query based on the XYZ table and name it XYZ Calculations Query, then save
it.
2. Switch to Design View and in the first blank column insert a calculated field using the
Expression Builder.16

3. Finish the Expression Builder calculation by clicking OK. You are returned to Query
Design View. Put a checkmark in the Show box for the SumXYZ field.
4. Run the query
5. Switch to Design View and choose Query, Make-Table Query
6. In the Make-Table dialog, make sure Current Database is selected and name the new
table XYZ Calculations Table, then click OK.

16
To start the Expression Builder right-click in the first cell of the blank column and choose Build, now type
SumXYZ:= and click the appropriate symbols (expand the Queries list by double-clicking on the plus sign, and
choose XYZ Calculations Query).
10
Using MS Access Queries and Filters

7. Click in an empty cell and Run the query


8. At the You are about to paste 4 rows into a new table prompt, click Yes.

XYZ Calculations Table is created in Table View. The table contains the field SumXYZ.

Creating an Append Query


This example is based on the Names01 and Names02 recordsets found in the Appendix
on page 15. The following instructions assume the database contains the tables Names01
and Names02.

1. Use Create query by using wizard to build a query based on all the fields in the
Names02 table. Name this query Add Names Query.
2. Switch to Design View and select:
Query
Append Query
3. In the Append dialog, use the drop-down list and select Names01, then click OK.
4. Run the query
5. At the You are about to append 2 rows prompt, click Yes.
6. Switch to Table View and verify that two records have been appended to the Names01
table.
Names01
Last Name First Name
Alpha Alexander
Bravo Jane
Gamma Charles
Delta Judy

Distinct Record and SQL


Situations arise when a recordset contains duplicate records and you need to eliminate
duplicates. The following instructions assume the database contains the table Distinct
Records based on the Distinct Records recordset found in the Appendix on page 15.

1. Create a query based on all the fields in the Distinct Records table, name it Distinct
Records Query.
2. Switch to Design View
3. Click in an area outside of the Design Grid, then right-click and choose Properties.
4. Change the Unique Records property to Yes.
5. Close the Query Properties dialog and run the query.

11
Using MS Access Queries and Filters

Distinct Records Query


Name_Last Name_First
Brown Sara
Smith Bob
Smith James
Smith Linda

Now no duplicate records exist, you could use the Make-Table method to create a table
containing these records.

6. Choose View, SQL View

Notice the SQL code. This code indicates the Unique Records property is set to yes.
Sometimes it is advantageous to examine the SQL code to determine the nature of specific
operations in your query.

SELECT DISTINCT [Distinct Records].Name_Last, [Distinct


Records].Name_FirstFROM [Distinct Records];

Using Modules with Queries


Using the features described above give you many ways to create many forms of
powerful queries, but there are particular situations when other techniques may come in
handy. VBA (Visual Basic for Applications) functions can be included in queries to create
additional operations.

1. Create a table and include the data and fields contained in the Color recordset,
Appendix, page 15. Format the Desc field as Text and the Qty field as Number. Set the
Qty Field Size to Double.
2. Switch to Module View and click New.
3. Click View, Properties Window, and name the module modColor.

12
Using MS Access Queries and Filters

4. In the code Window type the following code:

Option Compare Database


Option Explicit
'determine color code for unique situations
Function CC(Desc As String, Qty As Double) As String
Select Case Desc
Case "Echo"
Select Case Qty
Case 2
CC = "Red"
End Select
Case "Golf"
Select Case Qty
Case 3
CC = "Blue"
End Select
Case "Lima"
Select Case Qty
Case 1
CC = "Green"
End Select
End Select
End Function

5. Save the module, close it, and switch to Query View


6. Create a query based on all the fields in the Color table and name it Color Query.
7. Switch to Design View
8. In the first empty column, in the Field row type: CC_:CC([Desc],[Qty])
9. Click in a vacant cell and run the query

Color Query
Desc Qty CC_
Echo 1
Echo 2 Red
Echo 3
Golf 1
Golf 2
Golf 3 Blue
Lima 1 Green
Lima 2
Lima 3

13
Using MS Access Queries and Filters

Appendix
Car Pool

Car Pool
Last Name First Name City Date Number
Coner Chad Fort Myers 1 /5 /1999 1.1
Birch Brian Fort Myers 1 /5 /1999 20.1
Gordon Anna Fort Myers 4 /21/2005 21.58
Gordon Georgia Cypress Lake 2 /15/2000 54.8
Rig Karen Fort Myers 4 /20/2001 253.45
Smith Kirk Fort Myers 4 /4 /2002 879.25
Newton Danial Forest Island 1 /5 /1998 12548
Orion Jill Iona 1 /15/2004 25486

Work

Work
Last Name First Name Hours Pay Rate
Argon Andrew 20 $7.00
Birch Cindy Salary
Burke Erin Salary
Delucchi Bill 30 $14.00
Ernster Barbara 29 $9.00
Gapasin Ken Salary
Helfand Eric 18 $9.50

Mutual Fund
Instructions assume this recordset is a table in a MS Access application.

Mutual Fund
Mutual_Fund Date Price
AALP 5/24/2005 50
AALP 5/25/2005 50.5
AALP 5/26/2005 49
BRBT 5/24/2005 39
BRBT 5/25/2005 40
BRBT 5/26/2005 39.5
CHGA 5/24/2005 40
CHGA 5/25/2005 40
CHGA 5/26/2005 42.5

14
Using MS Access Queries and Filters

XYZ

XYZ
X Y Z
1 9 8
2 11 8
3 17 8
4 14 8

Names01 and Names02


Names01 Names02
Last Name First Name Last Name First Name
Alpha Alexander Gamma Charles
Bravo Jane Delta Judy

Distinct Records

Distinct Records
Name_Last Name_First
Brown Sara
Smith Bob
Smith Bob
Smith James
Smith Linda

Color

Color
Desc Qty
Echo 1
Echo 2
Echo 3
Golf 1
Golf 2
Golf 3
Lima 1
Lima 2
Lima 3

15

Das könnte Ihnen auch gefallen