microsoft office access 2007: advanced course 01 querying ... · true. you can view the syntax of...
TRANSCRIPT
Microsoft Office Access 2007: Advanced
Course 01 – Querying with SQL
Slide 1
Cours
e I
LT
Querying with SQL
Course objectives
Use SQL statements and examine
them in Access
Write SQL statements to create
queries and sub-queries, and assign
aliases
Create an SQL-specific query and
attach it to a command button
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
Slide 2
Cours
e I
LT
Topic A
Topic A: SQL and Access
Topic B: Writing SQL Statements
Topic C: Attaching SQL Queries to
Controls
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
Slide 3
Cours
e I
LT
Sample SQL Statements
SELECT * FROM Product;
SELECT Product.ProductID,
OrderItem.ItemQuantity FROM
Product, OrderItem;
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
Slide 4
Cours
e I
LT
A Query in SQL View
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
Slide 5
Cours
e I
LT
Topic B
Topic A: SQL and Access
Topic B: Writing SQL Statements
Topic C: Attaching SQL Queries to
Controls
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
Slide 6
Cours
e I
LT
Writing an SQL Statement
1. Create a new query in Design view
2. Switch to SQL view
3. In the query window, write the SQL
statement
4. Save the query with a relevant name
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
Slide 7
Cours
e I
LT
Syntax for WHERE Clause
SELECT FieldName
From DataSource
Where criteria;
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
Slide 8
Cours
e I
LT
Aliases
SELECT lngRetailerID as Vendor, strAddr1 as Address, strPhone as Contact
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
Slide 9
Cours
e I
LT
Syntax to Display Matched Records
SELECT DataSource1.FieldName,
DataSource2.FieldName
FROM DataSource1, DataSource2
WHERE DataSource1.CommonField=
DataSource2.CommonField;
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
Slide 10
Cours
e I
LT
Syntax to Display Unmatched
Records
SELECT DataSource1.FieldName
FROM DataSource1
WHERE CommonField not in
(SELECT DataSource2.CommonField
FROM DataSource2);
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
Slide 11
Cours
e I
LT
Context–Sensitive Help
1. Open the query in SQL view
2. Place the insertion point on the
keyword
3. Press F1 to view the Access Help
window
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
Slide 12
Cours
e I
LT
Topic C
Topic A: SQL and Access
Topic B: Writing SQL Statements
Topic C: Attaching SQL Queries to
Controls
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
Slide 13
Cours
e I
LT
SQL Specific Queries
Union query
Pass-through query
Data-definition query
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
Slide 14
Cours
e I
LT
Create a Union Query
1. Create a query in Design view
2. Close the Show Table dialog box
3. On the Design tab, click Union to
open the SQL window
4. Enter the SQL statement for the
query in the window
5. Save and run the query
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
Slide 15
Cours
e I
LT
Attach a SQL Query to a Button
1. Create a SQL query and save it
2. Open the form in Design view
3. Create a command button
4. In the wizard, under Categories, select
Miscellaneous
5. Under Actions, select Run Query
6. Select the query created
7. Select an option to display on the button
8. Edit the name of the button and click Finish
9. Update and close the form
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
Slide 16
Cours
e I
LT
Command Button Wizard
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
Slide 17
Cours
e I
LT
Course Summary
Learned how to use SQL statements
and examine them in Access
Learned how to write SQL statements
to create queries and sub-queries, and
to create aliases
Learned how to create an SQL-
specific query and attach an SQL
query to a command button
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
Review Questions:
1. What wildcard character can be used in the SELECT statement to display all of the fields from a
table?
A. Backslash (\)
B. Asterisk (*)
C. Ampersand (&)
D. Pound sign (#)
2. Which character is used to end an SQL statement?
A. Period (.)
B. Comma (,)
C. Semicolon (;)
D. Pound sign (#)
3. Which character is used to specify the table name along with the field name?
A. Period (.)
B. Comma (,)
C. Semicolon (;)
D. Pound sign (#)
4. True or False: In an SQL statement, the SELECT clause specifies the data source for the query.
A. True
B. False
5. True or False: To view the syntax of the SQL statements that are automatically created by Access in
SQL view, open the Design view and then click the SQL View button.
A. True
B. False
6. What type of query is used to combine fields from more than one table or query?
A. Union
B. Pass-through
C. Data-definition
D. Combine
Answer Key:
1. B
An asterisk (*) is the wildcard character that can be used in the SELECT statement to display all of
the fields from a table.
2. C
A semicolon (;) is used to end an SQL statement.
3. A
A period (.) is used to specify the table name along with the field name.
4. B
False. The FROM clause specifies the data source for the query.
5. A
True. You can view the syntax of the SQL statements that are automatically created by Access in SQL
view. To open a query in SQL view, first open it in Design view and then click the SQL View button on
the status bar.
6. A
A union query is used to combine fields from more than one table or query.