Microsoft Access XP (2002) Queries

Similar documents
Microsoft Access XP (2002) - Advanced Queries

Using Microsoft Access

Microsoft Access XP (2002) Reports

Objective 1: Familiarize yourself with basic database terms and definitions. Objective 2: Familiarize yourself with the Access environment.

Using Microsoft Access

Access Intermediate

EXCEL 2003 DISCLAIMER:

Microsoft Office 2010: Introductory Q&As Access Chapter 2

Access Intermediate

Microsoft Access 2010

Microsoft Access 2013

Access - Introduction to Queries

Microsoft Access 2013

Microsoft Access XP (2002) - Forms. Navigation Wizards Custom Forms Combo Boxes Calculations in Forms Pictures Multitable Input Summary Operations

Introduction to Microsoft Access 2016

Microsoft Excel 2010 Handout

Getting started with Ms Access Getting Started. Primary Key Composite Key Foreign Key

ENTERING DATA & FORMULAS...

Microsoft Access XP (2002) Switchboards & Macros

Using Microsoft Access

Access 2003 Introduction to Report Design

SOFTWARE SKILLS BUILDERS

Introduction to Microsoft Excel 2010

Introduction to Microsoft Excel 2010

Microsoft Access 2003 Quick Tutorial

Lesson 1: Creating and formatting an Answers analysis

Microsoft Access XP Queries. Student Manual

Microsoft Word 2010 Introduction to Mail Merge

Creating a Crosstab Query in Design View

Excel 2016: Part 1. Updated January 2017 Copy cost: $1.50

Complete Quick Reference Summary

Introductory Exercises in Microsoft Access XP

Table of Contents COURSE OVERVIEW... 5

TECHNOLOGY COMPETENCY ASSESSMENT MODULE Microsoft Access

Using Microsoft Access

A cell is highlighted when a thick black border appears around it. Use TAB to move to the next cell to the LEFT. Use SHIFT-TAB to move to the RIGHT.

DOING MORE WITH EXCEL: MICROSOFT OFFICE 2013

Microsoft Excel 2010 Tutorial

Access Review. 4. Save the table by clicking the Save icon in the Quick Access Toolbar or by pulling

ICDL & OOo BASE. Module Five. Databases

Microsoft Access 2016 Intro to Select Queries

In this section you will learn some simple data entry, editing, formatting techniques and some simple formulae. Contents

Enforce Referential. dialog box, click to mark the. Enforce Referential. Integrity, Cascade Update Related Fields, and. Cascade Delete Related

Microsoft Access 2010

FOCUS ON: DATABASE MANAGEMENT

Microsoft. Student Edition. The Richard Stockton College of New Jersey. Computer Courseware

CAPITAL V8. Capital Business Software Tutorial Series. Supplier Accounts Using Capital Business Manager V8 1.0

Access Objects. Tables Queries Forms Reports Relationships

ADD AND NAME WORKSHEETS

Using Microsoft Excel

Microsoft Access Description

Microsoft Access XP Edition for ECDL Syllabus 4.5 (UK only)

INTRODUCTION... 1 UNDERSTANDING CELLS... 2 CELL CONTENT... 4

REPORT WRITER MANUAL (Version 10)

Open. Select the database and click. Print. Set printing options using the dropdown menus, then click the

A Tutorial for Excel 2002 for Windows

Budget Exercise for Intermediate Excel

Database Use & Design

Rev. B 12/16/2015 Downers Grove Public Library Page 1 of 40

Exploring Microsoft Office Word 2007

Microsoft Excel Microsoft Excel

EXCEL BASICS: MICROSOFT OFFICE 2010

Visual Workflow Implementation Guide

Microsoft Office Illustrated Introductory, Building and Using Queries

Making and Editing a Table in Microsoft Word 2007

-Using Excel- *The columns are marked by letters, the rows by numbers. For example, A1 designates row A, column 1.

Microsoft Access Basics

Microsoft Access 2003 Edition for ECDL Syllabus 4.5 (UK only)

Access Intermediate

Introduction to PeopleSoft Query. The University of British Columbia

Step 1: Create a totals query to show the total cost price and total sale price of the wine for each supplier.

EXCEL BASICS: MICROSOFT OFFICE 2007

Application of Skills: Microsoft Excel 2013 Tutorial

