Access Intermediate

Similar documents
Access Intermediate

Access Intermediate

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

Complete Quick Reference Summary

Microsoft Access XP Queries. Student Manual

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

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

Table of Contents COURSE OVERVIEW... 5

New Perspectives on Access Module 5: Creating Advanced Queries and Enhancing Table Design

Introduction to Microsoft Access 2016

Creating a Crosstab Query in Design View

Follow these steps to get started: o Launch MS Access from your start menu. The MS Access startup panel is displayed:

Using Microsoft Access

Name: Class: Date: Access Module 2

As your databases continue to evolve, you will need to incorporate advanced queries and reports. This chapter addresses how to create and use action

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

Microsoft Access 2016 Intro to Select Queries

Excel Intermediate

Access Intermediate

Microsoft Access XP (2002) - Advanced Queries

Microsoft Access Illustrated. Unit B: Building and Using Queries

Microsoft Certified Application Specialist Exam Objectives Map

Formulas, LookUp Tables and PivotTables Prepared for Aero Controlex

Access 2003 Introduction to Report Design

CartêGraph Training Navigator

Microsoft Access XP (2002) Queries

Access - Introduction to Queries

Microsoft Access 2010

Microsoft Access 2013

Microsoft Access 2013

Microsoft Office Illustrated Introductory, Building and Using Queries

Database Design Practice Test JPSFBLA

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

Using Microsoft Access

ADD AND NAME WORKSHEETS

DEVELOPING DATABASE APPLICATIONS (INTERMEDIATE MICROSOFT ACCESS, X405.5)

Level 6 Relational Database Unit 3 Relational Database Development Environment National Council for Vocational Awards C30147 RELATIONAL DATABASE

Access Module 2: Building and Using Queries

Microsoft Office 2010: Introductory Q&As Access Chapter 2

Excel Tutorial 5: Working with Excel Tables, PivotTables, and PivotCharts. 6. You can use a table s sizing handle to add columns or rows to a table.

Excel 2010: Basics Learning Guide

Rev. C 11/09/2010 Downers Grove Public Library Page 1 of 41

Veco User Guides. Grids, Views, and Grid Reports

Microsoft Excel 2010 Handout

Tutorial 3 Maintaining and Querying a Database. Finding Data in a Table. Updating a Database

Excel 2007 New Features Table of Contents

TECHNOLOGY COMPETENCY ASSESSMENT MODULE Microsoft Access

Excel Tables & PivotTables

Using Microsoft Excel

EXCEL 2003 DISCLAIMER:

Excel Intermediate

Word Module 5: Creating and Formatting Tables

EXCEL TUTORIAL.

Microsoft Access 2016

Microsoft Office Access Learn how to use the Query window in Design view. Tutorial 3b Querying a Database

Excel Tables and Pivot Tables

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

Microsoft Access 2016

Preview. MS Access Tutorial. Data Files Available on

Using Microsoft Access

Chapter11 practice file folder. For more information, see Download the practice files in this book s Introduction.

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

Microsoft Excel 2010

Working with Macros. Creating a Macro

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

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

Using Microsoft Access

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

ICDL & OOo BASE. Module Five. Databases

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

Microsoft Access II 1.) Opening a Saved Database Music Click the Options Enable this Content Click OK. *

PowerPoint Presentation to Accompany GO! All In One. Chapter 13

eschoolplus+ Cognos Query Studio Training Guide Version 2.4

Database Use & Design

Excel Basics Rice Digital Media Commons Guide Written for Microsoft Excel 2010 Windows Edition by Eric Miller

Query Studio Training Guide Cognos 8 February 2010 DRAFT. Arkansas Public School Computer Network 101 East Capitol, Suite 101 Little Rock, AR 72201

CHAPTER 6 SUMMARY. Objective 1: Identify Good Database Design

Chapter 4: Single Table Form Lab

STIDistrict Query (Basic)

DESIGNING, BUILDING, AND USING DATABASES (BEGINNING MICROSOFT ACCESS, X405.4)

