![]() |
DAO Advanced Programmingby Dan Haught IntroductionData Access Objects, or DAO, is a powerful programming model for database services. Originally designed as an ODBC layer for Microsoft Visual Basic version 2.0, DAO has evolved into a model that encompasses Microsoft Jet and ODBC, and in the future, OLEDB. This session covers advanced topics in DAO programming that will give you the tools you need to become an advanced programmer using the DAO model. This session is of primary value to developers creating database applications using Microsoft Access, Microsoft Visual Basic, Microsoft Excel, or Microsoft Visual C++. Some of the material here is based on work done for the "Microsoft Jet Database Engine Programmers Guide" from Microsoft Press (ISBN 1-55615-877-7). This book is the most complete reference for DAO and Jet programming. The following topics will be discussed:
Back to Main Technical Papers Page Copyright 1996 FMS, Inc. All rights reserved
A Quick Overview of DAOUp until DAO 3.1, DAO was defined as: "The Programmatic Interface to Microsoft Jet". Jet is the database engine used by Microsoft Access and Microsoft Visual Basic. DAO has historically been synonymous with Jet, in that Jet was the database engine that it mapped to. As of DAO version 3.1, an important change has occurred. DAO 3.1, through the ODBC-Direct extensions, offers for the first time the ability to completely bypass Microsoft Jet. It is because of this important new functionality that at a conceptual level, DAO is no longer tied to Jet. It is now defined as: "A Programmatic Interface for Database Services" Where Can I Use DAO?DAO is available as a programming tool in to following development environments:
How Do I Get DAO?The DAO components are available in:
How Does DAO Licensing Work?If your application's users have any of the following products installed on their workstations:
then they can use both the DAO part of your application and DAO to write their own applications without restrictions. If your application's users don't have any of these products installed, then they can only use DAO in the context of your application. They cannot use DAO to write their own applications by using the DAO component you ship with your application. A Brief History of DAODAO had its genesis as a part of Visual Basic 2.0 known internally at Microsoft as "VT Objects". This component allowed ODBC access with a very limited set of options. With the November 1992 introduction of Access 1.0, DAO entered version 1.0 status, and allowed Access developers to use a limited set of database objects in a Microsoft Jet database. With version 2.0 of DAO, as introduced by Access 2.0, a much fuller object model was available. There was almost complete control over creating and modifying objects, and opening a variety of recordset types against data. Additionally, the concept of programmatic security control was introduced. The first 32-bit version of DAO was created soon after for the ODBC desktop driver pack introduced by Microsoft. Then, in 1995, DAO 3.0 was introduced with Access 95 and VB4 simultaneously. This version offered a full 32-bit typelib and added new properties and methods to round out the object model. In addition, a new Error object was added to allow easier access to runtime errors. DAO 3.1 was in beta status as of the writing of this paper, and its release date is uncertain. DAO 3.1 represents the first break with Microsoft Jet. Since version 3.1 allows direct access to ODBC data sources through its ODBC-Direct functionality, DAO is no longer only an interface to Jet. In the future, DAO will be the programmatic object model for OLE DB. The following diagram illustrates the evolution of DAO. ` Why Use DAO?DAO is a well-known, easy to use object model for database services, including Microsoft Jet, ODBC, and in the future, OLE DB. DAO is also multi-lingual-you can use it from VB and VBA environments, FoxPro, and Visual C++. Your knowledge of DAO allows you to access a variety of data sources from a variety of development environments. DAO is also Microsoft's strategic object model for Database Access. Therefore, you can assume that it is going to be around for a long while. The DAO ModelDAO is implemented as a hierarchy of objects and collections. Each of the object types is represented within this hierarchy. The following diagram illustrates the model:
Objects and CollectionsMost object types have a collection that acts as a holder for all the objects of that specific type. For example, each the QueryDefs collection holds all QueryDef objects in the database. The following syntax is used to refer to objects in a collection:
Traversing collectionsYou can traverse or iterate through a DAO collection in one of two ways: For intCounter = 0 To object.collection.Count - 1 ' do something with the object Next intCounter or, if you are using VBA, you can use For Each object In Collection ' do something with the object Next object The second form is obviously more compact and readable. (This is just one of the examples of how VBA is superior to Visual Basic or Access Basic for working with collections). Opening and Closing Object VariablesWith VBA (and Visual Basic and Access Basic in earlier versions), you can set object variables to represent an object in the DAO hierarchy. For example, the following code sets a TableDef object variable to a specific table in the database, and then uses that variable to point to the TableDef:Dim tdfTmp As TableDefSet tdfTmp = DBEngine.Workspaces(0).Databases(0).TableDefs("Customers")Debug.Print tdfTmp.Name To close the object variable, use the Close method. Note that there are certain objects which you do not want to close without understanding how DAO interacts with object close commands:
There are other internal idiosyncracies that are documented in the README.TXT file that ships with Access 95 and VB4.
Object Creation and ModificationDAO allows you to create new objects, and modify existing ones. This section outlines the steps necessary to accomplish this. Creating ObjectsTo create an object, the general steps are:
The rest of this section shows the SQL and DAO ways to create objects. Creating a DatabaseDAO:Dim dbNew As DatabaseSet dbNew = CreateDatabase("C:\MYTEST.MDB, dbLangGeneral, dbVersion30)SQL:No SQL equivalent. DAO:Dim dbsTmp As DatabaseDim tdfTmp As TabledefDim fldTmp As FieldSet dbsTmp = OpenDatabase("C:\MYTEST.MDB")Set tdfTmp = dbsTmp.CreateTableDefs("MyTable")Set fldTmp = tdfTmp.CreateField("MyField", dbText)tdfTmp.Fields.Append fldTmpdbsTmp.TableDefs.Append tdfTmpdbsTmp.CloseSQL:Dim dbsTmp As DatabaseSet dbsTmp = OpenDatabase("C:\MYTEST.MDB")dbsTmp.Execut ("CREATE TABLE MyTable (MyField Text);")dbsTmp.Close Setting Field PropertiesDAO:
SQL:Using SQL Data Definition Language statements, you can only specify the field names and data types. Use programmatic DAO access to specify all properties. Using Temporary ObjectsUnlike other objects in the DAO hierarchy, you can create QueryDef objects and use them without having to append them to the QueryDefs collection. This is a powerful feature that allows you to create queries on the fly, execute them, and not have to worry about deleting them from the database when you are done with them. Modifying ObjectsSome properties can't be changed on existing objects. For example, although you can add new fields to an existing table, you cannot change the data type of an existing field. DAO strives to be as lean and efficient as possible. The act of changing a field's data type actually relies on a number of operations such as restructuring the table, converting all the existing data, and writing the converted data back to the original table. If DAO were to support such operations, it would be a much larger component and require much more memory, Some applications that use DAO, such as Microsoft Access, allow you to change the data type of an existing field. This is accomplished through functionality supplied by Access, not DAO. If you want to modify an object such as the field data type, you must:
Types of PropertiesWhen using DAO to access Jet databases, it is important to understand the types of properties available through Jet. Properties are divided into two categories: Engine-Defined PropertiesThese properties are defined and managed by Jet. That means that these properties will always exist for new objects. For example, when you create a new field object, it always has a default set of properties such as Type and AllowZeroLength. User-Defined PropertiesThese are properties that are defined by the user of Jet. This user can be your application, or an application such as Microsoft Access. When you use Jet to create an object, User-Defined properties do not exist until you create them. As an example, if you create a field object in Microsoft Access using DAO, certain Access-defined (which in this case also means user-defined since Access is the "user" of Jet) properties will not exist (such as "Description") until you either create the property using DAO, or use the Microsoft Access interface to add a value in the Description field.
Working with DataThere are several advanced topics regarding data access using DAO. This section covers these topics. Choosing the Right RecordsetMicrosoft Jet (through DAO) offers several options for recordset types. The type of recordset you choose should be based on your specific data access needs. The available recordset types are: TableThis type of recordset refers to either a local table in the current database, or a linked table that resides in another database. When you use a Table type recordset, Jet opens the actual table itself, and any changes you make are made to the table directly. Note that a Table-type recordset can only be based on a single table-it cannot be based on a query that joins or unions two or more tables. Use this type when you need direct access to indexes. For example, if you want to use the Seek method to locate data based on an index, you must use the Table type recordset, since it is the only one that directly supports indexes. DynasetThis is the most flexible type of recordset. A dynaset is a logical representation of data from one or more tables that is accessed internally through a set of ordered pointers and pages of data. You can use a dynaset to retrieve or update data based on a set of joined tables (as with a query). You can also represent heterogeneous joins in a recordset-the tables joined into the dynaset can be based on disparate data sources such as Jet databases, Paradox tables, SQL Server data, etc. Use this type when you want a picture of data against multiple tables, and/or want to update the data in multiple tables. SnapshotThe Snapshot type or recordset is a static, read-only picture of data. The data in the Snapshot is a fixed picture of the data as it existed when the Snapshot was created. Use this type of recordset when you want a fixed picture of data, and don't need to make changes to the data. Advanced Recordset OptionsThere are variety of options you can use when working with Dynaset and Snapshot recordsets. The following table shows these options:
Inconsistent updates are those that violate the referential integrity of multiple tables represented in a multi-table dynaset. If you need to bypass referential integrity, use the dbInconsistent option and Jet will allow you to do so. Microsoft Jet TransactionsMicrosoft Jet implements a sophisticated transaction model that allows you define sets of data operations as an atomic unit. You can then tell Jet to commit or rollback the unit as a whole. There are several developer issues in working with transactions that you should be aware of. Scoping of TransactionsTransactions are scoped at the DAO Workspace level. Because of this, transactions are global to the Workspace object, not to a specific database or recordset. If you change data in more than one database, or more than one recordset, and you issue a RollBack command, the Rollback affects all of the objects opened within the workspace. Issues With the Temporary DatabaseDatabase transaction systems typically operate by buffering the data changes within a transaction to a temporary database. Then, once the transaction is told to commit, the contents of the temporary database are merged back into the original database. Jet uses a similar scheme-it buffers transactions in memory until cache memory is exhausted. It then creates a temporary Jet database and writes transaction changes there. This temporary database is created in the directory pointed to by your TEMP variable. If the disk space available for transactions is exhausted, a trappable runtime error occurs, allow you to handle the condition by either freeing up space, or issuing a RollBack to cancel the transaction.
Developing an Object DictionaryAs an example of the object and property interrogation capabilities of DAO, I have included two add-ins with this paper, one for Access version 2.0 and one for Access version 7.0. These add-ins allow you dump the entire DAO structure of a database to an ASCII file. See the .WRI files that accompany these add-ins for complete information on how to install and use them. The Access 2.0 VersionFirst, let's look at the Access 2.0 version and see how it works. All functionality is found in the following procedures found in the module behind the frmDAOStructureDumper form:
The Access 95/VBA WayNow let's look at the VBA implementation of the same code base. You can see that code is much more efficient because we can now use "late binding"-that is we can Dim Foo As Object, and pass that Object around to subroutines. Also, we can use the For Each Next construct to walk through collections:
Exercises for the ReaderYou are welcome to extend this add-in add more functionality. For example, you may want to add the ability to write the results to a set of tables in the current database, rather than text files. Additionally, you may want to add to ability to get the structure of non-DAO objects such as forms and reports.
Semi-Documented Tools and ResourcesThere are several Microsoft-Jet specific tools that you can use that are not fully documented in the documentation that ships with Microsoft products. Although these tools are not DAO-specific, they can make it easier to develop and maintain DAO applications. TypeLib BrowsersSince the DAO 3.0 component includes an OLE Type Library, you can use a TypeLib browser to view all of the objects, collections, methods and properties, including stuff that is not documented. I have used the following two products with great success:
ISAMStatsMicrosoft Jet contains an undocumented function called ISAMStats that shows various internal values. The syntax of the function is: ISAMStats ((StatNum As Long [, Reset As Boolean]) As Long Where StatNum is one of the following values:
See the included add-in (JETMETER.EXE) for an example of how to use this function. ShowPlanMicrosoft Jet implements a cost-based query optimize in its query engine. During the compilation process of the query, Jet determines the most effective way to execute the query. You can view this plan using the ShowPlan registry setting. To use this setting, use the Registry Editor that comes with your operating system (REGEDIT.EXE for Windows 95 or REGEDT32.EXE for Windows NT) and add the following key to the registry:
Under this key, add a string data type entry named JETSHOWPLAN in all capital letters. To turn ShowPlan on, set the value of this new entry to "ON". To turn the feature off, set the value to "OFF". When the feature is on, a text file called SHOWPLAN.OUT is created (or appended to if it already exists) in the current directory. This file contains the query plan(s). MSLDBUSR.DLLYou can use the 32-bit program LDBVIEW.EXE program to view the users currently logged into a database, or you can use the MSLDBUSR.DLL component for programmatic access to this information. These files and associated documentation are included with this paper.
Wrapping UpBy now, you should be comfortable with both the importance of understanding DAO, and how to use it your database applications. As the future unfolds, you will be able leverage your knowledge of DAO in more powerful and advanced database work. Back to Main Technical Papers Page
Copyright © 1998, FMS Inc. All rights reserved. This information may not be republished, reprinted or retransmitted in any form without the express written permission of FMS Inc. The information provided in this document is provided "as is" without warranty of any kind. |