Microsoft Access 2010 Queries II Action Queries & Lookups

Similar documents
Microsoft Access 2016 Intro to Select Queries

Microsoft Access 2016 Intro to Forms and Reports

Microsoft Access 2010 Select Queries

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

Creating a Crosstab Query in Design View

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

Microsoft Excel 2010 Linking Worksheets & Workbooks

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

Microsoft Access 2010

Microsoft Access 2013

Microsoft Access 2013

Excel 2016 Charts and Graphs

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

Excel 2016: Basics 2 Math and Functions

Complete Quick Reference Summary

Microsoft Office Illustrated Introductory, Building and Using Queries

Advanced Queries: Moving Beyond the Select Query Instructor: Edwin V. Bonilla

Microsoft Office Access 2013: Part 01. Lesson 04 - Creating Advanced Queries

Microsoft Access 2007 Tutorial. Creating a Database using Access 2007

ICDL & OOo BASE. Module Five. Databases

Name: Class: Date: Access Module 2

Introduction to Microsoft Access 2016

Access Intermediate

Microsoft Access XP (2002) Queries

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

Microsoft Office 2010: Introductory Q&As Access Chapter 2

TECHNOLOGY COMPETENCY ASSESSMENT MODULE Microsoft Access

Microsoft Access 2003 Quick Tutorial

More Skills 12 Create Indexes and Establish a One-to-One Relationship. To complete this database, you will need the following file:

Microsoft Access XP (2002) - Advanced Queries

Englische Access 2016-Trainingsskripte für IT Seminare und EDV Schulungen als Word-Dokument zum Ausdrucken und fürs Intranet

Phone: Fax: Directions for setting up MARCO Insert Item #A-6LI 3 H x 4 W

Microsoft Access Description

Shelly Cashman Series Microsoft Office 365 and Access 2016 Introductory 1st Edition Pratt TEST BANK

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

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

Access Groups. Collect and Store. Text Currency Date/Time. Tables Fields Data Type. You Your Friend Your Parent. Unique information

Introduction to Database Concepts and Microsoft Access Database Concepts and Access Things to Do. Introduction Database Microsoft Access

MODULE 5 DATABASES. Content

Access Intermediate

Access Module 2: Building and Using Queries

When you pass Exam : Access 2010, you complete the requirements for the Microsoft Office Specialist (MOS) - Access 2010 certification.

SOFTWARE SKILLS BUILDERS

Table of Contents COURSE OVERVIEW... 5

More Skills 14 Use a Query to Find Unmatched Data. To complete this database, you will need the following file:

Introduction. Table Basics. Access 2010 Working with Tables. Video: Working with Tables in Access To Open an Existing Table: Page 1

Dealing with the way Mail Merge changed in MS Word 2003

Adobe Acrobat 8 Professional Forms

Using Microsoft Access

Information from Your

Microsoft Access 2010

MS-Access : Objective Questions (MCQs) Set 1

Access - Introduction to Queries

Microsoft Office Specialist Access 2016

MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

Working with promotions

Microsoft Access XP Queries. Student Manual

Create Forms 5. KEY TERMS Blank Form tool common filters filter filter by form Form Design button Form tool Form Wizard Themes LESSON SKILL MATRIX

Create and Modify Queries 7

Using Microsoft Access

MS Access Let s begin by looking at the toolbar and menu of Access.

Microsoft Office 2010: Introductory Q&As Access Chapter 3

Access Groups. Collect and Store. Text Currency Date/Time. Tables Fields Data Type. You Your Friend Your Parent. Unique information

Creating Reports in Access 2007 Table of Contents GUIDE TO DESIGNING REPORTS... 3 DECIDE HOW TO LAY OUT YOUR REPORT... 3 MAKE A SKETCH OF YOUR

Microsoft Access XP (2002) Reports

Microsoft Access XP (2002) Switchboards & Macros

Introductory Exercises in Microsoft Access XP

Intermediate Microsoft Access 2010

Introduction to Microsoft Office Access 2010

Access 2007: Advanced Instructor s Edition

Microsoft Access 2007 Level 3

Creating a Database Using Access 2003 for Windows 2000/Me/2003

