How to Use DAO in Excel Without Referencing DAO Library (152400)
The information in this article applies to:
- Microsoft Excel 97 for Windows
- Microsoft Excel for Windows 95
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 ExampleMicrosoft 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
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:
176476
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.
Modification Type: | Minor | Last Reviewed: | 10/10/2006 |
---|
Keywords: | kbProgramming KB152400 |
---|
|