Connecting to a Database

   

Chapter 7, "VBA Essentials Reviewed," mentioned that there are differences between Data Access Objects (DAO) and ActiveX Data Objects (ADO). In some areas, the two are identical at least from the perspective of writing the code that uses the objects. For example, one way to move from one record to the next is

 EmployeeRecords.MoveNext 

You can use that command whether you've established your connection to the database with DAO or with ADO.

In other areas, the two object libraries couldn't be more different. The older DAO approach involves a strict hierarchy and your code has to observe it. A typical DAO sequence of events is as follows:

  1. Set an object variable, such as TheDB, that represents a database.

  2. Set another object variable, such as TheQuery, that represents a query or table in TheDB.

  3. Set yet another object variable, such as TheRecords, that represents the records and fields in TheQuery. (Both DAO and ADO term this a recordset.)

Even if you didn't set each of these explicitly as an object variable, DAO would insist that you conform to its hierarchy: refer first to the database, and then to a table or query in the database, and then to the records in the table or query.

You might take the same approach using ADO, but ADO is much more flexible. Objects in ADO still belong to other objects, but ADO is comparatively lenient as to when and where objects, properties, and methods are used. For example, using ADO, you could declare and create a recordset, complete with fields and records, before even mentioning a database to contain the recordset.

The act of connecting to a database, the topic of this chapter, is one area where the differences between ADO and DAO are most pronounced. ADO is discussed next in "Connecting Using ADO"; if you do not have access to ADO, or prefer to use DAO, you'll find the information you need in "Opening a Database Using DAO."



Managing Data with Microsoft Excel
Managing Data with Microsoft Excel
ISBN: 789731002
EAN: N/A
Year: 2003
Pages: 134

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net