Skills Exam Objective Objective Number. Creating crosstab queries Create a crosstab query

Microsoft Access 2003

Microsoft Access 2010

Institutional Reporting and Analysis (IRA) For help, blitz "Financial Reports", or

LIMS QUICK START GUIDE. A Multi Step Guide to Assist in the Construction of a LIMS Database. Rev 1.22

Copyright 2009 Labyrinth Learning Not for Sale or Classroom Use LESSON 1. Designing a Relational Database

Microsoft Access Illustrated. Unit B: Building and Using Queries

MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

Tutorial 1. Creating a Database

Copyright 2012 Pulse Systems, Inc. Page 1 of 46

Identifying Updated Metadata and Images from a Content Provider

Access Groups. Collect and Store. Text Currency Date/Time. Tables Fields Data Type. You Your Friend Your Parent. Unique information

4H4Me Announcement Letter

Introduction to Mail Merge. Use IT+

VUEWorks Report Generation Training Packet

GO! with Microsoft Access 2016 Comprehensive

Microsoft Certified Application Specialist Exam Objectives Map

Section 1 Creating Mail Merge Files

Links to Activities ACTIVITY 4.1. Links to Activities Links to Activities

Going Paperless: Operational & Billing Efficiencies

Access. Basics PRESENTED BY THE TECHNOLOGY TRAINERS OF THE MONROE COUNTY LIBRARY SYSTEM

Microsoft Access 2013

MIS Cases: Decision Making With Application Software, Second Edition. Database Glossary

Alan Davies and Sarah Perry

Let s create another simple report from one of our queries available: Author Age query.

Service Minder Plus Features/Helpful Hints

Tutorial 4 Creating Forms and Reports

Mail Merge. To Use Mail Merge: Selecting Step by Step Mail Merge Wizard. Step 1:

Transcription:

Microsoft Access 2010 Queries II Action Queries & Lookups Email: training@health.ufl.edu Web Page: http://training.health.ufl.edu

Microsoft Access: Action Queries & Lookups 1.5 hours This workshop requires completion of "Access: Basics", "Select Queries", and "Intro to Forms and Reports". Topics include counting occurrences; building queries from queries; working with One to One relationships; relationship join properties; action queries: Make Table, Append, Delete, and Update; and linking query values to forms and reports. Query 1: Number of Employees... 1 Counting Occurrences... 1 Providing More Information... 1 Query 2: First AN and Last LL... 2 Query with Queries... 2 Setting Default Query Relationship... 3 Query 3: Patient Home Phone Numbers... 4 One to One Relationship... 4 Setting Default Join Type... 5 Action Queries... 6 Running Action Queries... 6 Action Query Exercises... 6 Action Query 1: Make Table Query... 7 Action Query 2: Delete Query... 8 Action Query 3: Update Query... 9 Action Query 4: Append Query... 10 Modify Table... 10 Append Query... 11 Navigation Pane... 12 Creating an Active Lookup... 13 Active Lookup Steps... 13 Step 1 Create a unique list of values... 13 Step 2 Create a Form with a Combo box for "City List"... 14 Step 3 Create a query from table, with criteria from form... 14 Step 4 Create the report... 15 Step 5 Create button to Open Report... 15 Step 6 Test by adding new data... 16 Pandora Rose Cowart Education/Training Specialist UF Health IT Training C3 013 Communicore (352) 273 5051 PO Box 100152 prcowart@ufl.edu Gainesville, FL 32610 0152 http://training.health.ufl.edu Updated: 5/22/2016

Query 1: Number of Employees Counting Occurrences Access can be used to very quickly find how many times a value occurs on a list. For example, if we need to know how many employees work for each department we can do this using the Totals feature in a query design. The "trick" is to bring the same field down twice, grouping by one and counting the other. 1. From the Create Tab choose Query Design 2. Add the Employee Table and close the Show Table box 3. Bring Down Dept ID twice 4. From the Design Tab turn on Totals 5. Change Total under second Dept ID from Group by to Count Providing More Information If we decide to see the Department name as well as the number we can pull from the Departments table as well. We still need the Employee table to find the proper count. 1. Add Table Departments 2. Drag Department field between the two Dept IDs in the bottom half of the query 3. Change the Second Dept ID to read # of Emp: Dept ID 4. Close and save as "Number of Employees" Page 1

