Building Queries Microsoft Access 2016 Learning and Development Centre Access 2016 - Building Queries ABOUT THIS TRAINING MANUAL This manual is yours to keep and is intended as a guide to be used during the training course and as a reference once the course is completed. Each section begins with a list of topics to be explored. The courseware is designed so that each topic is fully explained and step–by-step instructions are given. There are a number of conventions used in this training manual: Format Description BOLD ITALICS This is indicates a command to follow e.g. an option or button to press [ ] Keys to press are shown in square brackets e.g. [space] HEADING This marks the start of a method for performing a specific task This marks additional information This marks additional information and a warning [CTRL] + [Page Up] This means the first key is used in conjunction with the second 1 Access 2016 - Building Queries 2 Access 2016 - Building Queries Building Queries - Course Contents QUERYING IN ACCESS ........................................................................................................................................ 5 UNDERSTANDING QUERIES ......................................................................................................................................... 6 CREATING A QUERY .................................................................................................................................................. 7 ADDING FIELDS TO A QUERY ....................................................................................................................................... 8 USING QUERY CRITERIA ........................................................................................................................................... 10 RANGE OPERATORS ................................................................................................................................................ 11 WILDCARDS .......................................................................................................................................................... 11 MULTIPLE CRITERIA ................................................................................................................................................ 12 SPECIAL CRITERIA OPERATORS .................................................................................................................................. 13 WORKING WITH CALCULATED FIELDS ......................................................................................................................... 14 FORMATTING CALCULATED FIELDS ............................................................................................................................. 15 ADVANCED QUERIES ....................................................................................................................................... 17 WORKING WITH ACTION QUERIES ............................................................................................................................. 18 DELETE QUERY....................................................................................................................................................... 19 UPDATE QUERY ..................................................................................................................................................... 20 APPEND QUERY ..................................................................................................................................................... 22 MAKE TABLE QUERY ............................................................................................................................................... 24 PARAMETER QUERIES .............................................................................................................................................. 26 USING MULTIPLE PARAMETERS ................................................................................................................................. 27 CROSSTAB QUERIES ................................................................................................................................................ 29 EXERCISES ....................................................................................................................................................... 35 QUERYING A TABLE - 1 ............................................................................................................................................ 36 QUERYING A TABLE - 2 ............................................................................................................................................ 37 QUERYING A TABLE – 3 ........................................................................................................................................... 38 MORE INFORMATION ..................................................................................................................................... 39 MORE INFORMATION .............................................................................................................................................. 40 ACCESS KEYBOARD SHORTCUTS ................................................................................................................................. 41 3 Access 2016 - Building Queries 4 Access 2016 - Building Queries Topics The following topics are covered in this chapter: Understanding Queries Creating a Query Adding Fields to a Query Using Query Criteria Range Operators Wildcards Multiple Criteria Special Criteria Operators Working with Calculated Fields Formatting Calculated Fields 5 Access 2016 - Building Queries U ND ERSTAN D ING Q U ER IES An Access user can retrieve and display selected data by building a query on single/multiple tables or data from other software packages. A query allows the user to view, change and analyse data in different ways. Queries can also be used as the source of records for forms and reports. The most common type of query is a Select Query. It retrieves data from one or more tables and displays the results in a datasheet where the user can update records. For example, if you had a customers table, you may wish to build a query that only showed you customers from a particular city. A select query can also be used to group records and calculate sums, counts, averages and other types of totals. 6 Access 2016 - Building Queries C REAT ING A Q UE RY A query is created using a wizard or ‘from scratch’ in design view. In design view, users specify the data they want to work with by adding the tables that contain the data, and then filling in the design grid. Query wizards do all the basic work once the user has provided answers to a series of questions. TO CREATE QUERY IN DESIGN VIEW 1. On the Ribbon, click the Create tab 2. In the Other group, click Query Design The Show Table dialogue box appears 3. Select the table that you wish to query 4. Choose Add 5. Once all the required tables have been added choose Close 7 Access 2016 - Building Queries A D DI NG F IE L DS TO A Q U ERY When a query is created the user has to decide what fields will be shown when the query is run e.g. Surname, Department etc. By default when a query is first created there are no fields. In order for fields to be displayed in the query, the user has to copy the field headings from the Field List box, to the query grid below. Field List TO ADD FIELDS TO A QUERY 1. Select the required field from the field list box 2. Click and drag the field to a column below Or Double click the field heading 8 Access 2016 - Building Queries TO REMOVE FIELDS FROM A QUERY 1. Click into the field you wish to remove 2. On the Design tab, within the Query Setup group, click Delete column The column should now be selected TO RUN A QUERY 1. Bring all the fields required to the grid below 2. On the Design tab, within the results group, click Run The results will now be shown 9 Access 2016 - Building Queries U S ING Q UE RY C R I TE RI A To work with specific records, the user must enter examples of data, known as criteria, in the query Design, e.g. setting the criteria ‘A’, in the department code field, will find all occurrences of the Department ‘A’. The criteria is typed directly into the Criteria cell, under the relevant field (see example below). The criteria set is not case sensitive unless the query includes linked tables, in which case the criteria is case sensitive, e.g. must match the case of the values in the underlying table. Additional criteria for the same or different fields can be entered. When typing the expressions in more than one criteria cell, access combines them using the And or the Or operator. An Expression is any combination of operators, constants, literal values, functions and names of fields (columns), controls and properties that evaluate to a single value. If the expressions are in different cells in the same row, access uses the ‘And’ operator, which means only the records that meet the criteria in all the cells will be returned. If the expressions are different rows of the design grid, access uses the ‘Or’ operator, which means records that meet criteria in any one of the cells will be returned. The example below illustrates the difference between And/Or. The above criteria is set on the same row. It will return records that match both sets of criteria (results will be from both dept A and grade 1) The above criteria is set on different rows. It will return data that matches any of the criteria (results will be from either dept A or grade 1) TO CREATE A QUERY WITH CRITERIA 1. In a new query bring the required fields to the grid below 2. Type the required criteria into the required cell, under the relevant field (e.g. Smith under the surname field) 3. Repeat for other fields if required 4. Click the Run button on the Ribbon The result should now be shown 10 Access 2016 - Building Queries R A NG E O P ER ATO RS The following range operators can be specified when creating criteria. Operators > Description Greater than < Less than >= Greater then or equal <= Less than or equal to <> Not equal Sample criteria >=10000 Description Finds values greater than or equal to 10000 <500 Finds values that are less than 500 W I LD C AR DS Wildcards are used as placeholders for other characters when only part of the value is known or to find values that start with a specific letter or match a certain pattern. When querying wildcards are used to find field values, records or file name. Wildcard * Description Matches any number of characters. You can use the asterisk anywhere in a character string Example wh* finds what, white, and why, but not awhile or watch. ? Matches any single alphabetic character B?ll finds ball, bell, and bill 11 Access 2016 - Building Queries M U LT I PL E C R ITER I A Multiple criteria examples fall in to two categories And or Or. Categories Description And All conditions specified must be satisfied before access displays data. When using ‘AND’. criteria in the same field you will need to manually type the ‘and’ between the 2 criteria needed e.g. >01/01/80 and <31/12/80. This example would display records for all start dates in 1980. One field using AND With multiple ‘AND’ criteria covering more than one field, conditions must be specified on the same row. Two fields using AND OR At least one of the conditions specified must be satisfied before the data is shown. Multiple OR criteria covering more than one field should be entered on separate field rows. OR criteria in the same field can be entered, e.g. Gould or Davis would display records for both names. One field using OR One field using OR Two fields using OR 12 Access 2016 - Building Queries S P EC I AL C RI TE RI A O PE RATORS Some operators carry out special functions. Operators Date() Description Will extract records that include today’s date Between, And Will extract records within a specific range, e.g. Between 100 and 600 Not Finds records not matching the criteria set, e.g. ‘Not B’ would bring back all records that didn’t match with B Will extract records that include one value from a list of values, e.g in (Manchester, London and Leeds) will find suppliers located in Manchester, London, and Leeds. The criteria can also be entered as Manchester or London or Leeds Will extract records except those containing blank values. E.g. to see a list if suppliers who have fax numbers, enter Not Null Will extract records that do not contain values, e.g. find companies that have not placed an order In Not Null Is Null 13 Access 2016 - Building Queries W OR K ING W IT H C A LCU L ATE D F IE L DS Calculated Fields perform calculations on numeric Fields, e.g. working out VAT. The calculation is performed in a blank field and the syntax typed is: New Field Name: Calculation The ‘New Field Name’ is what you want the column to be called. To separate the field name from the calculation a colon must be used When performing a calculation, any fields referred to are to be typed within square brackets ([ ]), e.g. [Price] The mathematical operators used are multiplication (*), addition (+), subtraction (-) and division (/) TO CREATE A CALCULATED FIELD 1. In the Query Design view, add the required fields to the design grid 2. In a blank column type the required field name followed by a colon, e.g. VAT: New Field Name 3. After the field name type the calculation, e.g. [Price]*0.175 4. Press [Return] New Field Name with calculation 5. On the Ribbon, click Run to run the query 14 Access 2016 - Building Queries F ORM AT TIN G C A LC UL ATE D F IE LD S The data format can also be amended to display the currency symbol or the percentage sign. The field format can be edited in two ways: changing the field properties or typing the format required. TO FORMAT A CALCULATED FIELD 1. Position the insertion point in the relevant field 2. On the Ribbon, withinthe query setup group, click Property Sheet The field properties dialogue box is displayed 3. In the Format box click the drop down arrow A list of formats are displayed 4. Select the required format e.g. Currency 5. Close the Properties dialogue box, by clicking the cross in the top right corner 6. Click the Run button to run the query 15 Access 2016 - Building Queries NOTES 16 Access 2016 - Building Queries Topics The following topics are covered in this chapter: Working With Action Queries Delete Query Update Query Append Query Make Table Query Parameter Queries Using Multiple Parameters Cross tab Queries 17 Access 2016 - Building Queries W OR K ING W IT H A C TI ON Q U ER IES An Action Query is a query that allows the user to make changes to multiple records in one operation. The four types of action queries are: Delete Update Append Make Table Some of these queries can update live data. It is always advisable to regularly back up your data especially before using some of these action queries. 18 Access 2016 - Building Queries D EL ETE Q UE RY The Delete Query is used to delete records from a table, e.g. all orders placed in 1998. TO DELETE RECORDS 1. Create a new Query 2. In the Design tab, in the Query Type group, Delete Query 3. Add the fields required to the design grid below 4. In the criteria area, type the criteria for the records you want to delete 5. On the Ribbon , click Run Access prompts the user for confirmation 6. Choose Yes 19 Access 2016 - Building Queries U P DATE Q UE RY Update queries are used to update a group of records in a table, e.g. increasing salaries by 5%. When you are creating an update query, the updates are typed into an ‘Update To’ row that appears in the query design grid. For example, you may want to update a table so that all records for a department code column changed from A to F. To do this, you would need to type A in the criteria of the department code and type F in the ‘Update To’ row. If you wanted to update figures, a calculation would be needed in the ‘Update To’ row. For example, you may want to give everyone in department B a pay rise of 2%. To do this you would need to add B in the criteria for Department code and a calculation in the ‘Update To’ row in the Salary column. The calculation to update the salary by 2% would be [salary]*1.02. All fields referred to in a calculation must be typed in square brackets. TO UPDATE RECORDS 1. Create a new Query 2. In the Design tab, in the Query Type group, Update Query 3. Add the required fields to the design grid 4. Type the required values in the ‘Update To’ row (what the update is) 5. Type the required criteria (what to update) 6. On the Ribbon, click Run 20 Access 2016 - Building Queries Access prompts the user for confirmation of the update 7. Choose Yes 21 Access 2016 - Building Queries A P PE ND Q UE RY Append queries are used to add records from one table to the end of another. The target table will need to be set up exactly the same as the source table in order for the append to take place. TO APPEND RECORDS 1. Create a new query 2. Add the required fields to the design grid 3. In the criteria box, type the required criteria 4. In the Design tab, in the Query Type group, Append Query The append query dialogue box appears 5. To append the query results to a table in the current database, in the Table Name Box, select the table name and choose Current Database Or To append to a table in another database, in the table name box, type the table name, choose another database and then type the filename & path of the target database in the File Name box 6. In the design ribbon click Run to append the records 22 Access 2016 - Building Queries Access prompts the user for confirmation of the append 7. Choose Yes The records should now be appended to the table specified 23 Access 2016 - Building Queries M A KE T AB LE Q U ERY This Query allows a user to create a brand new table based on the results of a query. For example, a query run to show all people from the accounts department could be copied into a brand new table. TO MAKE A NEW TABLE 1. Create a new query 2. Add the required fields to the design grid 3. In the Criteria box, type the required criteria 4. In the Design tab, in the Query Type group, Make Table The make table dialogue box appears 5. In the Table Name box, type in a new name 6. Choose Current Database to create the table in the current database Or Choose Another Database to create the table in another database 7. Choose OK 8. In the Design tab, click the Run button to make a new table 24 Access 2016 - Building Queries Access prompts the user for confirmation of the new table 9. Choose Yes The new table should now be created The data existing in the new table is not linked to the original data so can be modified without affecting any other data 25 Access 2016 - Building Queries P A RAM ET ER Q UE R IES A Parameter Query is a query that when run displays a dialogue box prompting the user for information, e.g. criteria to retrieve records or a value to insert in a field. The user can design the query to prompt for more than one piece of information, e.g. prompt the user for two dates. Access can then retrieve all records that fall between the dates. Parameter queries can also be used to save time if the user runs the same select or Crosstab query frequently but with different criteria. Parameter queries avoid the need for making changes to the query design grid, instead the query may run from the database window and criteria entered via the Parameter Value dialogue box. TO CREATE A PARAMETER QUERY 1. Create a new query in design view 2. Add the required fields to the design grid 3. In the criteria cell under each field to be used as a parameter, type a prompt enclosed in square brackets e.g. [Please type in name] Access displays this prompt when the query is run. The text of the prompt must be different from the field name, although it can include the field name 4. In the Design tab, click the Run button to run the query The enter Parameter Value dialogue box displays Type Criteria Choose to Confirm 5. Type the required Criteria, choose OK Access displays data according to the parameters entered 26 Access 2016 - Building Queries U S ING M U LT I PLE P A R AMET ERS A Parameter query can prompt the user for several values using the AND or the OR functions. AND Examples Between [First Date] and [Second Date] Like [Enter first Letter] & * Combination of 1 and 2 above Description Will allow the user to extract records falling within the range order dates Will allow the user to extract courses beginning with a particular letter Will extract courses beginning with a certain letter and which have been ordered between a specific range of dates When running the Query, access will prompt the user for each parameter. All the dialogue boxes need to be completed. 27 Access 2016 - Building Queries Example Description The user will input Surname or Grade Access will prompt the user for both parameters but only one dialogue box needs to be completed 28 Access 2016 - Building Queries C RO S STAB Q UER I ES A Crosstab query is a query that calculates a sum, count, average or other type of aggregate on records, then groups the results by two types of information: one down the left side of the datasheet (Row Headings) and another across the top (Column Headings). A crosstab query can be created using a wizard or ‘from scratch’ in the query design grid. In the design grid, users specify which fields values become column headings, which fields values become row headings and which fields values are to be summed, counted, averaged or otherwise calculated. Basic ‘select’ Query Crosstab Query 29 Access 2016 - Building Queries TO CREATE A CROSSTAB QUERY MANUALLY 1. Create a new query in design view 2. Add the required fields to the design grid 3. On the Design tab, click Crosstab You will now have a new crosstab row in your design grid 4. In the Crosstab Row in the Query Design grid, choose the relevant Crosstab option for the first field e.g. Row Heading 5. Choose the relevant Crosstab option for the second field e.g. Column Heading 6. For the calculated field, in the Total row choose the required Option e.g. Sum 7. In the Design tab, click Run to view the results 30 Access 2016 - Building Queries TO CREATE A CROSSTAB QUERY USING THE WIZARD 1. In the Create tab, click query Wizard The New Query dialogue box appears 2. Choose Crosstab Query Wizard 3. Choose OK The Crosstab Query Wizard dialogue box displays 4. In the Which Table or Query contains the fields you want for the crosstab query results box, choose the required Table/Query 5. Choose Next 31 Access 2016 - Building Queries 6. In the Available Fields box, add the required field values to display as Row Headings using 7. Choose Next 8. Select the required field to display in column headings 9. Choose Next 32 Access 2016 - Building Queries 10. Choose the field to be calculated & choose the calculation e.g. sum 11. Choose Next 12. Type in the required query name 13. Choose the required view 14. Choose Finish 33 Access 2016 - Building Queries NOTES 34 Access 2016 - Building Queries Topics The following topics are covered in this chapter: Querying a Table – 1 Querying a Table – 2 Querying a Table – 3 35 Access 2016 - Building Queries Q UE RY ING A T AB LE - 1 Open the Sample Database and using tblStaff, and create the follow queries: 1. List all the employees who earn more than £10,000 2. Produce a list of employees in Grade 1 or 2 3. List staff who started after 1982 4. Display all male employees who earn less than £10,000 5. Show all females who started before 1980 6. List all employees in department A 7. Display all employees who started before 1982 and who earn less than £10,000 8. List all staff who have the surname Smith or Peters 9. Produce a list of staff in department A earning between £10,000 and £12,000 and all those in department S earning less than £11,000 10. List all employees who started with the company before 1980 and who are currently earning less than £9,000 36 Access 2016 - Building Queries Q UE RY ING A T AB LE - 2 Using the Sample Database and tblStock List, and create the following queries: 1. All product codes starting with A 2. Display all product codes starting with C or D 3. List all items priced under £100 4. List all items priced in the range £100 to £1000 5. Which Items where last delivered before 1988 6. Which items where last delivered in 1989 7. An item needs re-ordering when the Number in stock falls below 50, display all items that require reordering 8. Calculate VAT for all items at a rate of 20% 37 Access 2016 - Building Queries Q UE RY ING A T AB LE – 3 Open the Sample Database. Create the following queries. 1. Create a parameter query based on the Orders table which prompts users to fill in the Ship Country. Save the query as qryShipped Country 2. Create a Delete query which removes all the USA customers in the Customers table 3. Create a new Make table query based on the table tblStock that copies all the items with a price greater than £100 into a new table called tblhigh price 38 Access 2016 - Building Queries Topics The following topics are covered in this chapter: More Information Keyboard Shortcuts 39 Access 2016 - Building Queries M OR E I NF ORMAT ION We hope that you have found the course helpful and this training documentation easy to use. If you require more information about other courses we run, please visit our IT Training Website at: http://www.uwe.ac.uk/ldc Visit our web pages to find: A comprehensive list of all IT courses currently available with information about course contents, duration and course tutor. An up-to-date timetable of when our courses are running and availability. Online Resources including links to all our training documentation, FAQ’s and training videos. Contact Information If you want to ask the IT Training Team a question about any aspect of training, please contact us at [email protected] or phone us on ext: 81202 40 A CC ES S K EYB OAR D S HO RTCU TS Database Actions Editing Description Open existing database Open a new database Save Save record Print Display database window Find and Replace Copy Cut Shortcut Keys [CTRL] + [O] [CTRL] + [N] [CTRL] + [S] [SHIFT] + [ENTER] [CTRL] + [P] [F11] [CTRL] + [F] [CTRL] + [C] [CTRL] + [X] Paste [CTRL] + [V] Undo [CTRL] + [Z] Help [F1] Toggle between Form and Design view Other Description Insert line break in a memo field Insert current date Insert current time Copy data from previous record Add a record Delete a record [F5] Shortcut keys [CTRL] + [ENTER] [CTRL] + [;] [CTRL] + [:] [CTRL] + ['] [CTRL]+ [+] [CTRL] + [-] Description Select all Copy Cut Paste Undo Redo Find Replace Spell checker Toggle between Edit mode and Navigation mode Open window for editing large content fields Switch from current field to current record Navigating Through a datasheet Description Next field Previous field First field of record Last field of record Next record Previous record First field of first record Last field of last record Shortcut keys [CTRL] + [A] [CTRL] + [C] [CTRL] + [X] [CTRL] + [V] [CTRL] + [Z] [CTRL] + [Y] [CTRL] + [F] [CTRL]+ [H] [F7] [F2] [SHIFT] + [F2] [ESC] Shortcut Key [TAB] [SHIFT] + [TAB] [HOME] [END] [DOWN ARROW] [UP ARROW] [CTRL] + [HOME] [CTRL] + [END]
© Copyright 2026 Paperzz