19.12.2012 Views

Computer Programming Concepts and Visual Basic David I. Schneider

Computer Programming Concepts and Visual Basic David I. Schneider

Computer Programming Concepts and Visual Basic David I. Schneider

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

332 <strong>Computer</strong> <strong>Programming</strong> <strong>Concepts</strong> <strong>and</strong> <strong>Visual</strong> <strong>Basic</strong><br />

St<strong>and</strong>ards Institute). <strong>Visual</strong> <strong>Basic</strong> uses a version of SQL that is compliant with ANSI-89 SQL.<br />

There are some minor variations that are of no concern in this book.<br />

SQL is a very powerful language. One use of SQL is to request specialized information<br />

from an existing database <strong>and</strong>/or to have the information presented in a specified order.<br />

■ FOUR SQL REQUESTS<br />

We will focus on four basic types of requests that can be made with SQL.<br />

Request I: Show the records of a table in a specified order.<br />

Some examples of orders with MEGACTY2.MDB are<br />

(a) Alphabetical order based on the name of the city.<br />

(b) Alphabetical order based on the name of the country, <strong>and</strong> within each country<br />

group, the name of the city.<br />

(c) In descending order based on the projected 2015 population.<br />

Request II: Show just the records that meet certain criteria.<br />

Some examples of criteria with MEGACTY2.MDB are<br />

(a) Cities that are in China.<br />

(b) Cities whose 2015 population is projected to be at least 20 million.<br />

(c) Cities whose name begins with the letter S.<br />

Request III: Join the tables together, connected by a foreign key, <strong>and</strong> present the records as<br />

in Requests I <strong>and</strong> II.<br />

Some examples with MEGACTY2.MDB are<br />

(a) Show the cities in descending order of the populations of their countries.<br />

(b) Show the cities whose currency has “u” as its second letter.<br />

Request IV: Make available just some of the fields of either the basic tables or the joined<br />

table. (For now, this type of request just conserves space <strong>and</strong> effort by <strong>Visual</strong> <strong>Basic</strong>. However,<br />

it will be very useful in Section 11.3 when used with a FlexGrid control.)<br />

Some examples with MEGACTY2.MDB are<br />

(a) Make available just the city <strong>and</strong> country fields of the table Cities.<br />

(b) Make available just the city <strong>and</strong> currency fields of the joined table.<br />

Normally, we set the RecordSource property of a data control to an entire table. Also, the<br />

records of the table are normally presented in the order they are physically stored in the table.<br />

We make the requests discussed above by specifying the RecordSource property as one of<br />

the following kinds of settings.<br />

Request I: SELECT * FROM Table1 ORDER BY field1 ASC<br />

or SELECT * FROM Table1 ORDER BY field1 DESC<br />

Request II: SELECT * FROM Table1 WHERE criteria<br />

Request III: SELECT * FROM Table1 INNER JOIN Table2 ON foreign field =<br />

primary field WHERE criteria<br />

Request IV: SELECT field1, field2, . . . fieldN FROM Table1 WHERE<br />

criteria<br />

The words ASC <strong>and</strong> DESC specify ASCending <strong>and</strong> DESCending orders, respectively. A<br />

criteria clause is a string containing a condition of the type used with If blocks. In addition<br />

to the st<strong>and</strong>ard operators , <strong>and</strong> =, criteria strings frequently contain the operator Like.<br />

Essentially, Like uses the wildcard characters ? <strong>and</strong> * to compare a string to a pattern. A

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!