- HubPages»
- Technology»
- Computers & Software»
- Computer Software
Microsoft Access Query
Introduction
Here are some helpful tips for using the Microsoft Access query tools to manipulate database tables. We will demonstrate how to create a Select query and also how to access the SQL generated by the query designer.
First, we create a simple table to work with. If you already have a table, you can use that instead. Our table, called tPlayer, consists of a list of names, positions, uniform numbers, and salaries. Our query will extract a subset of the table information and sort it alphabetically by name.
Table Design
Table Data
PlayerID
| PlayerName
| Position
| UniformNumber
| Salary
|
---|---|---|---|---|
1
| Cheaney
| Forward
| 40
| 900000
|
2
| Graham
| Guard
| 20
| 800000
|
3
| Bailey
| Guard
| 22
| 800000
|
4
| Lindemann
| Center
| 50
| 100000
|
5
| Nover
| Center
| 45
| 500000
|
6
| Reynolds
| Guard
| 21
| 500000
|
7
| Alford
| Guard
| 12
| 600000
|
8
| Knight
| Forward
| 25
| 10000
|
9
| Gordon
| Guard
| 2
| 500000
|
10
| Jeffries
| Forward
| 2
| 800000
|
Add the table to the query design
After creating the table in the database and the sample table, we create a new query. While the Query Wizard is useful for extremely elementary queries, we always start with the Query Designer in order to have access to the full capabilities of Microsoft Access. The query is initially empty; the first thing we do is add the Player table to the query. Refer to the figure below.
At this point we need a little direction. What should the query do? Since we are creating a "Select" query, the implication is that we are interested in a subset of the table data or an organized presentation of all the table data.
Let us assume for the sake of this tutorial that we are interested in a roster of all the players and their uniform numbers, sorted by player name. This is a subset of the table data since we plan to exclude salary and position. We can specify the sort order and the desired fields through the query designer. Simply double-click on the PlayerName and UniformNumber fields. Each field will be dropped in to the query grid. Refer to the following screen shot.
Add the desired fields to the query design
Sort the query results by Player Name
We cannot assume that the underlying table data is sorted in any particular order. Regardless of how the rows were entered into the table, always add a sort command to the query to ensure the results are sorted properly. Simply click on sort field below the desired field (in this case, PlayerName) and select Ascending or Descending.
Note that nothing we do here will affect the underlying data. A Select Query, by definition, is non-destructive. When the query executes, a snapshot of the data is presented. All the rows in the table(s) remain unchanged.
"Run" the query
View the query results
Microsoft Access uses the terminology "Datasheet View" to describe the results of running a Select Query. From the Query Design Toolbar, select Datasheet View. The query will be submitted to the database engine and the results will be presented on the screen.
Note that this is a snapshot view and it is not up-datable. It is possible to create a Select Query that is update-able, but that is beyond the scope of this tutorial.
Refer to the image below for the query results.
Datasheet View
SQL is generated by the Microsoft Access Query Tool
Behind the scenes, it's important to note that the query designer actually generates SQL (Structured Query Language). The Microsoft Access database engine actually processes the SQL rather than the query design. Experienced database designers often skip the designer tool and manually enter the SQL when creating a query.
Here is the SQL that was generated by the query we created in this tutorial:
SELECT tPlayer.PlayerName, tPlayer.UniformNumber
FROM tPlayer
ORDER BY tPlayer.PlayerName;
Accessing the SQL View of the query
Structured Query Language
Conclusion
In this tutorial we demonstrated how to create a simple "Select" query using the Microsoft Query Tool. We also explained how SQL is generated based on the query that is designed.