Microsoft Access VBA Macro Programming

(Tina Sui) #1

Chapter 12 SQL Queries


Chapter 12 SQL Queries


S


QL queries do not form part of the VBA language but it is very important to
understand how they work and what they can do.

The object of this chapter is to show you the queries you can use with VBA and how you
can use them.
In a relational database, it is very unusual to open a single table and view meaningful
information from it. This is because all the tables store different bits of information and only
by joining these tables together can you see a proper view of the data. For example, one table
might provide customer names and addresses and another table will show all the orders that
have been placed by those customers. In order to extract all the orders for each customer, a
query needs to be put together joining the customer and the order tables using a common key
field (usually a numerical ID).
SQL queries are the life blood of any Access application in presenting data to the user and
for manipulating data. They can be used in various ways through VBA.
Some of the examples in this chapter use the Access sample database Northwind to illustrate
the data. To open this database, load Access but do not select a database. In the pane in the
center of the window (Available Templates), click Sample templates. This will display the
icon for Northwind, among others. Click this and click the Create button on the right-hand
side of the window while holding down theSHIFTkey (so the database does not autorun).
When this has loaded, you will need to expand out the Navigation pane on the left-hand
side of the window to see the database objects. Do this by dragging the right-hand border of
the Navigation pane across the window to the right.


149

Free download pdf