You are on page 1of 16

Chapter 2 – Getting Started with SQL in Access

Chapter 2 – Getting Started with SQL in Access

Objectives
2.1 Opening the students.mdb Database
2.2 Getting into SQL in Access
2.3 Typing a Simple SQL Query
2.4 Running a SQL Query
2.5 Saving a SQL Query
2.6 Editing s SQL Query
2.7 Viewing Table Designs
2.8 Viewing Data in Tables
2.9 Deleting Tables or Queries
2.10 Printing SQL Code and Output from Access
Summary

Objectives

 Get into SQL in Access


 Type in a query, edit, execute and save a query
 Explore how to view table designs, data in the tables, delete tables
or queries and print from SQL in Access

2-1
Chapter 2 – Getting Started with SQL in Access

2.1 Opening the students.mdb Database

First of all, you must get your system to install with Microsoft Access 2003 (or any
version) before you can load the database. The database that we will use throughout
this course is called students.mdb and your instructor will let you know where you can
get this database downloaded. You need to copy this database into the directory that
you wish to use during the course of your studies.

In order to get Access started, click to Start > Microsoft Office > Microsoft Office
Access 2003 (as shown in Figure 2.1).

Figure 2.1 Start Access

2-2
Chapter 2 – Getting Started with SQL in Access

Once start Access, click to the ‘Open’ icon at the icon toolbar to open an existing
database (as shown in Figure 2.2). The Open dialog box will then appears and from
the Look in drop-down list, select the directory in which you have stored your
students.mdb file (as shown in Figure 2.3). Once you had successfully located the
students.mdb, select the file and then click the Open button.

Open existing
database

Figure 2.2 Opening an Existing Database

Figure 2.3 Opening an students.mdb Database

2-3
Chapter 2 – Getting Started with SQL in Access

Once Access successfully opens the students.mdb, you will see exactly what is shown
in Figure 2.4.

The Objects list on the left side of the screen that includes the various categories of
objects that are available. When you select a category from the Object list, each object
you may choose from within the selected category is listed on the right. Figure 2.4
shows all the available tables (that have been created) and some other options that you
can use to create new table.

Figure 2.4 students.mdb Database

2-4
Chapter 2 – Getting Started with SQL in Access

2.2 Getting into SQL in Access

Queries in SQL in Access have to be typed in the SQL view screen. There is no
option to go directly to the SQL view screen in Access. To get to the SQL view screen,
we have to go through Design View.

The following steps will lead you to the SQL view screen:
1. Select the Queries from the Object list.
2. Then select the Create query in Design view on the right side.
3. Click New to open the New Query windows (Figure 2.6).
4. In the New Query window, select Design View and then click OK.
5. Next, the Show Table dialog box opens, shown in Figure 2.7. Click Close.
6. Click to the SQL button in the upper-left corner (below the File menu) of the
main Microsoft Access window, as shown in Figure 2.8. Alternatively, you
can open the View menu and select SQL view (Figure 2.9).

Step 2

Step 1

Figure 2.5 Create a query

2-5
Chapter 2 – Getting Started with SQL in Access

Step 4

Step 3

Step 5

Figure 2.6 The New Query Dialog Box

Click
‘Close’

Figure 2.7 Closing the Show Table Dialog Box

Click
‘SQL’

Figure 2.8 Getting into the SQL View Screen from Design View

2-6
Chapter 2 – Getting Started with SQL in Access

Figure 2.9 Getting into the SQL View Screen from View menu

2-7
Chapter 2 – Getting Started with SQL in Access

2.3 Typing a Simple SQL Query

The screen below shows the SQL view which you can type a SQL query. You can try
by type the following SQL query on this screen (Figure 2.10).

SELECT *
FROM student;

Figure 2.10 SQL view

Note:
You can also type the query in Ms Word or Notepad, and copy and paste it into SQL
view.

2-8
Chapter 2 – Getting Started with SQL in Access

2.4 Running a SQL Query

To preview the output of the resulting SQL query, you need to ‘run’ the SQL query.
In order to achieve this, you need to click the red exclamation mark (!) button on the
toolbar (see Figure 2.11).
The output of the SQL query that you have entered earlier is shown in Figure 2.12.
The result of the query will be shown as a table of data and this return all the rows and
columns of the student table.

Run the
query

Figure 2.11 Running a SQL Query