Spreadsheet definition: Starting a New Excel Worksheet: Navigating Through an Excel Worksheet

Microsoft Access 2002 for Windows

Guide on RCRG-S Re-Designing Customized Reports from GST to SST

SUM - This says to add together cells F28 through F35. Notice that it will show your result is

ACCESS 2007 FOUNDATION

DataMaster for Windows

Veco User Guides. Grids, Views, and Grid Reports

Microsoft Access 2007 Module 2

Forms for Android Version Manual. Revision Date 12/7/2013. HanDBase is a Registered Trademark of DDH Software, Inc.

M I C R O S O F T A C C E S S : P A R T 2 G E T T I N G I N F O R M A T I O N O U T O F Y O U R D A T A

Microsoft Access Illustrated. Unit B: Building and Using Queries

Crystal Reports 2008 OFT - 700

Microsoft Excel Basics Ben Johnson

Name: Class: Date: Access Module 2

Using Microsoft Excel

Unit 12. Electronic Spreadsheets - Microsoft Excel. Desired Outcomes

Excel Primer CH141 Fall, 2017

Excel Intermediate. Click in the name column of our Range of Data. (Do not highlight the column) Click on the Data Tab in the Ribbon

SECTION 4 USING QUERIES. What will I learn in this section?

1. AUTO CORRECT. To auto correct a text in MS Word the text manipulation includes following step.

Collector and Dealer Software - CAD 3.1

MIS 2502 Access 2007 Tutorial

Making an entry into the CIS Payments workbook

Pre-Lab Excel Problem

1 THE PNP BASIC COMPUTER ESSENTIALS e-learning (MS Excel 2007)

Study Guide. PCIC 3 B2 GS3- Key Applications-Excel. Copyright 2010 Teknimedia Corporation

Excel Shortcuts Increasing YOUR Productivity

Transcription:

Microsoft Access XP (2002) Queries Column Display & Sorting Simple Queries And & Or Conditions Ranges Wild Cards Blanks Calculations Multi-table Queries

Table of Contents INTRODUCTION TO ACCESS QUERIES... 3 Files Needed... 3 What you can do with Queries:... 3 Query Characteristics... 3 SIMPLE QUERIES... 4 Example 1: Displaying Columns, Sorting, and simple searches:... 4 Placing fields on the data grid... 4 Running the Query... 5 Sorting Columns... 5 Specifying a Criteria... 6 Hiding Columns With Show... 6 Saving Queries... 6 Closing Queries... 6 Opening a Saved Query... 7 Manipulating Query Fields... 7 AND & OR CONDITIONS... 8 Or Conditions... 8 Using the OR operator... 8 AND Conditions: Separate Fields... 9 Combined AND & OR Conditions... 9 SPECIFYING RANGES... 10 Range Operators... 10 Range Examples: Date Fields... 10 Range Examples: Number or Currency Fields... 11 WILDCARDS... 11 Using Ranges With Wildcards... 11 SEARCHING FOR BLANK FIELDS... 12 Editing a Table Using Through a Query... 12 USING NOT EQUAL TO.... 13 CALCULATIONS IN QUERIES... 14 Calculation Characteristics... 14 Calculation Rules... 14 Calculations With Constants: Single Field & Table... 14 Formatting Fields... 15 Calculations Between Fields: Single Table... 15 CALCULATINS WITH DATE FIELDS... 16 Single Table, Multiple Fields... 16 Equations With A Criteria... 16 Dates Using Constants... 16 LINKING TABLES... 17 Linking When Data Doesn t Match... 18 Linking Two Tables... 19 Removing Tables from the Query Grid... 21 Creating the Link Line... 21 Calculations Between Tables with a Criteria... 21 Access_2002-Queries.doc April 22, 2003 Page 2of 25