Query 2: First AN and Last LL Consider this very simple example: First an: Find everything from Patients where the First name contains AN (seven records) Last ll: Find all records from Patients where the Last name contains LL (six records) Query with Queries We would like to find the records that are in both. 1. From the Create Tab choose Query Design 2. Add queries First an and Last ll a. Close the show table box 3. Bring down Last and First fields from both queries Because there is no relationship between the two queries, Access provides us with every possible combination. On a rare occasion you may want data repeated with every record, for example trying to calculate a percentage of a total where you would want the total to appear on each line. To avoid the repetition we need to create a relationship between the two or more data sources. Page 2

4. In the top half of the query design drag Med Rec from one query to another a. This will create a relationship line 5. Close and Save as " First an Last ll" Queries don't have primary keys, so at this point in your database development you need to know your data; you need to know how these two datasets should connect. Since our Patient's Med Rec is the key in the table, it's our best match between these two queries. Setting Default Query Relationship If you plan to use these queries as the source for other database objects (queries, forms, and reports) you should create a permanent relationship from the Relationship window. 1. From the Database Tools tab choose Relationships 2. Drag the queries First an and Last ll from the navigation pane into the relationship window 3. Drag Med Rec field from one query to the next a. Click Create in the Edit Relationship Window This sets a 'default' relationship between the two queries. Anytime you pull the two of them into a database object, Access will know to join them along the Med Rec field. Page 3

Query 3: Patient Home Phone Numbers One to One Relationship One to one relationships are less common than one to many, but they do occur. Our Patients table contains names and addresses; Patient Info contains their phone numbers and email address. These are data sets that can be combined into one table, so it's a One to One relationship. 1. From the Create tab choose Query Design 2. Add tables Patients and Patient Info 3. From Patients add fields First and Last 4. From Patient Info add field Home Phone 5. Move to the Datasheet view Resultant query shows all 15 patients in the Patient Info table. The query shows the records where the matching fields exist in both tables, whether the fields you add to the query have data in them or not. 6. Move to the Design view a. On the Criteria line for the Home Phone type Is Not Null 7. Move to the Datasheet view Resultant query shows the 12 patients that are in both tables AND have phone numbers. 8. In Design view, delete the criteria 9. Double click on the slope (thin portion) of the relationship line a. If double clicking doesn't work, try right clicking and choosing Join Properties 10. Choose option 2 include ALL records from 'Patients' and only those records from 'Patient Info' where the joined fields are equal a. If you create the relationships the way I taught you, 99% of the time this will be option 2 Page 4

11. Move to the Datasheet view Resultant query shows all 76 patients in the Patients table. 12. In the Design view, notice the arrow pointing toward Patient Info 13. Add the Med Rec field from both tables We only see the Patient Info Med Rec for the records that exist in that table. Notice Kerry Katz has a Med Rec, but no phone number. 14. Create a Home Phone number for Orville Owns a. When you move out of the field you will see the Patient Info.Med Rec field fill in Setting Default Join Type When you are working with a one to one relationship you will almost always want to set the data to join with everything from the Primary table, and the data that matches in the secondary table. We can set the default so the join type is already created in the relationships window. 1. From the Database Tools tab choose Relationships 2. Double click on the slope (thin portion) of the relationship line a. If double clicking doesn't work, try right clicking and choosing Edit Relationship 3. Click on the Join type button 4. Choose Option 2 and click OK a. Click OK again to close the Edit Relationship window Page 5