Figure 2.12 Output Screen

2-9
Chapter 2 – Getting Started with SQL in Access

2.5 Saving a SQL Query

A SQL query can be save in Access. This actually allows you to edit and re-run the
query in future without the needs to re-type the SQL statement again.
To save the SQL query, you have to click to the ‘Save’ button at the toolbar (see
Figure 2.13). Next, the Save As dialog box will appears (Figure 2.14) and now you
need to name the query. You can name your query with any name you like and by
default it will called “Query1”. Now, enter the query name as “Display all students
records” and click OK button.

Click the
‘save’ icon
to save the
query

Figure 2.13 Saving a SQL query

Figure 2.14 Naming a SQL query

2-10
Chapter 2 – Getting Started with SQL in Access

Access now has save your query in the Queries category of the Object list. Noticed
the icon for the query that you have just saved, which is a SELECT query (Figure
2.15). There are many other types of queries in Access SQL, which we will look at in
later chapters.

Icon for
SELECT query

Figure 2.15 SQL query in Queries category

2-11
Chapter 2 – Getting Started with SQL in Access

2.6 Editing a SQL Query

Once you have saved your query, later on you may retrieve and can modify it.
To edit a query (see Figure 2.16):
1. Select the Queries category in the Object list.
2. Then, click the query that you would like to edit
3. Next, click the Design button located at the top of the screen.

Your query now opens on the SQL view screen (as in Figure 2.17), enabling you to
edit and re-run your query. When you are finished the changes, close the SQL view
and resave if it necessary.

Step 3

Step 2

Step 1

Figure 2.16 Selecting a SQL query to Edit

Figure 2.17 SQL view

2-12
Chapter 2 – Getting Started with SQL in Access

2.7 Viewing Table Designs

Table designs provide the detail information about the tables in the database (such as
field names, field data types and etc.). To view the design of a particular table in the
database, you must select Table tabs at the Object list. At the right side, it listed
down all the available tables that been created for that database.
For us to explore the table design, select the Course table and then click to the Design
button.
Now you can view the design of the Course table, as shown in Figure 2.19. From the
Table Design, you can see the detail information for each field of that table.

Access Table

Figure 2.18 Selecting a Table to View Its Design

Figure 2.19 Table Design View for Course Table

2-13
Chapter 2 – Getting Started with SQL in Access

2.8 Viewing Data in Tables

To view the data in a table, you can simply select a table in Figure 2.18, such as the
Course table and then you can either double-click the table that you would like to
view or highlighting the table; then click the Open button. Figure 2.20 shows the data
from the Course table.

The table is presented in rows and columns. Each row represented the records of that
entity. You can even resize each column by adjust the column margins with the
mouse cursor.

Figure 2.20 Data in Course Table

2-14
Chapter 2 – Getting Started with SQL in Access

2.9 Deleting Tables or Queries

Since tables and query are saved separately, you can delete a query without affecting
the table in anyway. However, if you delete a table first, you will not be able to run
the queries that are dependent on it (even if you saved the query earlier).

The same steps are use to delete both table and query:
1. Select the Table / Queries tabs in the Object list.
2. Next, select the table or query that you have saved and now to be remove
3. Once have selected your table or query, you can click the ‘X’ button on top of
the menu.
4. Alternatively, you can right-click the query and select Delete from the shortcut
menu (see Figure 2.22)
Step 3

Step 1 Step 2

Figure 2.21 Selecting a Query to Delete

Figure 2.22 Deleting query using the Right-click Shortcut menu

2-15
Chapter 2 – Getting Started with SQL in Access

2.10 Printing SQL Code and Output from Access

First of all, you must ensure that your system is attached to a printer before you can
start to print.

Print SQL Code from Access

SQL code cannot be printed directly from Access. Therefore, to print the SQL code,
you have to copy and paste the code into a word processing program, such as Notepad
or Word, and then print it from the word processor.

Print the Query Output from Access

Access allows you to print the tables that produce by the SQL code. You have to click
to the ‘Print’ button at the toolbar while the tables of data are being view (see Figure
2.23).
Click to print

Figure 2.23 Printing Output from Access

Summary

The chapter basically introduces the user to Access environment. At the end of this
chapter, they should know how to get into SQL view to create, run and edit a SQL
query. Additionally, they should also know how to view the table design, the table of
data, delete a table or query and print the output.

2-16

You might also like