DAO Advanced Programming

原创 2002年04月11日 18:07:00

DAO Advanced Programming

by Dan Haught
Vice President of Product Development



Data 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 DAO

Up 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:

  • Access 2.0
  • Access 95 (version 7)
  • Microsoft Visual Basic (version 4)
  • Microsoft Excel (version 7)
  • Microsoft Visual C++
  • Any development environment that can use an OLE Typelib

How Do I Get DAO?

The DAO components are available in:

  • Access 2.0
  • Access 95 (version 7)
  • Microsoft Visual Basic (version 4)
  • Microsoft Excel (version 7)
  • Microsoft Visual C++ (version 2.01 and later)
  • The DAO SDK ships with the book "Microsoft Jet Database Engine Programmers Guide" from Microsoft Press

How Does DAO Licensing Work?

If your application's users have any of the following products installed on their workstations:

  • Microsoft Office 95
  • Visual C++
  • Microsoft Excel version 7
  • Microsoft Access version 7
  • Microsoft Visual Basic version 4 (32-bit)

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 DAO

DAO 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.

`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 Model

DAO 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:

The DAO Model

Objects and Collections

Most 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:

Collection!name or Collection![Name]


Collection(expression) where expression is a variable or other expression

Collection(index) where index is the number of the object's position within the collection.

Traversing collections

You can traverse or iterate through a DAO collection in one of two ways:

or, if you are using VBA, you can use

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 Variables

With 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:

  • Version 3 of DAO allows you to close a Workspace object, but it closes all of that workspace's child objects.
  • If you close the current database in Access using the DBEngine(0)(0) syntax, any other DAO variables you have under DBEngine(0)(0) will also be closed.

There are other internal idiosyncracies that are documented in the README.TXT file that ships with Access 95 and VB4.


[Dividing Line Image]

Object Creation and Modification

DAO allows you to create new objects, and modify existing ones. This section outlines the steps necessary to accomplish this.

Creating Objects

To create an object, the general steps are:

  1. Use the appropriate Create method
  2. Set properties
  3. Create child objects
  4. Set child object properties
  5. Append it to the collection

The rest of this section shows the SQL and DAO ways to create objects.

Creating a Database

DAO:Dim dbNew As DatabaseSet dbNew = CreateDatabase("C:/MYTEST.MDB, dbLangGeneral, dbVersion30)


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.Close

SQL:Dim dbsTmp As DatabaseSet dbsTmp = OpenDatabase("C:/MYTEST.MDB")dbsTmp.Execut ("CREATE TABLE MyTable (MyField Text);")dbsTmp.Close

Setting Field Properties



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 Objects

Unlike 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 Objects

Some 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:

  1. Create a new table
  2. Clone the source table's structure to the new table
  3. Change the data type of the desired field while creating the new table
  4. Run an Append query that copies the data from the source table to the new table
  5. Delete the source table and rename the new table to the source table's name

Types of Properties

When 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 Properties

These 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 Properties

These 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.


[Dividing Line Image]

Working with Data

There are several advanced topics regarding data access using DAO. This section covers these topics.

Choosing the Right Recordset

Microsoft 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:


This 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.


This 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.


The 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 Options

There are variety of options you can use when working with Dynaset and Snapshot recordsets. The following table shows these options:


Applies To


dbAppendOnly Dynaset You can specify this option to allow only the addition of new records. Because Jet can dispense with certain internal routines, this option allows a more efficient and fast recordset if all you want is to add data.
dbSeeChanges Table, Dynaset If you invoke the Edit method on a record and another user changes data in the record, but before the Update method was invoked, a run-time error will occur. This is helpful if multiple users have simultaneous read/write access to the same data.
dbDenyWrite Dynaset, Snapshot Prevents other users from modifying data in any of the tables that are included in the recordset.
dbDenyRead Table Prevents other users from reading data from the table
dbReadOnly All Prevents you from making changes in the recordset's underlying tables (implied on a snapshot)
dbForwardOnly Snapshot Creates a snapshot that you can only move forward through. This allows Jet to dispense with the creation and maintenance of internal buffers and structures, resulting in a more efficient recordset with faster access.
dbSQLPassThrough Dynaset, Snapshot Use this option to pass a SQL string directly to an ODBC database for processing. Jet does no internal processing of the query.
dbConsistent Dynaset Allows only consistent updates*
dbInconsistent Dynaset Allows inconsistent updates*

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 Transactions

Microsoft 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 Transactions

Transactions 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 Database

Database 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.


[Dividing Line Image]

Developing an Object Dictionary

As 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 Version

First, 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 Way

Now 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 Reader

You 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.


[Dividing Line Image]

Semi-Documented Tools and Resources

There 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 Browsers

Since 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:

  • OLE2View, from Microsoft which ships as part of Visual C++
  • VBA Companion, from Apex Software


Microsoft 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:

StatNum Description
0 Number of disk reads
1 Number of disk writes
2 Number of reads from cache
3 Number of reads from read-ahead cache
4 Number of locks placed
5 Number of release lock calls

See the included add-in (JETMETER.EXE) for an example of how to use this function.


Microsoft 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).


You 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.

Advanced Programming in UNIX Environment Episode 3

APUE Episode 3
  • myfather103
  • myfather103
  • 2017年09月10日 20:06
  • 101

关于APUE(Advanced Programming in the Unix Environment)第三版 代码在ubuntu中的编译运行

1. 下载代码:http://www.apuebook.com/code3e.html 2. 安装依赖库:sudo apt-get install libbsd-dev 3. 进入下载目录m...
  • hellochenlu
  • hellochenlu
  • 2016年01月09日 18:38
  • 770

《Advanced PHP Programming》读书笔记

  • 21aspnet
  • 21aspnet
  • 2011年12月12日 00:34
  • 3356


Mac.OS.X.Programming  Advanced.Mac.OS.X.Programming.The.Big.Nerd.Ranch.Guide,.Mark.Dalrymple,.B...
  • Cloud_Strife_1985
  • Cloud_Strife_1985
  • 2014年01月29日 20:19
  • 804

动态规划经典文章:Dynamic Programming: From novice to advanced

来源:http://blog.csdn.net/doc_sgl/article/details/9026405 Topcoder原文:http://community.topcoder.com/tc?...
  • NOI2015
  • NOI2015
  • 2015年03月15日 17:07
  • 423

读书笔记之:Advanced Linux Programming

lyjinger读书笔记之:Advanced Linux Programming原书信息:名称:Advanced Linux Programming版本:FIRST EDITION: June, 20...
  • lyjinger
  • lyjinger
  • 2007年05月29日 17:49
  • 1395


由于游戏引擎涉及到方方面面,这个列表得继续完善,暂时发布个beta版。还有国人写的就基本不要看了。。。。暂时没收录声音相关。 主要目录来自这里:http://www.gamedev.net/page...
  • rongyongfeikai2
  • rongyongfeikai2
  • 2012年11月20日 08:59
  • 2125

GPU Programming in MATLAB

By Jill Reese, MathWorks and Sarah Zaranek, MathWorks View PDF Multicore machine...
  • cuoqu
  • cuoqu
  • 2013年11月24日 14:27
  • 4917

Mac OS X Programming读书笔记1 - 系统架构

Chapter 1 System Components and Programming Technologies1.    Mac OS X IntroductionMac OS X是Apple最新一...
  • ATField
  • ATField
  • 2007年03月01日 21:22
  • 7265


  • chszs
  • chszs
  • 2016年04月19日 21:08
  • 3455
您举报文章:DAO Advanced Programming