XL: How to Use DAO in Excel Without Referencing DAO LibraryLast reviewed: February 27, 1998Article ID: Q152400 |
The information in this article applies to:
SYMPTOMSIn 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.
WORKAROUNDInstead 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 Databasewould generate the "User-defined type not defined" error. However, the following statement does not:
Dim Db as Object Visual Basic Code ExampleThe 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. Microsoft provides examples of Visual Basic for Applications procedures 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. The Visual Basic procedures in this article are provided 'as is' and Microsoft does not guarantee that they can be used in all situations. While Microsoft AnswerPoint Engineers can help explain the functionality of a particular macro, they will not modify these examples to provide added functionality, nor will they help you construct macros to meet your specific needs. If you have limited programming experience, you may want to consult one of the Microsoft Solution Providers. Solution Providers offer a wide range of fee-based services, including creating custom macros. For more information about Microsoft Solution Providers, call Microsoft Customer Information Service at (800) 426-9400.
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"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
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |