Access 2007. Queries



Similar documents
Microsoft Access 2010 Part 1: Introduction to Access

Objectives. Understand databases Create a database Create a table in Datasheet view Create a table in Design view

Microsoft Access 2010 handout

Access Queries (Office 2003)

Lab 2: MS ACCESS Tables

Tutorial 3. Maintaining and Querying a Database

Microsoft Office 2010

Introduction to Microsoft Access 2013

Click to create a query in Design View. and click the Query Design button in the Queries group to create a new table in Design View.

Microsoft Using an Existing Database Amarillo College Revision Date: July 30, 2008

Database File. Table. Field. Datatype. Value. Department of Computer and Mathematical Sciences

Crystal Reports Payroll Exercise

Microsoft Access 3: Understanding and Creating Queries

MICROSOFT ACCESS 2003 TUTORIAL

Introduction to Microsoft Access 2010

Creating and Using Databases with Microsoft Access

Creating tables in Microsoft Access 2007

IN THIS PROJECT, YOU LEARN HOW TO

Structure a Database. Key Concepts LESSON. Access 380. Lesson 2: Structure a Database. Standards

Using an Access Database

Access II 2007 Workshop

Use Find & Replace Commands under Home tab to search and replace data.

Access 2010 Intermediate Skills

MS Access Lab 2. Topic: Tables

Chapter 5. Microsoft Access

A Basic introduction to Microsoft Access

Microsoft Office 2010

Planning and Creating a Custom Database

Introduction to Microsoft Access 2003

Excel Database Management Microsoft Excel 2003

STATEMENT OF TRANSACTION REPORT ANALYSIS USING EXCEL

C omputer D riving L icence

Microsoft Access Basics

Beginning Microsoft Access

Microsoft Access 2000

Access 2007 Creating Forms Table of Contents

Microsoft Access 2010 Advanced Queries

Create a New Database in Access 2010

Getting Started with Access 2007

Ken Goldberg Database Lab Notes. There are three types of relationships: One-to-One (1:1) One-to-Many (1:N) Many-to-Many (M:N).

European Computer Driving Licence

Microsoft Access 2007 Module 1

Introduction to Microsoft Access 2007

A database is a collection of data organised in a manner that allows access, retrieval, and use of that data.

User Services. Intermediate Microsoft Access. Use the new Microsoft Access. Getting Help. Instructors OBJECTIVES. July 2009

Excel 2010: Create your first spreadsheet

CHAPTER 11: SALES REPORTING

Microsoft Access 2010: Basics & Database Fundamentals

Word 2010: Mail Merge to with Attachments

MICROSOFT OFFICE ACCESS NEW FEATURES

Sample- for evaluation only. Introductory Access. TeachUcomp, Inc. A Presentation of TeachUcomp Incorporated. Copyright TeachUcomp, Inc.

Introduction to Microsoft Office Access 2010

Access 2010: Creating Queries Table of Contents INTRODUCTION TO QUERIES... 2 QUERY JOINS... 2 INNER JOINS... 3 OUTER JOINS...

Access Tutorial 1 Creating a Database. Microsoft Office 2013 Enhanced

Reduced Quality Sample

Microsoft Access 2003 Module 1

Querying a Database Using the Select Query Window

Unit 10: Microsoft Access Queries

MICROSOFT ACCESS TABLES

Creating and Using Forms in SharePoint

Simple Invoicing Desktop Database with MS Access c 2015 by David W. Gerbing School of Business Administration Portland State University

User Services. Microsoft Access 2003 II. Use the new Microsoft

Microsoft Access Introduction

Learning Services IT Guide. Access 2013

Access Creating Databases - Fundamentals

Introduction to Microsoft Access XP

Access 2003 Introduction to Queries

4. The Third Stage In Designing A Database Is When We Analyze Our Tables More Closely And Create A Between Tables

MICROSOFT ACCESS 2007 BOOK 2

Section DB DATABASE - Microsoft Access

ACCESS Importing and Exporting Data Files. Information Technology. MS Access 2007 Users Guide. IT Training & Development (818)

Introduction to Microsoft Access

Microsoft Excel 2007 Consolidate Data & Analyze with Pivot Table Windows XP

Step One. Step Two. Step Three USING EXPORTED DATA IN MICROSOFT ACCESS (LAST REVISED: 12/10/2013)

Creating a Database in Access

BusinessObjects: General Report Writing for Version 5

Lab Manual. Databases. Microsoft Access. Peeking into Computer Science Access Lab manual

Check out our website!

COURSE DESCRIPTION. Queries in Microsoft Access. This course is designed for users with a to create queries in Microsoft Access.

Ohio University Computer Services Center August, 2002 Crystal Reports Introduction Quick Reference Guide

BUSINESS OBJECTS XI WEB INTELLIGENCE

Advanced Database Concepts Using Microsoft Access

Excel Working with Data Lists

Filter by Selection button. Displays records by degree to which they match the selected record. Click to view advanced filtering options

Microsoft Access 2007

EXCEL PIVOT TABLE David Geffen School of Medicine, UCLA Dean s Office Oct 2002

EXCEL Using Excel for Data Query & Management. Information Technology. MS Office Excel 2007 Users Guide. IT Training & Development

Microsoft Access 2010

Using Spreadsheets, Selection Sets, and COGO Controls

Topic: Relationships in ER Diagram and Relationships in MS Access

Microsoft Access 2010 Overview of Basics

INTRODUCTION TO MICROSOFT ACCESS Tables, Queries, Forms & Reports

Exploring Microsoft Office Access Chapter 2: Relational Databases and Multi-Table Queries

Access 2010: The Navigation Pane

Transcription:

Access 2007 Queries

WORKSHOP DESCRIPTION... 1 Overview 1 Prerequisites 1 Objectives 1 WHAT IS A QUERY?... 2 WHY USE QUERIES?... 2 TERMS TO KNOW... 2 Select Queries 2 Action Queries 2 Crosstab Queries 2 Parameter Queries 2 CREATE A QUERY... 3 Using a Wizard 3 Using Design View 3 Adding Fields to Query 4 Running a Query 4 EDITING QUERIES... 5 Move a Field 5 Change Column Width 5 Insert a Field 5 Delete a field 5 TABLE RELATIONSHIPS... 6 Viewing Relationships 6 Creating Relationships 6 Edit Existing Relationships 8 Delete Relationships 8 Remove a Table from the Relationships Window 8 Adding a Table to the Relationships Window 8 MODIFYING A QUERY... 9 Specify a Sort 9 Criteria 9 Rules for Setting Criteria 9 DIFFERENT TYPES OF QUERIES... 10 Grand Total Expression Query 10 Creating a Parameter Query 10 Performing a Grouped Count Query 11 Make Table Query 11

Update Query 12 Delete Query 12 Append Query 13

WORKSHOP DESCRIPTION Overview Once data has been entered into a table, it is often helpful and necessary to manipulate the data. A query is created when you want to gather information from one or more tables, perform calculations, sort data on more than one field, and update a group of records. This workshop will identify the different types of queries and explore, through the use of lecture and hands-on practice, the basics of creating, running, and modifying a query. Prerequisites Access 2007 Overview & Tables Objectives Participants in this workshop will: Review creating a table Use filter and sort Define query types Create a query based on a single table Identify different types of queries Understand and manipulate table relationships Modify a query by using sorting and criteria options Create a query based on two tables Create a lookup query Summarize data in a query Calculate a field Information Resources and Technology : Fall 2007 Access 2007 : Queries 1

WHAT IS A QUERY? A query creates a question about the data stored in an associated table(s), or a request to perform an action on the data. WHY USE QUERIES? Select only the fields you want displayed Specify only the records you are interested in displaying with criteria Sort more than one field Gather information from more than one table Perform calculations by creating new fields Serves as a source of data for a form, report or other queries Update a group of records TERMS TO KNOW Select Queries A select query asks questions about the data stored in your tables and returns a datasheet that contains the result of the query, without changing the data in the original datasheet. Action Queries An action query can change data or move data in the associated table. Append, delete, make-table, and update queries are action queries. Crosstab Queries Crosstab queries can compute summary totals based on values for each row and column. Parameter Queries Parameter queries, which are not a separate type of query, extend the flexibility of other queries by prompting you to enter certain criteria each time the query is run. Information Resources and Technology : Fall 2007 Access 2007 : Queries 2

CREATEE A QUERY Here s the scenario: you are the owner of Northwind Trading Company. You use Access for your employee, customer, product, order, supplying and shipping data. You need to send your employees a payroll update. To do this you need to query your database for employee names and addresses. To do this you will perform a Select Query. Using a Wizard 1 Create the Northwind 2007 database. From Template Categories, choose Local Templates, then choose Northwind 2007. 2 In the Navigation Pane, click the database header, and click Object Type to view the objects in the database by type. 3 Click the Create tab on the ribbon. 4 Click the Query Wizard button. 5 Choose the Simple Query Wizard and click OK. Figure 1. Navigation Pane 6 Choose the name of the table or query, on which the query will be based. Choose the Employees table. 7 Select the fields whose data you want to retrieve. Choose First Name, Last Name, Job Title, Address, City, State/Province and ZIP/PostalCode. Click Next. Note: you can select additional tables or queries to add more fields. 8 Name your query qrytblempl loyees(addresses). 9 You now have two options, you can choose either to run the query or to see and modify the query's structure in Design view. Choose Open the query to view information. Click Finish. Note: If the resulting query is not exactly what you want, you can rerun the wizard or change the query in Design view. 10 Save the query. Close the query by right-clicking on the query s tab and choosing Close. Using Design View The end of your fiscal year is coming and you will need to prepare company bonuses. You decide to pull up a quick list of Salespeople and the invoice totals of the accounts on whichh they have worked. 1 From the Create tab on the ribbon, click the Query Design button. Information Resources and Technology : Fall 2007 Access 2007 : Queries 3

2 Add the Invoice Data query to your query by selecting it from the Show Table dialog box and clicking the Add button. Close the dialog box. Note: If you have multiple tables or queries in this query, make sure they are related. If there are not any visible connections, create the join lines. Figure 2. The Show Table Dialog Box Adding Fields to Query For data to appear in a query from the table on which the query is based, fields must be added into the query. There are several different ways to add fields to the query. Select the fields from the field lists and drag them to the columns in the grid. You can select and drag one field or several fields, as described below. One field Drag and drop the field, double-click the field in the field list, or select a field directly from the list box in the Field row on the grid. A block of fields Hold down the Shift key and select the first and last fields in the block. To choose nonconsecutive fields, hold down the Ctrl key and click the fields. All fields - Double-click the title bar of the field list and drag the fields in the block. 1 Using the drop-down list box, add the following fields to the query grid: Salesperson and ExtendedPrice. 2 Save the query as qryqryinvoicedata(bonus) by right-clicking on the tab and choosing Save or clicking the Save button on the Quick Access Toolbar. Running a Query Run or activate the query once the fields have been set. 1 Click the Run button. NOTE: After a query is run, the results will appear in a dataset that looks just like a Table. Information Resources and Technology : Fall 2007 Access 2007 : Queries 4

EDITING QUERIES Move a Field 1 Return to design view. 2 Select the ExtendedPrice field by clicking the column selector above the field name. 3 Click the Column selector again while holding down the mouse button and drag the column in front of the Salesperson column. 4 Run the query. Change Column Width 1 Position the pointer on the right edge of the column you want to resize. 2 Drag the edge to resize the column. Insert a Field 1 Return to design view. 2 From the field list, select the field. 3 Drag the field from the field list to a column in the grid. Insert the Quantity field. Delete a field 1 Return to design view. 2 Select the field to be deleted by clicking above the field name. 3 Press the DELETE key. Delete the Quantity field. Information Resources and Technology : Fall 2007 Access 2007 : Queries 5

TABLE RELATIONSHIPS To relate informationn from one table to another table a relationship between two or more tables must be created. To query related records, they must be linked. Viewing Relationships 1 Select the Database Tools tab on the ribbon. 2 Click the Relationships Button. Creating Relationships Here s the scenario. You are the owner of a pet sitting business. You have a database that tracks the animals that you watch over and their owners. Step One Create the Owners Table 1 Close the Northwind 2007 database by clicking on the Microsoft Office Button and choosing Close Database. 2 Create a new blank database entitled QueryTraining. 3 Create a table named tblowners. Add the following fields, data types, and descriptions: OwnerID Text First three letters of first name and first three of last name FirstName Text Owner s first name LastName Text Owner s last name 4 Delete the ID field and make the OwnerID field your primary key. 5 Save and close the table by right-clicking on the tab and choosing Close. Step Two Create the Animal Types Table 1 Create a table using the Table Design button. 2 Add the following fields, data types, and descriptions: TypeID AnimalType Text Text Three letter abbreviation Full name of type of animal 3 Make the TypeID field the primary key. Information Resources and Technology : Fall 2007 Access 2007 : Queries 6

