Microsoft KB Archive/152400

= How to Use DAO in Excel Without Referencing DAO Library =

Article ID: 152400

Article Last Modified on 10/10/2006

-

APPLIES TO


 * Microsoft Excel 97 Standard Edition
 * Microsoft Excel 95 Standard Edition

-



This article was previously published under Q152400



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. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers 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 requirements. 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