INTRODUCTION TO ACCESS QUERIES Queries are questions that you ask of your tables. For example, you wish to create a list containing the name & address of only those customers who live in Washington and who s credit limit is over $30,000. A query can provide this information for you. Files Needed To be able to follow along with this handout, you will need a file called: SweetsXP.mdb. It is on the web at: http://www.marshall.usc.edu/web/computing.cfm?doc_id=770#access What you can do with Queries: Select which columns to display. Sort columns. Display only those records meeting a particular criteria. Perform calculations on a field or between fields (same table or linked tables.) Perform summary calculations on grouped data. Link tables together through a common field. Perform global edits on a database. For example, raise the credit limit of Californians by 10%. Delete all records that meet the specified criteria. For example, delete the records of all customers who have not made a purchase in five years. Create a table from a query. Append records from one table to another. Find duplicate records. Perform a cross tabulation. Create a dynamic query that asks the user questions. Find out if information is in one table, but not in another. For example, you have two tables: a table that lists all of the products that you carry, and one that lists all of the products you have sold. You wish to know which products you have not sold. Query Characteristics A query must be based upon a table or a saved query. Therefore, the first step in creating a query is selecting the table or saved query. When you save a query, you are saving the question, not the answer. This means that if the data in the table changes, the next time your run your saved query, you may get a different answer. This also means that you are not wasting space on your hard drive by duplicating data. Depending upon the type of query created, if you edit the data displayed in a query, the data in the table also changes. Access_2002-Queries.doc April 22, 2003 Page 3of 25

SIMPLE QUERIES Example 1: Displaying Columns, Sorting, and Simple Searches: In this example we will create the following tasks: Specify which fields (columns) to list. Sort data. Specify criteria. Run and save the query. 1. Open the file called SweetsXP.mdb (See Files Needed above.) 2. Click on the Tables tab in the Database window. 3. Click on the table called: General Candy Info. 4. From the menu, click on: INSERT - QUERY 5. In the New Query window, click on: Design View and then Ok. Field lists appear in this area. To move the field list, click & drag it by its title. Use the up/down arrows to see fields not currently visible. Double click a field name to place it on the data grid. Use the scroll track to move the data pane. Field: Field names and calculations go here. Table: The name of the table appears here. Sort: Sorts from left to right, ascending or descending. Show: Hides/displays a column in the query. Criteria: Type what you re searching for here. Or: Creates an or condition. Rows below are also OR. Placing fields on the data grid The first step in constructing your query is deciding which fields (columns) you wish to display. You make a column display in your query by placing its field name in the Field row of your query grid. There are several methods of getting a field onto the grid: Double clicking a field name places it on the query grid. Clicking & dragging a field to the grid will place it on the grid. Clicking in the Field row of the grid will make a down arrow appear. If you click the down arrow, you can pick a field from the list that appears. Double clicking the name of the table in the tile area of the field list will highlight all fields. If you then click and drag any one of them to the query grid, all will go. Access_2002-Queries.doc April 22, 2003 Page 4of 25

1. In the field name box, double click the following fields: Manufacturer, Brand Name, and Cost Per Unit to place them on your query grid. Running the Query 2. To view the result, click on the Run button on the toolbar: The results of your query will be listed in table format. Note that the data is not sorted, only those fields appearing on the query grid are present, and none of the data is filtered. 3. Return to Design View, by clicking on the icon in the upper left corner of your screen: Sorting Columns You can sort a field (column) by clicking in the sort row of the column to be sorted and selecting either: Ascending: Text (A-Z), Numbers (- to + ), dates (old to recent). Descending: Text (Z-A), Numbers (+ to - ), dates (recent to old). Columns sort from left to right. This means that the leftmost column will be the primary sort and any other columns to its right will be sub sorts (tie breakers).we will sort primarily by Manufacturer and then perform a secondary sort by Brand Name. 1. Click in the Sort row in the Manufacturer column. 2. Click the down arrow that appears. 3. Select: Ascending 4. Click in the Sort row in the Brand Name column. 5. Click the down arrow that appears. 6. Select: Ascending 7. To test what you have done, run the query. (The data should be alphabetized first by Manufacturer they by Brand Name.) 8. Click on the Design View button on the toolbar to return to the query grid: Access_2002-Queries.doc April 22, 2003 Page 5of 25

