[ Team LiB ] |
![]() ![]() |
Recipe 1.16 Save My Queries in a Table for Better Programmatic Access and Security1.16.1 ProblemYour 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? 1.16.2 SolutionYou 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 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 the value from the SQLText field. To use this technique in your application, follow these steps:
1.16.3 DiscussionThe core of this technique is a simple function that retrieves a value from the tblQueryDefs table. The function uses the Seek method to find the supplied value and, if it finds a match, returns the record's SQLText field value. Public Function acbGetSavedQuerySQL(strName As String) As String ' Returns a SQL string from tblQueryDefs ' In : strName - name of query to retrieve ' Out : SQL string Dim db As DAO.Database Dim rst As DAO.Recordset Set db = CurrentDb( ) Set rst = db.OpenRecordset("tblQueryDefs") rst.Index = "PrimaryKey" rst.Seek "=", strName If Not rst.NoMatch Then acbGetSavedQuerySQL = rst!SQLText End If rst.Close Set rst = Nothing Set db = Nothing End Function (If you import this module into an Access 2000 or later database,
make sure to use the Tools An obvious enhancement to this technique would be a conversion routine that reads each of your database's saved queries and converts them to records in the tblQueryDefs table. Once this conversion is complete, you can delete the queries from the database window.
![]() |
[ Team LiB ] |
![]() ![]() |