Action Queries By default queries are Select Queries. We have seen one other type, a Crosstab query. The next few pages are dedicated to Action Queries. There are four types of action queries: Make Table, Append, Update, and Delete. You can change a query type from the Query Design Tab. Running Action Queries All four of the action query icons have the exclamation point in common. Once you have set the query you can view the datasheet, but if you want it to take 'action' the query must be RUN. When you Run a select query it will "select" the data for you, when you run an "action" query, it will perform the query action. Double clicking on a closed query will "run" the query. Delete query Delete a group of records from a table. For example, you could use a delete query to remove products that are discontinued, patients that have been inactive for more than three years, drugs that have been discontinued, or imported data that you don't need. Delete queries remove entire records, not just selected fields within records. Access will not delete the records if they are related to other tables unless you have set the relationship to Cascade Delete. If the cascade delete option is turned on in the relationship the delete query will erase the related records from the child table as well. Update query Makes changes to fields in a set of records within a table. For example, you can raise prices by ten percent for all dairy products, change the area code for everyone living in Alachua, or change all of Dr. Smith's Patients to now be Dr. Jones's Patients. With an update query, you can change data in the individual fields of your existing tables. Append query Add a set of records from one table/query to the end of an existing table. For example, you hire a new doctor and transfer all her existing patients into your clinic. Instead of typing in all the new data, you can append it into your existing tables. The new data doesn't need to have to have the same field names, nor be in the same order, but it does need to be the same field types (text/date/number). Make table query Create a new table from all or part of the data in one or more tables. Make table queries can be useful to create tables to export to other databases, for example, you may want to create a table based on your employees for another database that doesn't contain *all* the fields that you are storing in this database. Make table queries are also very useful for creating backup and history tables that contain old records. For example, you could create a table that stores all your old orders before deleting them from your current Orders table. Action Query Exercises The following exercises will take you through each of the four action queries. 1. Make Table Query Make a table of people whose last name starts with the letter "M" 2. Delete Query Delete the "M" people from the original Patient Table 3. Update Query Move everyone in the new M people table from Florida to Georgia 4. Append Query Add everyone in the M people table to the original Patient Table Page 6

Action Query 1: Make Table Query 1. From the Create tab, choose Query Design 2. Add the Patients table and close the Show Table window 3. Add all the fields to the query 4. From the Query Design tab, in the Query Type group, click Make Table 5. In the Table Name: type M People and click OK 6. On the Criteria line for the Last field type M* a. Access will change it to Like "M*" b. This will give us only the patients with a last names that begins with an M Page 7

7. The Datasheet view shows the five records that will be put into our new M People Table 8. In the Design view, click on the Run button 9. Click Yes to make the new table 10. Close and save as "1 Make M People" 11. Open Tables Patients and M People a. M People has the five new records b. Patients still has 76 records, the M People were copied to the new table Action Query 2: Delete Query 1. From the Create tab, choose Query Design 2. Add the Patients table and close the Show Table window 3. Add all the fields to the query 4. On the Criteria line for the Last field type M* a. Access will change it to Like "M*" 5. From the Query Design tab, in the Query Type group, click Delete a. Notice the changes at the bottom of the query Page 8

6. The Datasheet view shows the five records that will be deleted from table Patients 7. In the Design view, click on the Run button 8. Click Yes to make the delete the records (rows) 9. View datasheet, there should be no records because there are no longer any records in the Patient table that match our criteria 10. Close and save as "2 Delete M People" 11. Open Tables Patients and M People a. M People still has the five records b. Patients now has 71 records, the M People were deleted from this table i. Because the cascade delete was on, we have also deleted any Patient Info records for those M People Action Query 3: Update Query 1. From the Create tab, choose Query Design 2. Add the M People table and close the Show Table window 3. Add all the fields to the query 4. From the Query Design tab, in the Query Type group, click Update 5. Set Update To line to move everyone to PO Box 555, Macon, GA, 55555 Page 9

6. The Datasheet view shows only the fields being updated 7. In the Design view, click on the Run button 8. Click Yes to make the update the records (rows) 9. View datasheet, all the addresses are the same 10. Close and save as "3 Update M People to GA" Action Query 4: Append Query When we use an append query we can use different field names, but the data types and field sizes need to be the same. Modify Table 1. Open M People Table in Design View 2. Change Field names a. FIRST to FIRST NAME b. LAST to LAST NAME c. ST to STATE 3. Close and save the table Page 10

Append Query 1. From the Create tab, choose Query Design 2. Add the M People table and close the Show Table window 3. Add all the fields to the query 4. From the Query Design tab, in the Query Type group, click Append 5. On the Append To: line, match the missing fields a. If we had not changed the field names they would have all been perfect matches and everything would have filled in. 6. In the Design view, click on the Run button 7. Click Yes to append the records (rows) Page 11