The specific steps to build Wooden Crafts database are here: 1. Create New Database. i. After opening Access, click Blank Desktop Database :

Microsoft Access Basics

ACCESS 2007 FOUNDATION

Microsoft Access 2010 Select Queries

Create and Modify Queries 7

Advanced Topics in Access: Queries

Introducing Gupta Report Builder

Using Microsoft Access

EXCEL BASICS: MICROSOFT OFFICE 2010

Tutorial 5: Working with Excel Tables, PivotTables, and PivotCharts. Microsoft Excel 2013 Enhanced

Intro to Excel. To start a new workbook, click on the Blank workbook icon in the middle of the screen.

MIS 2502 Access 2007 Tutorial

Using Reports. Access 2013 Unit D. Property of Cengage Learning. Unit Objectives. Files You Will Need

Microsoft Access 2007 Module 1

Tutorial 5 Advanced Queries and Enhancing Table Design

Microsoft Access 2016 Intro to Forms and Reports

Workbook Also called a spreadsheet, the Workbook is a unique file created by Excel. Title bar

Advanced Excel Skills

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

Numbers Basics Website:

Transcription:

Access 2013 - Intermediate 103-134 Advanced Queries Quick Links Overview Pages AC124 AC125 Selecting Fields Pages AC125 AC128 AC129 AC131 AC238 Sorting Results Pages AC131 AC136 Specifying Criteria Pages AC136 AC138 AC142 AC153 AC241 AC242 Wildcards Pages AC237 AC241 AC242 AC244 Relational Queries Pages AC129 AC131 Calculated Fields Pages AC156 AC161 AC245 AC249 Using Conditional Functions Pages AC245 AC249 AC261 AC262 Aggregate Fields Pages AC161 AC167 Parameter Queries Pages AC249 AC253 Top Value Queries Pages AC267 AC269 Update Queries Pages Delete Queries Pages Crosstab Queries Pages AC256 AC262 Finding Duplicate Records Pages AC262 AC264 Finding Orphan Records Pages AC264 AC266 Using SQL View Page AC262 This icon designates that a recorded lecture for the designated topic is available on my website Queries (Review) Queries allow the user to extract data from a database, across multiple tables. Queries can also show calculated data data that is not stored in the database. Queries can also be used to update data in a table or delete data from tables. I have also used queries to create new tables using the query results. 1

Select Queries Select queries are one way for the user to extract data from database tables Select the Create Ribbon, then the Query Design button in the Queries group. Select the tables to be used in this query If you miss one, you can add it later The QBE Grid appears Query by Example Table-like object in the bottom of the window Select the fields to be included in the query Double-click a field name to add it to the end of the list of fields in the QBE grid Can drag fields from the field list to the grid and drop (inserted before the field you drop it on) Ctrl-Click fields to select more than one Ctrl-Click again to remove from selected list Shift-Click to select a range of fields Can select all the fields by double-clicking the table name (all fields will highlight) then drag any field to the grid. Changing the fields selected To select an existing field in the grid, point to the thin gray bar above the field name. Cursor should change to a thick, black arrow. Click to select, drag to select multiple columns (fields). Remove Fields Select the field(s) Press the delete key on the keyboard Move Fields Select the field(s) Point to the bar above the field until the cursor changes to a white arrow. Drag to new location (dark bar shows drop location) 2

Executing (Running) the Query View button gives you a preview of the queries results Executes the query Preview and Execute are basically the same when doing Select Queries. They have different results when doing Update or Delete Queries. To return to design mode, click (only appears in run mode) Specifying the Sort Order Unless you specify otherwise, records in the query result are sorted by primary key Click in the Sort row of the field you want to sort by Choose the sort order Ascending (A-Z, 1-10) Descending (Z-A, 10-1) Note you can also remove sorting here You can choose a secondary sort key This field will be used when the data in the primary sort field is the same (tie breaker) NOTE: Secondary sort keys must always appear to the right of the primary sort key (there can be fields between them). You can change the display order in datasheet view (query results) if you need the fields to show in a different order. You can add third, fourth, etc. sort keys if you need to 3

