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-15.MDB.
After a few moments of processing, the form shown in Figure 1-44 should appear. This form is based on the
tblQueryDefs table, which stores a record for each query you save. To
add a new query to the table, add a new record and enter the SQL
statement in the SQL Text control. You may find it easier to copy the
SQL from an existing query (see Step 2 for more details). Type in a
name and description. Notice that creation and modification times are
automatically updated.

Figure 1-44. The saved queries form, frmSavedQueries
To use a saved query in your code, search the tblQueryDefs table for the name of a query and get ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access