Specifying a Criteria Access uses Query by Example to set criteria. This means that you will need to specify what it is you are searching for in order for Access to find it. Access is not case sensitive. We will list only those products produced by Nabisco. 1. Click in the Criteria row in the Manufacturer column. 2. Type: Nabisco and press ENTER (note that Access places quotations around your text.) 3. Run the query. Only those products made by Nabisco are listed. 4. Click on the Design View button on the toolbar to return to the query grid: Hiding Columns With Show This option is useful when you wish to sort or search by a column, but do not wish to display it. For example, we wish to list only those products sold by Nabisco, but we do not wish to display the manufacturer. 1. In the Manufacturer column, click on the Show box to remove the check. 2. Run the query. (The manufacturer column is missing) Saving Queries If the question you asked is one you may need to ask again, you should save your query. Note that you are saving just how to ask the question, not the answer. Therefore, the next time you run the query, if the data in the table has changed, you may get a different answer. 1. From either Design View or Datasheet View, click on the Save button: 2. In the Query Name box, type a short descriptive name such as: Nabisco Products. 3. Click on Ok. Closing Queries You can close a query from either Design View or Datasheet View. 1. From the menu, click on: FILE - CLOSE If you haven t saved your query Access will prompt you to save. If you have already saved it, Access will close the query. Access_2002-Queries.doc April 22, 2003 Page 6of 25

Opening a Saved Query You can run and edit saved queries. Saved queries are stored under the Queries tab in the Database window. 1. In the Database window, click on the Query tab. 2. Click on the query you wish to run or redesign. 3. Click on Open to run it or Design to redesign it. Manipulating Query Fields To remove all fields from your query grid: 1. In Design View, from the menu select: EDIT - CLEAR GRID To remove a single field from the query grid: 1. Click on the gray bar just above the name of the field on the grid. 2. Press DELETE on your keyboard. To move columns on the query grid: 1. Click on the gray bar just above the name of the field on the grid then let up on the button. 2. Click & drag the gray bar to the left or right. To Adjust Column Widths: 1. Place the mouse between two columns in the gray bar area. 2. Click & drag the mouse either left or right. Access_2002-Queries.doc April 22, 2003 Page 7of 25

AND & OR CONDITIONS So far we have only performed a simple single criteria query; however, Access can also perform AND & OR queries which contain compound criteria. Or Conditions OR Conditions are needed when you are looking for any instances from a group of possibilities. At least one instance of the group needs to match for the record to be returned. For example, you would like to list products sold by Nabisco, General Foods, or Hershey. 1. In the Database window, click on the Tables tab. 2. Select the table: General Candy Info. 3. From the menu, click on: INSERT - QUERY. 4. Select Design View and click on Ok. 5. Double click the fields: Manufacturer, Brand Name, Cost Per Unit, and Introduction Date. 6. In the Manufacturer column, type the Manufacture names as shown below. All of the rows below Criteria are for Or conditions. 7. Run the query. Using the OR operator You can also use the OR operator to get the same results. A query using it would be constructed like the one shown. Access_2002-Queries.doc April 22, 2003 Page 8of 25

AND Conditions: Separate Fields If criteria is specified in the same row, but different fields, the result is an AND condition. With an And condition, all criteria must be met for the record to be returned. Exercise: We will list only those products manufactured by Nabisco that cost over $40 dollars per case. 1. From the query above, delete the criteria in the manufacture column. 2. Create the query grid shown. 3. Run the query to see the result. You should get the following datasheet: Combined AND & OR Conditions Separate rows do not see each other. When you place criteria on separate rows, you are asking completely separate questions, the answers just happens to appear on the same page. Example: In the picture to the right, the query is making the following request: Display all General Food products but only those Nabisco products whose Cost Per Unit is over $40. If you wanted to see only Nabisco where the cost per unit was greater than $40 or General Food products whose cost per unit was greater than $40, you would have to create the query in one of the following two ways: Access_2002-Queries.doc April 22, 2003 Page 9of 25