Specifying Query Criteria Criteria allow you specify criteria (conditions) that a record must meet in order to be included in the query results Specified in the Criteria rows of the QBE grid Click in the Criteria row of the field that the criteria applies to Comparison operators: > greater than >= greater than or equal to < less than <= less than or equal to <> not equal to = equal to (optional) Not reverse result of condition Specifying different data types Surround text that is entirely comprised of digits with quotes Surround dates and times with # Tip: Try entering the criteria without the quotes or #. Often, Access will insert them automatically. If not, add them manually 4

Wildcard * * represents any character, any number of characters, or no characters. Basically, * represents anything *smith matches any string that ends in smith also matches simply smith smith* matches any string the starts with the letters smith also matches simply smith *smith* matches any string of characters that contains smith anywhere, beginning, middle or end. Also matches simply smith Completely case insensitive Access adds the word Like to the criteria to make it compatible with standard SQL syntax. Tip: To use the wildcard with dates, specify the date in m/d/yyyy format. E.g. Like 7/*/2011 Between And Matches all numbers between the first number and the second number, including those two numbers Can also be used with text and dates In(value1, value2, value3 ) Matches all values that are contained in the list Can be used for numbers or text Multiple Criteria You can specify criteria for more than one field Records must match each criteria to be included in the query results AND condition (match this AND that AND that) Can also specify OR conditions match one criteria OR the other (or both) list each criteria in a different row (even if in different fields) Each row is considered a new criteria. If mix AND and OR conditions, must specify AND criteria in all rows 5

Renaming Fields in Query Results Can change the name of a field in the query result column header Most frequently used for calculated fields Place the desired field name before old field name (or equation) and separate with a colon (:) NOTE: Captions in table design override these names Specifying Criteria for Lookup Fields Remember that lookup fields actually store the primary key value (StudentID, Course Number) not the values shown in the lookup lists. You cannot specify a criteria of Like Prog* in the course lookup field To resolve this problem, use a relational query that combines fields from multiple tables (see next section) Relational Queries Relational queries allow you to combine results from more than one table. Build query exactly the same way except add more than one table To add a table at a later time, click the Show Table button in the Query Setup group of the Query Tools Design Ribbon If table relationships are not defined, define them here Drag field from parent to matching field in child Add fields from any table in any order Lookup fields will automatically show the lookup value (from table design mode) If don t want the lookup, add the key from the parent instead of the linking field from the child Sort using any fields from any combinations of tables Specify criteria from any fields from any tables 6

Creating Calculated Fields Int Access 2.1: Concatenating Text With the power of today s computers, Access can quickly combine data from multiple fields into equations that calculate new values that are not stored in the data but only appear in the results of a query. Calculated fields can also be included in forms and reports Because of this fact, most database designers remove all fields from the database that can be calculated and use queries to calculate the values only when needed. The true power comes from Access ability to combine fields from multiple tables in one equation The easiest way to create calculated fields in Access is to use the Expression Builder The Expression Builder allows you to select fields, operators and functions from a list to create complicated (or simple) equations. The alternative is to type the field names and operators manually, which is more susceptible to errors. 7

Create a query in design view Add non-calculated fields to the query Optional. A query can be comprised of just calculated fields, or the calculated fields can precede the non-calculated fields. Often, the support data used in the calculation is included in the query as well. Tip: If you include non-calculated fields, save the query before defining your calculated fields. The query s fields will display in the Expression Builder s Expression Categories column Click in the next available QBE column Field column Click the button in the Query Tools Design tab, Query Setup group. In the Expression Categories column (center), doubleclick the field name to insert it into the expression Access includes [square brackets] around field names (in case they have spaces in them) If the field doesn t appear in the Expression Categories column (did you save the query?), in the Expression Elements column: Click the + next to the database name Click the + next to Table Click the appropriate table name The fields for that table will appear in the Expression Categories column Enter operators for the equation by typing them + - * / Order of precedence (like in Excel) applies * / are processed first, followed by + - Use parentheses to override order of precedence Repeat for all fields and operators to be included in the equation Use regular editing techniques (delete, highlighting, etc.) to change the expression if necessary 8

Access will not verify the validity of your equation until you try to run the query. Error messages often don t help determine what s wrong. To correct the error, click the calculated field in the QBE and launch the Expression Builder again. The Expression Builder will already include the equation you entered previously. Proofread your equation to uncover the error. If you get the Enter Parameter Value dialog box, you inserted a field in the equation that Access doesn t recognize. Naming the Calculated field By default, Access names the calculated field Expr# This name is not very handy if you need to refer to the field at a later time (form, report or another query) One technique you could use is to change the field s Caption using its properties (see Formatting Calculated Fields below) I prefer to add the name of the field in the QBE grid itself. Point to Expr# at the beginning of the equation in the QBE grid Double-click Expr# to select it. Type a new name for the field. CAUTION: don t delete the colon ( : ) that follows the field name. It must be there to separate the field name from the equation. Tip: You can enter the field name in the Expression Builder. Precede the expression with your field name followed by a colon. Tip: Don t include spaces in your calculated fields. Put spaces in the caption (see below) just like you did with regular fields in Tutorial 2. 9

Formatting Calculated Fields Normal fields have their formatting designated in the query s design structure, but calculated fields are not included in the structure. To change the formatting of a calculated field, you must access it s properties Right-click the field and select Properties from the popup menu Or, click the button in the Query Tools Design tab, Show/Hide group In the Properties dialog box, designate the format for the field and if appropriate the number of decimal places. Tip: You can leave the Properties dialog box open and format multiple fields. Tip: Select multiple fields (see above) to change all their properties to the same values at once. You can also designate a Caption for the calculated field in the Properties window. You can use a calculated field as a sort field in a query You can include criteria in calculated fields. To use a calculated field to create another calculated field, you must first save the query. When you then reopen the Expression Builder, the calculated field will be included in the Expression Categories list so you can use it in subsequent calculations Concatenation is a programming term for sticking two (or more) strings together. Handy in many database queries Operator is & or + Can include string literals which are characters that are surrounded by apostrophes or quotes (pick one) These characters appear in the concatenation exactly as you type them. 10

Using Conditional Functions in Calculated Fields Int Access 2.2: Using Conditional Functions If you watched closely when using the Expression Builder, you may have noticed that Access comes with many predefined functions that you can use to create calculated fields (similar to Excel functions) Two convenient functions are ISNULL and IIF ISNULL is a Boolean function (results in True or False) that allows you check to see if a field has been left blank. You cannot check for blank fields by using =0 or = "" The function is easy to use. Simply insert the field name you want to check inside (parentheses) that follow the field name. Ex. ISNULL(MobilePhone) Be sure to use the field name, not the caption. ISNULL is typically used with an IIF statement IIF (Immediate IF or Inline IF) allows you to ask a Yes/No question and process one way if the answer is yes and a different way if the answer is no. IIF(condition, true processing, false processing) IIF includes three parts after the parenthesis Condition to be checked must result in a yes/no answer often uses the relational operators used to designate criteria Boolean functions like ISNULL are also commonly used here True processing What should be done if the condition is true False processing 11

Examples IIF(gender="F", "Ms.", "Mr.") Results in Ms. for females and Mr. for males IIF(ISNULL(Company), LastName & ", " & FirstName, Company) Split over three lines for readability Customers may be companies or individuals. If the Company field is blank (null), individual s last and first name (concatenated) are used. If the Company field is not blank (false), company name is used. Note how quotation marks surround actual text values (literals) (could use apostrophes as well) Summary / Aggregate Fields Aggregate fields combine data from many records to calculate statistics: sum, average, min, max, count Select the fields to calculate statistics for (add to QBE grid) (you can list fields more than once) Click the Totals button on the toolbar A new Total: row will be added to the QBE grid For each field, select the statistic you want calculated in the Totals row You can also specify criteria for records to be added to the aggregate query. Set Totals row to Where Specify criteria Grouping totals In addition to calculating totals, you can also calculate subtotals by grouping the statistics. Grouping creates subtotals for each unique value found in the grouping field. You can create multiple levels of grouping 12

Parameter Queries Int Access 2.3: Parameter Queries Sometimes, the criteria for a query is not known at design time; we want the user to enter the criteria when the query is run. Parameter queries allow the user to enter criteria when the query is run In the criteria row for the appropriate field, enter a prompt describing what information the user should enter. SURROUND THE PROMPT WITH [SQUARE BRACKETS ]. That s it!!! When the user runs the query, a dialog box appears first, asking the user to enter the appropriate value (using the prompt you specified). You can specify more than one parameter in a query. Multiple dialog boxes will appear, one after the other. Users can be allowed to enter wildcards in the parameters, but the parameter must be preceded by Like You can also include relation operators (< > etc.) in the criteria before the parameter. Tip: for even more flexibility (for the user) include wildcards around the parameter. Like * & [Enter a color:] & * Matches any record that contains the color the user entered If user enters blue this will match Blue Green, Light Blue, etc. This has a side effect of displaying all records if the user enters nothing for the prompt. Without the wildcards, if the user enters nothing, nothing ends up in the result set. Tip: To run a parameter query again without switching back and forth between Design and Datasheet view, press the F5 key in Datasheet view or click the Refresh All button in the Home tab, Records group. 13

Top Value Queries Often, you (or management) will not want to see all the records in a result set, but only the top records. Only the top 10% of students are honor students Access makes this very easy. Key Concept: In order to generate a top query your query must be sorted on the correct field. To find the honor students, you have sort by GPA, descending. First specify all query requirements (fields, criteria, sorting) Tip: Sort ascending to find the bottom and descending to find the top Next, use the option in the Query Tools Design tab, Query Setup group to designate which records should be included. Click the dropdown arrow to select from commonly used options Alternatively, type the number or percent of records you want to see in the Return: box. Run the query. 14

Update Queries Int Access 2.4: Update Queries Update queries allow you to change values in multiple fields in many records with one command. I suggest you test the query in Select mode first to ensure the correct records are selected before running the Update query. It s not a bad idea to create a backup copy the database before doing update or delete queries. Create a new query like always. Select fields and specify criteria to designate which rows should be changed. Designate you want to make an update query by clicking the Update button in the Query Type group of the Query Tools Design Ribbon Enter data in the Update To: row for the appropriate field whose value should be changed. Click the View button on the toolbar to preview the records that will be changed. Not much help, but a good double-check Click the Run button on the toolbar to actually execute the update query THERE IS NO UNDO FOR THE UPDATE QUERY!! 15

Delete Queries Delete queries allow you to delete records that meet specified criteria It s not a bad idea to create a backup copy the database before doing update or delete queries. Create a new query like always Designate you want to make a delete query by clicking For information only. the Delete button in the Query Type group of the Query Tools Design Ribbon Select the fields and criteria necessary to designate the records to be deleted. Click the View button on the toolbar to preview the records that will be changed. Not much help, but a good double-check Click the Run button on the toolbar to actually execute the update query THERE IS NO UNDO FOR THE DELETE QUERY!! 16

Crosstab Queries Int Access 2.5: Crosstab Queries Crosstab queries create tables of statistics for two fields (or more) Crosstab queries are very similar to PivotTables created in Excel This is one kind of query that I use the Query Wizard (Create Ribbon, Queries group) to help with the process. Click the Query Wizard button. Select the Crosstab Query Wizard Select the table that contains the appropriate data Select the field to be used as the row heading Multiple fields create subtotals Select the field for the columns Tip: I think about how many unique values the row header field and column header field will have. The field with most unique values generally works best as the row header Columns and rows can be reversed with no effect on the data and reversing them is pretty easy to do in Design mode. Select the field and the statistic to be calculated Name your query Note that the wizard automatically creates an extra column that holds the total for each row in the crosstab Check out Design view. Many features could be customized. Crosstab, Update and Delete queries get different kinds of icons in the Navigation Pane than Select queries and grouped (by type) in the Queries list. 17

Dealing with Yes/No fields in Crosstabs If your column (maybe row) field is a Yes/No field, Access uses -1 (yes) and 0 (no) for the field values (column headings). This is usually confusing and unacceptable. To make the column heading more meaningful Switch to Design view. Zoom in on the Yes/No field column Use the IIF function to convert the -1/0 to more acceptable values Example: IIF(Fulltime, "Fulltime", "Part-Time") This checks the Fulltime field. If it is -1 (yes, true) the column heading displays Fulltime. Otherwise, the column heading displays Part-Time. Don t forget the quotes Finding Duplicate Records Int Access 2.6: Find Duplicates Query As mentioned in Unit 1, tables are often designed using artificial keys key values that are made up to simplify database design (E.g. customerid, studentid) These kinds of fields are usually Autonumber fields. When you use these kinds of keys, Access cannot directly check to see if the database contains duplicate records because the Autonumber values are never duplicated. John Smith could accidentally be added to the database twice, be given two distinct customer ID numbers and be sent all mailings twice. Though this can be annoying (getting two mailings) it will also cause inaccuracies in any statistics that are created be the duplicate record s values will be used more than once. A common query, Find Duplicates, can be used to locate duplicate records. 18

Because it s so common, Access includes a wizard to make the process easier. Create a query using the Query Wizard and select the Find Duplicates option. The wizard dialog boxes will guide you through the process. Select the table you want to find duplicate records in. Select the logical key field (or fields). These are the fields that you feel might contain incorrect, duplicate data (as opposed to fields where you expect duplicate data). Select additional fields you d like to see in the query results. These fields should include the primary key and might include other fields to help you verify which records are duplicated. Designate the name for the query. The query results will automatically be sorted by the logical key fields you selected, making recognizing duplicate records easier. Caution: when you delete the duplicate record be sure to delete the correct one. Also be sure to reassign and child records where the linking field value is the duplicate record ID to the correct record ID. Do you see why avoiding duplicate records in the first place is a good idea? 19

Finding Orphan Records Int Access 2.7: Find Unmatched Query Orphan records are records in a child table that have no corresponding record in the parent table. An order record whose customer number doesn t exist A course section record whose instructor number doesn t exist. Access avoids orphan records by requiring you to select a linking field value that exists in the parent table if Referential Integrity is set for the relationship. Orphan records (in Access) are only possible if referential integrity is not on (and it always should be). NOTE: This type of query can also be used to find parent records without child records. Customers who have not placed orders Students who haven t registered for classes. Access also includes a wizard for creating a Find Unmatched Query Create a query using the Query Wizard and select the Find Unmatched option. The wizard dialog boxes will guide you through the process. First select the table that does have records Note this could be a query Select the table that you suspect has missing records Designate which field the tables have in common (linking field) Select the fields you want to see in the results. Select enough fields to help you recognize which records have issues. Name the query. The query results will show which records in the first table have NO corresponding records in the second table. 20

Using SQL View Whenever you build a query in Access (any kind of query), Access automatically creates the SQL that corresponds to your query designations. Some queries are not possible to represent in SQL; like parameter and crosstab queries Filtering and sorting in datasheet view are not stored/reflected in SQL SQL-Structured Query Language-is a (fairly) standard language used by all database programs to represent query requirements. Normally, you won t need to use this view, but if you were to start developing programs that manipulate databases, you ll have to become familiar with SQL. SQL View in Access can help you learn SQL. Create a query in Access using the QBE grid or a Wizard and then switch to SQL View to see the corresponding SQL Some extremely complex queries can only be created using SQL they are too complicated for the QBE grid To view the SQL for a query, click the View button in the Views group of the Home Ribbon and select the SQL View option. You can actually edit the query here if you know SQL. The QBE will reflect the changes if possible. 21