XL: How to Use DAO in Excel Without Referencing DAO Library

ID: Q152400

The information in this article applies to:

SYMPTOMS

In Microsoft Excel, if you attempt to use data access objects (DAO) without first referencing the Microsoft DAO 3.0 or 3.5 Object Library, you may receive an error message. This can cause difficulties when you are developing applications for distribution.

WORKAROUND

Instead of referencing the Microsoft DAO 3.0 or 3.5 Object Library, you can use object linking and embedding (OLE) to create a database engine object. You can then use the database engine object in references to the database. In this way, you do not have to create a reference to the Microsoft DAO 3.0 or 3.5 Object Library file.

This method does have a limitation. You can only declare your database variables as the generic Object type. For example, the statement

   Dim Db as Database

would generate the "User-defined type not defined" error. However, the following statement does not:

   Dim Db as Object

Visual Basic Code Example

Microsoft provides programming examples for illustration only, without warranty either expressed or implied, including, but not limited to, the implied warranties of merchantability and/or fitness for a particular purpose. This article assumes that you are familiar with the programming language being demonstrated and the tools used to create and debug procedures. Microsoft support professionals can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific needs. If you have limited programming experience, you may want to contact the Microsoft fee-based consulting line at (800) 936-5200. For more information about the support options available from Microsoft, please see the following page on the World Wide Web:

   http://www.microsoft.com/support/supportnet/refguide/

The following code example shows how to declare and use the database engine. The example assumes you have the Northwind.mdb sample database installed in c:\MSOffice\Access\Samples.

   Sub DaoWithoutReferences()

       'Declare variables.
       Dim dbEng As Object
       Dim Db As Object
       Dim Rs As Object

       'Set the dbEng object using OLE
       Set dbEng = CreateObject("DAO.DBEngine")

       'NOTE: In Microsoft Excel 97, use this line of code instead of the
       'above line of code:
       '
       '   Set dbEng = CreateObject("DAO.DBEngine.35")

       'Open a database. Note that the statement contains the dbEng object.
       Set Db = _
           dbEng.workspaces(0).opendatabase("c:\MSOffice\Access\" & _
           "Samples\Northwind.mdb")

       'Open a recordset in the database.
       Set Rs = Db.openrecordset("Customers")

       'Perform a move last and find the number of records
       'in the database to test if the operation worked.
       Rs.movelast
       MsgBox Rs.recordcount

       Set Rs = Nothing
       Set Db = Nothing
       Set dbEng = Nothing

   End Sub

REFERENCES

For more information about creating a DAO reference in Microsoft Excel 97, from the Visual Basic Editor, click the Office Assistant, type "Dao reference," click Search, and then click to view "What you need to create Visual Basic macros that retrieve external data."

NOTE: If the Assistant is hidden, click the Office Assistant button on the Standard toolbar. If the Assistant is not able to answer your query, please see the following article in the Microsoft Knowledge Base:

   ARTICLE-ID: Q176476
   TITLE     : OFF: Office Assistant Not Answering Visual Basic Questions

"Developing Microsoft Excel 95 Solutions with Visual Basic for Applications," Chapter 7, "Database Access and Messaging", page 489.

DAO Reference library, "DBEngine Object"

For more information about DAO, establish a reference to the "Microsoft DAO 3.0 Object Library". Then on the View menu, click Object Browser. Under Libraries/Workbooks, select DAO, and under Objects/Modules, click DBEngine.

Additional query words: 7.00 97 XL97 XL7

Keywords          : kbprg kbdta KbVBA 
Version           : WINDOWS:7.0,7.0a,97
Platform          : WINDOWS

Last Reviewed: May 18, 1999