Integrated data management is the key to Access's success in the desktop RDBMS and client/server front-end market. Access and its wizards let you create basic data-bound forms, reports, and pages with minimal effort and little or no VBA programming. Linked tables provide dynamic access to a wide range of data sources. As your Access applications grow larger and more complex, automation with VBA code in class and public modules becomes essential. When networked Access applications gain more users, performance may suffer as a result of Jet record-locking issues or multiple connections to client/server back ends. Decreasing performance with increasing user load is a symptom of lack of scalability. Achieving scalability requires VBA code to manage your application's database connections. This advanced chapter shows you how to write the VBA code that's required to improve the scalability of Access front ends. You also learn how to use the Stream object to generate XML data documents from SQL Server 2000's FOR XML AUTO queries. Access 2003 continues Microsoft's emphasis on "Universal Data Access" for VBA and Visual Basic 6.0 programmers. Microsoft wants Access developers to abandon Jet's Data Access Objects (DAO), Access 97's ODBCDirect, and the venerable Open Database Connectivity (ODBC) Application Programming Interface (API) in favor of a collection of Component Object Model (COM) interfaces called OLE DB and ActiveX Data Objects (ADO). To encourage Access power users and developers to adopt OLE DB and ADO, all traditional Microsoft database technologies (referred to by Microsoft as downlevel or legacy, synonyms for "obsolete") are destined for maintenance mode. Maintenance mode is a technological purgatory in which Microsoft fixes only the worst bugs and upgrades occur infrequently, if ever. In 1999, OLE DB, ADO, and, for Jet programmers, ActiveX Data Object Extensions (ADOX), became Microsoft's mainstream data access technologies. Microsoft's primary goals for Universal Data Access were to
This chapter introduces you to the fundamentals of Universal Data Access and Microsoft Data Access Components (MDAC). MDAC makes connecting to databases with OLE DB practical for Access users and developers. MDAC includes ADO and ADOX for conventional relational data, plus ADOMD for multidimensional expressions (MDX) to create and manipulate data cubes. Note Microsoft SQL Server Analysis Services (formerly OLAP Services) generates data cubes from online sources, such as transactional databases. Office 2003 installs Msadomd.dll and other supporting files for MDX and data cubes. Microsoft provides OLE DB for OLAP and the PivotTable Service to enable Excel 2003 PivotTables to manipulate data cubes. MDX and PivotTable services are beyond the scope of this book. Redesigning from the Bottom Up with OLE DBTo accommodate the widest variety of data sources, as well as to spread the gospel of COM and Windows XP/2000+'s COM+, Microsoft's data architects came up with a new approach to data connectivity OLE DB. OLE DB consists of three basic elements:
Figure 30.1 illustrates the relationship between OLE DB data providers, data consumers, and data services within Microsoft's Universal Data Access architecture. You should understand the relationships between these objects, because Microsoft commonly refers to them in ADO documentation, help files, and Knowledge Base articles. Database front ends written in C++ can connect directly to the OLE DB interfaces. High-level languages, such as VBA, use ADO as an intermediary to connect to OLE DB's COM interfaces. Msado15.dll, which implements ADO 2.x, has a memory footprint of about 327KB, about 60% of Dao360.dll's 547KB. Figure 30.1. This diagram shows the relationships between front-end applications, ADO and ADOX, and OLE DB service and data providers.
ADO support files install in your \Program Files\System\Ado folder. If you're running Windows XP/2000+, the ADO support files are subject to Windows File Protection (WFP), which places a copy of the file in the DLL cache and prevents you from permanently deleting or moving the ADO support files. WFP also prevents unruly installation programs from overwriting the ADO support files with an earlier or corrupt (hacked) version. Some ADO 2.x support file names have a 1.5 version number, as in Msado15.dll; the strange versioning of these files is required for backward compatibility with applications that used very early versions of ADO. Note MDAC 2.x also supports Remote Data Services (RDS, formerly Advanced Database Connector, or ADC). RDS handles lightweight ADOR.Recordsets for browser-based applications; RDS, which commonly is used for three-tier, Web-based applications, is required to make Data Access Pages (DAP) accessible safely over the Internet.
For more information on the use of RDS with DAP, see "Enabling Private or Public Internet Access," p. 1058. Mapping OLE DB Interfaces to ADOYou need to know the names and relationships of OLE DB interfaces to ADO objects, because Microsoft includes references to these interfaces in its technical and white papers on OLE DB and ADO. Figure 30.2 illustrates the correspondence between OLE DB interfaces and the highest levels of the ADO hierarchy. Figure 30.2. This diagram illustrates the correspondence between OLE DB interfaces and ADO Automation objects.
The OLE DB specification defines a set of interfaces to the following objects:
ADO maps the four OLE DB objects to the following three top-level Automation objects that are familiar to Access programmers who've used ODBCDirect:
The ADODB prefix, the short name of the ADO type library, explicitly identifies ADO objects that share object names with DAO (Recordset) and DAO's ODBCDirect (Connection and Recordset). For clarity, all ADO code examples in this book use the ADODB prefix. Tip To make ADOX 2.7 accessible to VBA, you must add a reference to Microsoft ADO Ext. 2.7 for DDL and Security to your application. Access 2003 doesn't add the ADOX reference automatically to new projects. Comparing ADO and DAO ObjectsFigure 30.3 is a diagram that compares the ADO and DAO object hierarchies. The ADO object hierarchy, which can consist of nothing more than an ADODB.Connection object, is much simpler than the collection-based object hierarchy of DAO. To obtain a scrollable, updatable Recordset (dynaset), you must open an ADODB.Recordset object on an active ADODB.Connection object. Figure 30.3. This diagram compares the ADO and DAO object hierarchies.
Access VBA provides a DAO shortcut, Set dbName = CurrentDB(), to bypass the first two collection layers and open the current database, but CurrentDB() isn't available in VBA code for other members of Office 2003 or Visual Basic 6.0. Note Access VBA provides a similar ADO shortcut, CurrentProject.Connection, which points to a default ADODB.Connection object with the Jet OLE DB Service Provider for the current database. Unlike CurrentDB(), which is optional, you must use CurrentProject.Connection as the ADODB.Connection to the currently open database. If you try to open a new ADODB.Connection to the current database, you receive a runtime error stating that the database is locked. Unlike DAO objects, most of which are members of collections, you use the New reserved word with the Set instruction to create and the Close method, the Set ObjectName = Nothing, or both statements to remove instances of ADODB.Connection, ADODB.Command, and ADODB.Recordset objects independently of one another. The Set ObjectName = Nothing instruction releases memory consumed by the object. DAO supports a variety of Jet collections, such as Users and Groups, and Jet SQL Data Definition Language (DDL) operations that ADO 2.7 alone doesn't handle. ADOX 2.7 defines Jet-specific collections and objects that aren't included in ADO 2.x. The "Provider-Specific Properties and Their Values" section later in the chapter describes how to roll your own cross-reference table to aid in migrating your DAO code to ADO. The most important functional difference between DAO and ADO is that ADO supports Web-based applications and DAO doesn't. Thus, DAP bind to ADODB.Recordset objects. The continuing trend toward Internet-enabling everything means that Windows database programmers must make the transition from ODBC, ODBCDirect, RDO, and DAO technologies to ADO and OLE DB, so this book covers VBA programming of ADO, not DAO, objects. ADO supports ODBC connections to shared-file and client/server RDBMSs with the Microsoft OLE DB Provider for ODBC (more commonly called by its beta code name, Kagera). ODBC introduces another layer into the database connection, so it's less efficient than OLE DB. The examples of this chapter use only native OLE DB providers. Upgrading from ADO 2.5 and Earlier to Version 2.6+ADO 2.x in this chapter refers collectively to ADO 2.1, 2.5, 2.6, and 2.7. Windows XP and Office 2003 install ADO 2.7, which includes type libraries for ADO 2.0, 2.1, 2.5, 2.6 for backward compatibility. Windows 2000 Service Pack (SP) 1 or later installs ADO 2.5 SP1, which includes type libraries for for prior versions. Installing the SQL Server 2000 Desktop Engine (MSDE2000) from the Office 2003 distribution CD-ROM or any other version of SQL Server 2000 upgrades Windows 2000's ADO 2.5 to 2.6. Version 2.7 is required only to support Intel's 64-bit Itanium processors. Upgrading from ADO 2.6 to 2.7 doesn't add new features or alter existing features. Note
To review use of the VBA editor's References dialog, see "References to VBA and Access Modules," p. 1157.
Tip If you're interested in learning more about ADOX, open the VBA Editor, type adox in the Ask a Question text box, select the ADOX methods option, click See Also in the "ADOX Methods" help page, and select ADOX API Reference in the list. |