Locked lesson.
About this lesson
Creating your first query to search through records in a database.
Exercise files
Download this lesson’s related exercise files.
9.01 creating-a-query - Exercise.docx50.2 KB 9.01 creating-a-query - Exercise solution.docx
95.2 KB TechGurusTicketing22.accdb
900 KB TechGurusTicketing22 - Complete.accdb
912 KB
Quick reference
Creating a Query - Query Design
Create a simple query using Query Design to search for records based on specific criteria.
When to use
We create a query whenever we want to perform a fast search for records based on criteria we specify.
Instructions
Simple Query - Query Design
- From the Create tab, in the Queries group, select Query Design.
- From the Add Tables pane, select ‘tblTicket’ and click Add Selected Table.
- Drag the ‘TicketNumber’ field from ‘tblTicket’ into the grid.
- Change the Sort to Ascending.
- From the Query Design tab, in the Results group, click Run.
- Switch to SQL View and review the code.
- Close the Query.
- Name the Query ‘qryTest’.
Creating a Query Using Multiple Fields
- Reopen 'qryTest'.
- Switch to Design View.
- Drag the ‘CustomerName’ field from ‘tblTicket’ into the grid.
- From the Query Design tab, in the Results group, click Run.
- Review the SQL Code in SQL View
- Switch to Design View.
Sorting Query Results
- From the 'Customer Name field in the grid, click the drop-down arrow next to Sort.
- Select Ascending or Descending to sort by that field.
Adding Criteria
- Drag the ‘Priority’ field from ‘tblTicket’ into the grid.
- Add the Criteria, ‘Moderate’. (Criteria needs to be in quote marks "")
- From the Query Design tab, in the Results group, click Run.
Adding Fields from Other Table to the Query
- From the Add Tables pane, select ‘tblStatus’ and click Add Selected Table.
- Drag the ‘Status’ field from ‘tblStatus’ into the grid.
- In the Criteria field, type 'Active' (Criteria needs to be in quote marks "")
- From the Query Design tab, in the Results group, click Run.
AND/OR Conditions
When we add two or more pieces of criteria, the Query will run a search using the AND condition. We can change this to run an OR condition. I.e. instead of returning tickets with a priority of high AND a status of Active, we can return tickets with a priority of high OR a status of Active.
- Switch to SQL View.
- Replace the word AND in the code with OR.
- From the Query Design tab, in the Results group, click Run.
Hints & tips
- To run a query that includes all fields from a table, drag the asterisk (*) from the table to the grid.
- If we do not specify a sort order, the default is ascending.
Lesson notes are only available for subscribers.