Microsoft Access 2003 - Intro Class 5 Working with Queries.

16
Microsoft Access 2003 - Intro Class 5 Working with Queries

Transcript of Microsoft Access 2003 - Intro Class 5 Working with Queries.

Page 1: Microsoft Access 2003 - Intro Class 5 Working with Queries.

Microsoft Access 2003 - Intro

Class 5Working with Queries

Page 2: Microsoft Access 2003 - Intro Class 5 Working with Queries.

2

Queries Vs. FiltersCharacteristics Filters Queries

*Are saved as an object in the database* No Yes

Can be used to select a subset of records in a datasheet

Yes Yes

Can be used to select a subset of fields in a datasheet

No Yes

Its resulting datasheet can be sued to enter and edit data

Yes Yes

Its resulting datasheet can be used to sort, filter, and find records

Yes Yes

Is commonly used as the source of data for a form or report

No Yes

Can calculate sums, averages, counts and other types of summary statistics across records

No Yes

Can be used to create calculated fields No Yes

Page 3: Microsoft Access 2003 - Intro Class 5 Working with Queries.

3

Table Vs. Dynaset

The table includes red, blue, and yellow circles.Given a “Red” criteria, the dynaset only has red circles.

Page 4: Microsoft Access 2003 - Intro Class 5 Working with Queries.

4

???Questions???Let Queries provide the answers!

What data is contained in LastName, FirstName and Phone fields of the Employee Table?

How many employees work in the Accounting Department of the company?

Queries may “answer” questions such as:

Page 5: Microsoft Access 2003 - Intro Class 5 Working with Queries.

5

Matching Criteria

This group includes women wearing suits.“Suits” is a criterion.

This group includes women wearingblue suits. “Blue” and “suits” is a multiple criteria.

Suppose our “database” is a ladies business luncheon.

Page 6: Microsoft Access 2003 - Intro Class 5 Working with Queries.

6

Queries – Select Queries Combine fields from more than one table

Up to 16 tables can be used Criteria – consists of an expression – a

combination of symbols & text that produce a result

• Set a property that establishes a validation rule of sets a default field property

• Enter a criteria expression• Create a calculated field• Set a condition for carrying out a macro• Construct functions or procedures in VBA• Edit an SQL Query

Page 7: Microsoft Access 2003 - Intro Class 5 Working with Queries.

7

Creating a Query in Design View

In the Database Window, click the option for creating a query in Design View.

A Show Table screen will appear where you select the table(s) that you want to include in the query.

Once you have added the tables you want, close the Show Table screen.

To remove a table from Query Design View, simply click once on the desired table (box), and press the Delete key on the keyboard.

To add more tables to Query Design View, click the Show Table button on the toolbar

Page 8: Microsoft Access 2003 - Intro Class 5 Working with Queries.

8

A Tour of Query Design View

Table that query is based on

Desired field(s) to include in query results

Table where the field is coming from (useful if

using multiple tables in query)

Choose whether to sort a field in query results

– when sorting by more than one field, query

sorts leftmost field first

Enter expression to determine what results

the query will give – may enter criteria in

multiple fields and use “or” row for multiple criteria in same field

Page 9: Microsoft Access 2003 - Intro Class 5 Working with Queries.

9

Working with Fields in Design View

To add a field to the design grid: Double-click the field OR Drag the desired field to the grid OR Use the drop-down list in the grid

To delete a field from the design grid: Move the mouse to the top of the desired grid

column until the mouse turns into a black down-pointing arrow, then click the mouse to select the column

Press the Delete button on the keyboard

Page 10: Microsoft Access 2003 - Intro Class 5 Working with Queries.

10

To Change Properties of a Field in a Query (e.g. to change the format of a field to currency)

In the Query Design Grid, right-click the desired field and click the Properties command from the pop-up menu.

The screen that appears should read Field Properties, not Query Properties. If not, keep trying, right-clicking right on the field name in the grid.

Once in the Field Properties window, set any properties on any tab, then close the screen.

Page 11: Microsoft Access 2003 - Intro Class 5 Working with Queries.

11

Comparison OperatorsOperator Description Expression Meaning

> Greater than >50 Value exceeds 50

>= Greater than or equal to >=50 Value is 50 or greater

< Less than <50 Value is less than 50

<= Less than or equal to <=50 Value is 50 or less

<> Not equal to <>50 Any number except for 50

Between…And Finds values between two numbers or dates

Between #2/2/95# and #2/2/98#

Dates between 2/2/95 and 2/2/98, inclusive

In Finds a value that is one of a list In(“IA”,”KS”,”NE”) Value equals IA or KS, or NE

Null Finds records that are blank Null No value has been entered

Is Not Null Finds records that are not blank Is Not Null Any value has been entered, even 0

Like Finds records that match the criteria Like “A” Value equals A

Not Finds records that do not match the criteria (similar to Not equal to <>)

Not 2 Numbers other than 2

Page 12: Microsoft Access 2003 - Intro Class 5 Working with Queries.

12

Calculated Fields in Queries Begin with expression name (what you

want to name the new field), followed by a colon :

Use square brackets [] around field names if they are to be used

Use mathematical operators such as +, -, *, /, as well as () for order of operations

Resulting field appears only in the query, not stored in the table

Page 13: Microsoft Access 2003 - Intro Class 5 Working with Queries.

13

Expression Guidelines Separate the name of the expression from

the actual expression (formula) by a colon Do not include spaces or special

characters Use brackets if spaces or special

characters are used in the expression Ie:

Name:[First Name]&Space(1)&[Last Name] Name:[First Name] & ” “ & [Last Name]

Page 14: Microsoft Access 2003 - Intro Class 5 Working with Queries.

14

Calculated Field Syntax

Cost: [Quantity]*[Price]

The name of calculated field is

followed by a colon.

Existing field names must be enclosed in

square brackets.

Operators are placed outside the square

brackets.

Page 15: Microsoft Access 2003 - Intro Class 5 Working with Queries.

15

Examples of Calculated Fields Total Price:[UnitPrice]*[Quantity]

• Creates a new field called Total Price that consists of each unit price multiplied by each quantity entry in the table.

New Price:[UnitPrice]*1.05• Creates a new field called New Price that

calculates the unit price when increased by 5%.

New Price:[UnitPrice]+10• Creates a new field called New Price that

simply adds $10 to each existing unit price in the table

Page 16: Microsoft Access 2003 - Intro Class 5 Working with Queries.

16

         This work is licensed under a Creative Commons Attribution-ShareAlike 2.5 License.