4 Save the table as tblanimaltypes. Step Three Create the Animals Table 1 Create a table using the Table Design button. 2 Add the following field, data type, and description: AnimalID AutoNumber Access assigned autonumber 3 Set the AnimalID field as the Primary Key. 4 Continue adding the following fields and data types to the table: AnimalName Text Name of the animal OwnerID Text Six letter ID based on the owner s first and last names Type Lookup Wizard 4a. In the first screen of the lookup wizard, choose next. 4b. Base the look up on the tblanimaltypes table and click Next. 4c. Choose the AnimalType field. 4d. Sort by AnimalType. 4e. Keep the key column hidden. 4f. Change the label name to Animal Types and click Finish. 4g. Save the table by clicking Yes. Name the table tblanimals. 4h. Add the Description Animal type. 5 Save and close the table by right-clicking on the tab and choosing Close. Step Four Create the Relationships 1 Select the Database Tools tab and click the Relationships button. The Relationships Window will appear. Notice that a link was created automatically when you used the Lookup Wizard. Now create a link manually. 2 Add the tblowners table by clicking the Show Table button, selecting the table, and clicking the Add button. 3 Close the Show Table window. 4 In the Relationships window, drag the field OwnerID from the tblowners table to the OwnerID field in the tblanimals table. 5 Enforce Referential Integrity. Now an ID cannot be added in the tblanimals table until the ID has been created in the tblowners table. Click Create. Information Resources and Technology : Fall 2007 Access 2007 : Queries 7

Step 5 Add Data 1 Add the following data to your tables: tblowners OwnerID cardar bilgre shemeg FirstName Carla Bill Sheila LastName Darma Green Megget tblanimaltypes TypeID kan rab dog cat AnimalType Kangaroo Rabbit Dog Cat tblanimals AnimalID This is automatically generated. AnimalName Bingo Ziggy Bouncer Lucky OwnerID cardar cardar bilgre shemeg TypeID Dog Cat Kangaroo Rabbit Edit Existing Relationships 1 Open the Relationships window. 2 Double-click the relationship line for the relationship you wish to edit. 3 In the Relationship Dialog Box, set any changes. 4 Choose OK. Delete Relationships 1 Click the line for the relationship you wish to delete. 2 Press the DELETE key. Remove a Table from the Relationships Window 1 Choose Tools, Relationships. 2 Select the table to remove and press the DELETE key. 3 Close the relationships window. Adding a Table to the Relationships Window 1 Click the Show Table button. 2 Add the table back that you just deleted. Information Resources and Technology : Fall 2007 Access 2007 : Queries 8

MODIFYING A QUERY Specify a Sort A query can be defined to sort records in ascending or descending alphabetic or numeric order. The sort order is determined by the field placement in Design View. The fields to the left are sorted first. 1 Open the Northwind 2007 database by clicking on the Microsoft Office Button and choosing it from the Recent Documents list. 2 Open the Invoice Data query in Design View by right-clicking on the query and choosing Design View. 3 Click in the Sort cell of the ShipName field. 4 Click the down arrow associated with the Sort cell and select ascending from the list. 5 Sort ProductName in ascending order also. 6 Select the Dataset View button on the ribbon or click the Run button. Criteria Criteria defines the records you are searching for. The updated set of records is a dataset. You can change data in a dataset and Access updates the records in the underlying tables in most cases. 1 View the Invoices Data query in Design View. 2 Click in the Criteria cell for the field you are searching on. Choose City. 3 Type the expression = Seattle Or Las Vegas in the cell and press Enter. 4 Click the Run button on the ribbon. 5 From the Microsoft Office Button Save the query as qryqryinvoicedata(seattle&lasvegas). Rules for Setting Criteria When entering a text criteria, you can use either uppercase or lowercase. You can use wildcard characters? for any character and * for any group of characters. Use the Not operator to find records not matching a value. Use Is Null to find records with a blank field; use Is Not Null to find only records with data in a specific field. Use the In operator to find a value from a list of value [e.g. typing the expression In(NY, CA, TX) would find customers in the states of New York, California, and Texas]. Information Resources and Technology : Fall 2007 Access 2007 : Queries 9

