A SQL Select statement provides a query that can be interpreted by a particular
DBMS to retrieve particular data from its tables.
The most basic form of the SQL statement specifies columns (fields) to retrieve
from one or more tables in the rows (records) of its result set.
In ADO terms, the result set will be the records of a Recordset object. The
syntax for this most elementary SQL Select statement is this:
Select FieldList From TableName
where FieldList is a comma-delimited list of field names existing in the specified
table denoted by TableName. You might specify a Recordset containing rows, for example, each
of whose contents represented the LastName and FirstName fields from the Employees table of
the current database:
Select FirstName, LastName From Employees
You can specify all fields from the table by using the asterisk character (*)
instead of writing out all their names:
Select * From Employees
You could use a SQL statement such as this as the CommandText property of
a Command object or as an argument to the Execute method of a Connection object or the Open method
of a Recordset object, as illustrated in Listing 9.22.
LISTING 9.22
USING A SQL STATEMENT IN A STRING VARIABLE TO OPEN A RECORDSET
Dim strSQL As String
strSQL = _
"Select [First Name],[Last Name],HireDate From Employees"
The advantage of first storing the query text to a string variable is that
it makes the line that manipulates the data object method more readable. More importantly,
it enables you to possibly build the SQL statement in several steps in your code, thus permitting
more complex logic to be used in your program to query data.
NOTE - Field Names with Spaces: Some DBMSs (such
as Microsoft Access) permit spaces in the names of fields. To refer to such a
field in a SQL statement, you should surround it with square brackets. A field
named "Last Name" would appear as [Last Name] in a SQL query.