SPECIFYING RANGES Use range operators when looking for ranges of numbers or ranges of dates. For example, you want only those records where the Cost Per Unit is between 30 and 90. Or you want only those records where the date of introduction is between 1/1/50 and 1/1/90. Range Operators Allowable range operators are: Operator Description > Greater than. < Less than. >= Greater than or equal to. <= Less than or equal to. <> Not equal to. Range Examples: Date Fields Try some of the examples below in a date field. Note that dates must be enclosed by # signs. When looking for data in a range: Use AND to look for data between two points. Use OR to look for data outside of two points. (Highs and lows but not the middle.) Example Description #1/1/95# List records equal to 1/1/95 <>#1/1/95# List all records not equal to 1/1/95 >#1/1/95# List records greater than 1/1/95 <#1/1/95# Lists records greater than 1/1/95 >=#1/1/95# Lists records greater than or equal to 1/1/95 <=#1/1/95# Lists records less than or equal to 1/1/95 >=#1/1/95 and <=#1/1/98# Lists records between 1/1/95 and 1/1/98 inclusively. >#1/1/95 and <#1/1/98# Lists records between 1/1/95 and 1/1/98 exclusively. Between #1/1/95 and #1/198# Lists records between 1/1/95 and 1/1/98 inclusively. <#1/1/95# or >#1/1/2000# Lists records earlier than 1/1/95 or later than 1/1/2000 but nothing in between. <#1/1/95# and >#1/1/2000# Would list nothing. There is no date that is both less than 1/1/95 AND greater than 1/1/2000. >#1/1/95# or <#1/1/2000# Would list all dates. >Date() Lists dates greater than today s date. >Date()+30 Lists dates greater than 30 days from today s date. >=Date() and <#1/1/2000# Lists dates between today s date and the year 2000. Access_2002-Queries.doc April 22, 2003 Page 10of 25

Range Examples: Number or Currency Fields Example Description >5 Greater than five. >=5 Greater than or equal to five. <5 Less than five. <=5 Less than or equal to five. 5 Equal to five. <>5 Not equal to five. >5 and <10 Greater than 5 and less than 10. >=5 and <=10 Greater or equal to 5 and less than or equal to 10. Between 5 and 10 Greater or equal to 5 and less than or equal to 10. >5 and <2 There is no number greater than 5 and less than 2. >5 or <10 You would get all numbers. WILDCARDS Wildcards are useful in text fields where you wish to list records containing a particular string of characters. There are two wild cards in Access: * represents any number of characters.? represents any single character. Example Description H* Lists all those records beginning with the letter H. *Foods Lists all records ending in the word Foods. *chocolate* Lists all records containing the word Chocolate Sm?th Would list Smith or Smyth, but not Smooth Try the following examples using the table General Candy Info. Using Ranges With Wildcards You can also search for ranges of text using wild cards. You must enclose the criteria in square brackets [ ] and type the word Like. Example Description Like [NFG]* Would list only those records beginning with an N, F, or G. Like [A-G]* Would list only those records beginning with the first 7 letters of the alphabet. Access_2002-Queries.doc April 22, 2003 Page 11of 25

SEARCHING FOR BLANK FIELDS It is often necessary to locate fields which were left blank. For example, you would like to list everyone who does not have an entry for their social security number or anyone who does not have a fax machine. The criteria used to search for blanks is: Is Null Example: Using the Sweets Customers table, you would like to list all those customers who you do not have a fax number for. 1. In the Database window, click on the table: Sweets Customers. 2. From the menu, click on: INSERT - QUERY 3. Select Design View and click on Ok. 4. Set up the query as shown. 5. Run the query You should get the information shown below Editing a Table Using Through a Query In Access, when you edit the information a query gives you, the data in the table is also modified. Example: In the proceeding example, we listed all records that contain blanks for fax numbers. We now have Anthony s Beer and Ale s fax number and will enter it into the query datasheet. 1. You are viewing the results of the query in the previous example. 2. Type in a fax number for Anthony s Beer and Ale: (509) 555-8646. 3. Press SHIFT + ENTER on your keyboard to save the changes to the record. 4. Note the total number of records at the bottom of the screen. 5. Click on the Design View icon : 6. Run the query. Note that Anthony s Beer and Ale is not listed and you have one less record number at the bottom of the screen. This is because the table Sweets Customers now has a fax number for Anthony s Beer and Ale. Access_2002-Queries.doc April 22, 2003 Page 12of 25

USING NOT EQUAL TO. If you would like to see everything but a particular item, use the Not command. For example, you would like to see all customers except those living in the USA. The command for not equal to is: Not Example: This will list all customers who do have fax numbers. (Those without blanks.) 1. In the Database window, click on the table: Sweets Customers. 2. From the menu, click on: INSERT - QUERY 3. Select Design View and click on Ok. 4. Set up the query as shown. 5. Run the query. The command: Not Is Null means do not list blanks. Example: This will list all customers that do not live in the United States. 1. Set up the query grid as shown. 2. Run the query. The command: Not USA will list all customers except those living in the United States. Access_2002-Queries.doc April 22, 2003 Page 13of 25