To find a Yes value, enter Yes, True, On, or 1. To find a No value, enter No, False, Off, or 0. DIFFERENT TYPES OF QUERIES Grand Total Expression Query You can use a query to perform calculations on your table data. 1 Click the Create tab on the ribbon and click the Query Design button. 2 Add the Invoice Data query to the query. 3 Add all the fields to the query. 4 In a blank query field, type the following GrandTotal: [ExtendedPrice]+[Shipping Fee] Note: You can press Shift + F2 to zoom 5 Run the query to see the results. Note: If you want to multiply the fields together use * (asterisk) If you want to subtract the fields, use - (minus) If division is part of the equations, use / (forward slash) 6 Save your query as qryqryinvoicedata(grandtotal). Close the query. Creating a Parameter Query You can set up a parameter on one or more fields in your query to narrow down your query results each time you run the query. 1 Create a new query in Design View. 2 Add the Invoice Data query to the design of the query. 3 Add all the fields to the query. 4 Create a parameter for the OrderDate field, type Between [Enter the Beginning Date] and [Enter the End Date] in the criteria cell. 5 Run the query to see the results. 6 Close the query without saving it. Information Resources and Technology : Fall 2007 Access 2007 : Queries 10

Performing a Grouped Count Query You can group your query results by the data within a field. You can also have a query count the number of entries for that field or that group. This query will group each row by the data in a column and will provide a count for each instance of the data. 1 Create a new query in design view. 2 Base the query on the Invoice Data query. 3 Add the field Ship Name to the query. Add the field again. 4 Click the Totals button on the Design tab of the ribbon. 5 A new row titled Total: will be added to your query grid. Keep the first instance of the field as Group By. 6 Change the second instance to Count by selecting it from the drop-down list in the Total: row. 7 You can now run the query. Close the query without saving it. Make Table Query You can use a make table query to create a new table based on fields in an existing table(s). 1 Create a new query in design view. 2 Add the Customers table and close the Show Table dialog box. 3 Add the First Name, Last Name, and Company fields to your query. 4 Click the Make-Table button from Design tab of the ribbon. 5 Name the new table tblcustomers(contactnames). 6 Click OK. 7 Run the query by clicking the Run button. 8 Click Yes to paste the new rows into a new table. 9 Close the query without saving it. 10 Open the table you created. 11 Close the table. Information Resources and Technology : Fall 2007 Access 2007 : Queries 11

Update Query Use the update query to update data in records. 1 Create a new query in design view. 2 Add the Customers table. 3 Click the Update button from the Design tab of the ribbon. 4 Add the field City to the query. 5 In the Update To: field type Vegas. 6 In the Criteria field type Las Vegas. 7 Run the query. 8 Open the Customers table and perform a find. Look for the city Vegas. 9 Perform a Filter by Selection on the selection Vegas. 10 Rerun the query. This time update the City Vegas to Las Vegas. 11 Close the query without saving it. Delete Query Use the delete query to delete records in a table. 1 Create a new query in design view. 2 Add the table tblcustomers(contactnames). 3 Click the Delete button on the Design tab of the ribbon. 4 Add the field Last Name to the query. 5 In the Criteria field, type Lee. In the or: field, type Li. 6 Run the query. 7 Open the tblcustomers(contactnames) table. 8 Close the query without saving it. Information Resources and Technology : Fall 2007 Access 2007 : Queries 12

Append Query You can add data from a table(s) to a different table using the Append Query. 1 Create a new query in design view. 2 Add the Customers table. 3 Click the Append button on the Design tab of the ribbon. 4 In the Append dialog box, choose tblcustomers(contactnames). 5 Add the First Name, Last Name, and Company fields to the query. 6 Run the query. 7 View the tblcustomers(contactnames) table. Notice that the records were added twice since it did not violate primary key rules. Information Resources and Technology : Fall 2007 Access 2007 : Queries 13