8. Try to run the Action Query again a. Confirm the append b. View Error message This error message tells us that it cannot append these five records due to Key violations. The Patients table is using the Med Rec as the key, so we try to add these five people again, it violates the "no duplicates" rule of the Primary Key Field. 9. Close and Save the query as "4 Append M People" 10. Open the Patients and M People Tables a. Patients again has 76 records (Some that live in Macon GA) b. M People still has its five records Navigation Pane 1. Close all database objects 2. Notice the sort order of the Query list a. It's sorted by Object Type i. Append Queries ii. Delete Queries iii. Make Table Queries iv. Select Queries v. Update Queries 3. Right click on All Access Objects at the top of the navigation pane a. Choose Sort by > Name 4. Right click on All Access Objects at the top of the navigation pane a. Select Search Bar 5. In the search bar type M People a. View the filtered list 6. Click on the Clear Filter button at the end of the line, or erase "M people" to clear the filter Page 12

Creating an Active Lookup We have created lookup lists in our tables and forms using the Lookup Wizard and Combo boxes. We have created them by typing in our own values and by looking them up from another table. In this exercise we are going to learn how to have the combo box provide a list of values from the current table. Not from another table, not from a stagnant list we provide, but from the data already in the field. If we do this right, as we change our data in this field, the list will update. In the Basics Queries workshop we created a query with a missing parameter value to prompt us for criteria. Any database object dependent upon this query will request this data. We can modify the criteria by adding asterisks (*) so that we only have to enter in a few letters (i.e. Like "*" & [Which City?] & "*"), but we still have to be careful with our spelling, and we have to know our data very well. Creating an Active Lookup will help us find the exact value. Active Lookup Steps Create unique list of values Create form with combo linked to unique list of values Create query that has a criteria to match the form combo If you want a report, you'll need to add these steps Create a report to print out second query Create a button on the form to preview the report Step 1 Create a unique list of values 1. From the Create tab, choose Query Design 2. Add the Patients table and close the Show Table window 3. Bring down the City Field 4. Turn on the Totals a. From the Show/Hide group on the Design tab 5. Close and Save as "City List" Page 13

Step 2 Create a Form with a Combo box for "City List" 1. Create a Combo box from the controls on the Design tab 2. Follow the steps of the wizard a. "I want the combo box to get the values from another table or query" b. Choose Queries, select Query: City List c. Include the City Field (it's the only one ) d. No Sorting e. The width of the column is ok f. Label the Combo Box Which City? 3. Adjust the size and placement of the box 4. Close and save as "Which City" Step 3 Create a query from table, with criteria from form 1. From the Create tab, choose Query Design 2. Add the Patients table and close the Show Table window 3. Add all the fields to the query 4. Right click on the Criteria line for the City field and choose Build 5. Choose the Combo Box on the Form a. Double click to open each level i. 8 Access Queries II ii. Forms iii. All Forms iv. Which City b. From the second column doubleclick on the combo box (Combo0) 6. Click OK 7. Close and Save as "Patients by City" Page 14

Step 4 Create the report 1. From the Create tab, choose Report Wizard 2. Follow the steps of the wizard a. If necessary choose "Query: Patients by City" and bring over all the fields b. No Grouping c. No Sorting d. Tabular, Portrait e. Save as Patients by City" 3. If the form is not open, enter a city name 4. Close and Save Step 5 Create button to Open Report 1. Open "Which City" Form in Design view 2. Create a Command Button by using the controls on the Design tab a. Report Operation, Preview Report 3. Test the button 4. Save Changes Page 15

Step 6 Test by adding new data 1. Open the Patients table in Datasheet view 2. Add a record to the Patient table a. Use Med Rec 123 456 b. Use a City that's not on the list i. Not Gainesville, Macon, Micanopy, Starke, or Waldo Suggestions: Alachua, Jacksonville,... 3. Open the "Which City" Form 4. Find new city in the Which City? combo box and click on the Preview Report button Page 16