CALCULATIONS IN QUERIES Calculation Characteristics Calculations in queries will create a new column in the query; however, the answer is display only meaning that a new field will not be added to the table. Calculations can be performed on a single field or between different fields. Calculations can be performed between fields from different tables if the tables have been linked. Calculations can be used with Number, Currency, and Date/Time field types. You can use the criteria row when doing calculations. Calculation Rules Field names must be in square brackets [ ] and must be spelled correctly. Available operators are: * multiplication / division + addition - subtraction The calculation should be proceeded by a new name for the field and a colon. For example: Tax: The calculation must be placed in the Field row of the query grid. Calculations With Constants - Single Field In this example, we will multiple the Cost Per Unit field in the General Candy Info table by 1.0775 to get the Cost Per Unit with tax. 1. In the Database window, click on the Tables tab. 2. Select the table: General Candy Info 3. From the menu, click on: INSERT - QUERY. 4. Select Design View and click on Ok. 5. Set up the query grid as shown: The equation is: Cost With Tax: [Cost Per Unit]*1.0825 6. Run the Query. Access_2002-Queries.doc April 22, 2003 Page 14of 25

Formatting Fields The new field (Cost With Tax) is created when you run the query. Note that there are more than two places to the right of the decimal point. We will format this to so that the new field has a $ and only two decimal places to the right of the decimal point. 1. Return to Design View 2. Click in the field containing the equation. 3. Click on the Properties icon : 4. Click on the General tab. 5. Click inside of the Format box. 6. Click the down arrow and select Currency 7. Close the Field Properties window. 8. Run the query. Calculations Between Fields In this example, Cost Per Unit is divided by Items Per Unit to find out how much it costs for a single candy bar. 1. In the Database window, click on the Tables tab. 2. Select the table: General Candy Info 3. From the menu, click on: INSERT - QUERY. 4. Select Design View and click on Ok. 5. Set up the query grid as shown: The equation is: Cost Per Item: [Cost Per Unit]/[Items Per Unit] 6. Run the query. Access_2002-Queries.doc April 22, 2003 Page 15of 25

CALCULATINS WITH DATE FIELDS Multiple Fields In the table Orders, we would like to know how many days are between the order date and ship date of orders. 1. In the Database window, click on the Tables tab. 2. Select the table: Orders 3. From the menu, click on: INSERT - QUERY. 4. Select Design View and click on Ok. 5. Set up the query grid as shown: 6. Run the Query. The equation is: Days: [Shipped Date]-[Order Date] Equations With A Criteria We would like a list of late orders. (Where the Shipped Date is after the Date Required.) If we subtract the Order Date from the Shipped Date, any orders with negative numbers were late orders. We can then tell Access to only list those orders where the Days is less than zero. 1. Set up the query grid as show (be sure to include the criteria). 2. Run the Query. The calculation is: Late Orders: [Required Date]-[Shipped Date] Dates Using Constants Payment for an order is due 30 days after the Order Date, try the calculation below: Due Date: [Order Date]+30 Access_2002-Queries.doc April 22, 2003 Page 16of 25

LINKING TABLES Access allows you to query separate tables at the same time. This allows you to place fields from different tables on the same grid. To link tables, they must meet the following conditions: Tables can only be linked if they have a common field. The picture below shows how the five tables in the SweetsXP database can be linked in a query. Note which fields the lines are connecting to. The names of the linking fields should be named the same. If they are not, you can still link but you must draw the linking line yourself. (Unless you have set up a relationship between the fields in the Relationships window.) One or both of the linking fields should be keyed. If neither are, you can still link but you must draw the linking line yourself. Note that field names appear bold on the query grid if they are keyed. Some of the data in the linking fields must contain the same data. For example, if the Candy ID field in one table contains ID numbers that are completely different than the Candy ID numbers in another table, when you run your query, you will get a blank screen. By default, Access only shows you records that have a match on the linking fields in both tables. (See the illustration on the next page.) Linked fields must be of the same data type: i.e. text to text, number to number, etc. However, you can link an autocounter field to a number field (as long as the format of the number field is set to Long Integer.) You cannot link two tables together if the linking fields are Memo field types. Access_2002-Queries.doc April 22, 2003 Page 17of 25

