Access Cookbook - Ken Getz [32]
To see how this works, open 01-15.MDB. Note that there are no sample tables in this database. Open the sample DDL query, qryCreateClients (see Figure 1-43). Select Query→ Run or click on the exclamation point icon on the toolbar to execute the DDL query. The tblClients table will be created, complete with a primary key and two other indexes.
Figure 1-43. A sample DDL query and the table it creates
Discussion
When you run a DDL query, Access reads through the query's clauses and creates a table according to your specifications. This allows you to precisely control the structure of the table and its indexes.
A DDL query can contain only one data-definition statement. The five types of data-definition statements are:
CREATE TABLE
Creates a table
ALTER TABLE
Adds a new field or constraint to an existing table (a constraint creates an index on a field or group of fields)
DROP TABLE
Deletes a table from a database
CREATE INDEX
Creates an index for a field or group of fields
DROP INDEX
Removes an index from a field or group of fields
Note that we specified the lengths of most of the text fields in the sample query to save space. If you don't specify a length for a text field in a DDL query, Access will assign it the maximum length of 255 characters, but that length won't necessarily affect the size of the database. The field length is just a maximum—the space is not used unless it is needed.
If you wish to create field names with embedded spaces, you'll need to surround the names with brackets; otherwise, the brackets are optional.
Like make-table queries, DDL queries do not automatically overwrite an existing table. However, unlike make-table queries, you aren't offered the option of overwriting the existing table if you want to. If you need to overwrite an existing table when running a DDL query, first execute another DDL query containing a DROP TABLE statement.
After you create (or delete) a table with a DDL query, the new table won't immediately appear in (or disappear from) the database window. To refresh the display and see the change you made, click on another object type in the database window (for example, Forms) and then on the Table tab again.
WARNING
As with other SQL-specific queries, be careful not to switch a DDL query to another query type, such as a Select query. If you do, your SQL statement will be discarded, because SQL-specific queries don't have a design-view equivalent.
You can also create tables complete with indexes using Data Access Objects (DAO) or ADOX, using VBA code, and you can use DAO QueryDefs or ADO commands to execute your DDL statements in code.
TIP
New DDL syntax was added in Access 2000 (Jet 4.0), but few Access programmers ever used it because it didn't work in the SQL pane of the Access user interface. The only way to take advantage of the new syntax was by executing ADO commands. In Access 2002 and Access 2003, this syntax is supported inside of Access. For example, you can use ALTER TABLE ALTER COLUMN to change the data type of an existing field in a table. In the past, you had to drop the column and create a new one.
1.16. Save My Queries in a Table for Better Programmatic Access and Security
Problem
Your application uses a lot of queries, and you don't want these queries available or even visible to the users of your application. Also, you call your queries from VBA code. How can you hide the queries from users and make them easier to retrieve, modify, and execute?
Solution
You can create a query-management table that stores the SQL string of your queries in a memo field. Each query is named and includes a description. This technique allows you to store your queries in a table rather than in the Access collection of queries. You can also create a simple VBA function that you can use to quickly retrieve the SQL string of any of your saved queries.
Open and run frmSavedQueries from 01-16.MDB. After