In summary, linked fields should have the same name, be of the same data type, contain some of the same data, and at least one of them should be keyed. Linking When Data Doesn t Match This example shows what happens in a linked query when both tables do not contain the same data in the linked fields. Note that linked fields do not always have to contain exactly the same data; the purpose of this example is to increase your understanding of field links. Below are two tables. On the left is a table that lists every product we carry. On the right is a table of only those products sold. (Some are listed more than once because they have been sold more than once in different orders.) Note that in the table on the right, we have never sold Candy ID number 2 (Milky Way). Lists every product we carry. Lists only those products sold. When the tables are linked through their common field of Candy ID, the query produces the data below. Note that Candy ID number 2 is not listed (Milky Way). Milky Way is not listed because although it does exist in one of the tables, its Candy ID does not exist in both tables. By default, Access will only list records when the data contained in the linking fields exists in both tables. Note that this is not a bad thing, if you have not sold Candy 2, you probably would not want to list it anyway. Access_2002-Queries.doc April 22, 2003 Page 18of 25

Linking Two Tables In this example, we will link Orders to Order Details to produce a query that tells us which items were ordered by which customer. The tables can be linked through their common field of Order ID. 1. In the database window, click on the Tables tab. 2. Select the table: Orders 3. From the menu, click on: INSERT - QUERY. 4. Select Design View and click on Ok. Adding More Tables To the Query We will now add the Order Details table to the query grid. 1. Click on the Add Table icon on the toolbar: 2. Click on the Tables tab. (If necessary). 3. Select Order Detail then click on Add and then Close. Placing Linked Fields On The Query Grid Linked tables can be used in queries in almost all of the same ways that single tables can be used. For the most part, treat fields from separate tables just as you would fields coming from a single table. 1. Create the query grid as shown. Note that the Tables row shows which table each field came from. (If your Tables row is not present, go to: VIEW - TABLE NAMES (on the menu.) 2. Run the query : The query shows that in Order ID 1, candy ID s 1, 2, & 13 were purchased. This information is accurate but it would be more useful if the Brand Name and Company Name were listed rather than the Candy ID and Customer ID. These fields are in the tables General Candy Info and Sweets Customers. 3. Return to Design View : Access_2002-Queries.doc April 22, 2003 Page 19of 25

Adding More Tables to the Query We will add the tables: Sweet s Customers and General Candy Info to the query grid. 1. Click on the Add Table icon on the toolbar: 2. Select the Tables tab, (if necessary). 3. Click on the Sweets Customers table and click Add. 4. Click on the General Candy Info table and click Add. 5. Click on Close. Note that you can move the tables by clicking & dragging them by the table names. 6. Remove all fields from the query grid. From the menu, click: EDIT - CLEAR GRID. 7. Place the fields shown on the grid. 8. Run the query : Note that the Company Names and Brand Names are listed rather than the Customer ID and Candy ID. Note also that the linking field (Order ID) does not have to be placed on the grid for the query to work. Access_2002-Queries.doc April 22, 2003 Page 20of 25

Removing Tables from the Query Grid When a table is removed from the query grid, all of the fields which came from it will also be removed from the query grid. To remove a table: Click on the Table and press DELETE on the keyboard. Creating the Link Line If the linking fields are not named the same or one of them is not keyed, you will have to draw the link line yourself. To add a line: Click & drag from one of the linking fields to the other and the line will be drawn. To remove, click on the link line and press DELETE on the keyboard. Calculations Between Tables with a Criteria Calculations between fields from different tables are performed in the same manner as calculations with a single table. Criteria can also be specified. Example: We want to know how much money we have grossed from each sale in May of 1989. Our cost, quantity, and order date for an item are in the tables General Candy Info, Order Details, Orders, respectively. 1. In the database window, click on the Tables tab. 2. Select the table: General Candy Info 3. From the menu, click on: INSERT - QUERY. 4. Select Design View and click on Ok. 5. Click on the Add Table icon on the toolbar: 6. Select Order Detail then click on Add. 7. Select Orders and then click on Add then Close. 8. Set up the query grid as shown. 9. Run the query : Only records from May of 89 should be listed. Note that we did not have to list Quantity or Cost Per Unit. Mars Bars is listed more than once because it has been sold more than once. We multiply by 2 because our mark up is 100%. The calculation is: Gross: [Cost Per Unit]*[Quantity]*2 The criteria is: Between #5/1/98# and #5/31/89# Access_2002-Queries.doc April 22, 2003 Page 21of 25

FIND DUPLICATE RECORDS Though Primary Keys and Indexes set to Not Duplicates help to prevent the creating of duplicate records, you may still find yourself with a table containing duplicate records. Access provides a query wizard that will help you locate duplicate records. Find Duplicate Records in Sweets Customers 1. Close any open tables. 2. From the menu, click: INSERT - QUERY 3. Select Find Duplicates Query Wizard then click Ok. 4. Select the Sweets Customers table then click Next. 5. Double click the Company Name field to select it as a Duplicate-value field. 6. Click on Next. 7. Double click Customer ID, Contact Name, and Address as additional fields to list. 8. Click on Next. 9. Name the query and then click Finish. If you wish to delete one the duplicate records, simply click the record you wish to remove and then press DELETE on your keyboard. Access_2002-Queries.doc April 22, 2003 Page 22of 25

SEARCH A TABLE WITHOUT CREATING A QUERY It is not always necessary to create a query when searching for records. When all you wish to do is search for something in a single table without using calculations or performing summary operations, Access provides three alternative methods for searching a table: Find, Filter by Selection, and Filter by Form. FIND You may be familiar with this from MS Word or Excel. Find can be used to search a column for a specific criterion; further, there is a Replace feature that can be used to replace any matches with new data. Note that wild cards and the Is Null command are allowed but ranges, And, Or, and Not are not allowed. Find customers who live in the 604 area code. 1. Click on the Tables tab. 2. Double click the Sweets Customers table to open it. 3. In the Phone column. 4. Click the Find button : 5. In the Find What box, type (604)* 6. Keep clicking Find Next to go to each subsequent match. Replace: Allows you to replace a field match with new data. Find What: Type the criteria to search by here. Look In: Specify to search only the selected column (recommended) or the entire table. Match: Used like wildcards, this allows you to search the: Whole Field, Start of Field, or Any Part of Field. Search: Controls the vertical direction of the search (All, Up, or Down). Match Case: When checked, the search is case sensitive. Search Fields as Formatted: Searches the field as formatted. Access_2002-Queries.doc April 22, 2003 Page 23of 25

Filter By Selection This is the simplest method of searching a table. To use Filter by Selection, click on an example of what you are searching for and Access will displays all other records which match your example. We will find all customers that live in London. 1. Click on the Tables tab. 2. Double click the Sweets Customers table to open it. 3. In the City column, click on the word London 4. Click the Filter by Selection button. Access should only be displaying customers from London. Note the word Filtered in parenthesis at the bottom of your screen. Remove the Filter Click the Remove Filter button the following filter methods): to display all records once again (this also applies to any of Note that the Remove Filter button becomes the Apply Filter button once a filter has been removed and will reapply the most recent filter used. Filter - Excluding Selection This function is similar to Filter by Selection except that Access displays everything except what you select. Display all customers except those from the USA. 1. Click on the Tables tab. 2. Double click the Sweets Customers table to open it. 3. In the Country column, click on the word: USA 4. From the menu, click: RECORDS - FILTER - FILTER EXCLUDING SELECTION Access displays only those customers who are not from the USA. Remove the Filter Click the Remove Filter button to display all records. Access_2002-Queries.doc April 22, 2003 Page 24of 25

Filter By Form This method takes you into a special screen where you can select your criteria using drop down arrows. Wild cards, ranges, Or and And conditions are also possible. Further, Filter by Form can be saved as a query. Example: Find USA customers who are Owners or Canadian customers who are Marketing Managers. 1. Click on the Tables tab. 2. Double click the Sweets Customers table to open it. 3. Click the Filter by Form button. 4. Click on the Look For tab at that bottom of the screen. 5. Set Contact Title to: Owner 6. Set Country to: USA 7. Click on the OR tab at the bottom of the screen. 8. Set Contact Title to: Marketing Manager 9. Set Country to: Canada 10. Click the Apply Filter button. Assess should only customers who match our criteria. Remove the Filter Click the Remove Filter button to display all records. Access_2002-Queries.doc April 22, 2003 Page 25of 25