Driving Access from other Office applications is about as easy as manipulating Access objects from within Access itself. This chapter discusses how to drive Access from Project. To manipulate Access objects from any other application, you need to understand the Data Access Objects model (see Figure 13.13). When working with Access objects such as reports and forms, you will want to have a reference to the Microsoft Access 9.0 Objects Library. When you work with tables and queries, depending on whether you are using DAO or ADO, you will have a reference to either the Microsoft DAO 3.6 Object Library for DAO or one or more of the three libraries for ADO. For a list of these libraries, see Chapter 6, “Introducing ActiveX Data Objects.”
The AutomateToAccess routine is located in a Microsoft Project project called Access.mpp, which can be found on the CD-ROM in the ExamplesChap13 folder. This routine performs the following steps:
1. |
It opens the database where you want to create the table—for this example, Chap13.mdb. This table, tblProjects, will be used to hold information about a project. |
2. |
AutomateToAccess calls the CreateProjectTable routine, which creates the Projects table; then AutomateToAccess appends the Projects table to the database's TableDefs collection. |
3. |
The AutomateToAccess subroutine opens the Projects table in Access and cycles through each task, assigning the Project-specific fields. |
4. |
The subroutine closes the recordsets. |
Listing 13.6 shows the actual code that performs these tasks.
Note
The ! (bang) character is used with the Access objects. Not all Office applications use the bang character for referencing objects.
The following line of code uses the OpenDatabase method from Access combined with the object ActiveProject's Path property used in Project:
Set dbProjDemo = OpenDatabase(ActiveProject.Path & "Chap13.mdb")
This line opens an Access database from within Project. (Is that power, or what?)
To run this code in the project itself, open Access.mpp in Microsoft Project, and you see the message shown in Figure 13.14.
To examine the code for the AutomateToAccess subroutine, choose Macros from the Tools menu. You can then select the AutomateToAccess macro and click Edit.
One last thing to look at before moving on is the CreateProjectTable subroutine. Just by looking at Listing 13.7, you can't tell whether it was written in Access or Project (for the record, it was written in Project). That's one of the beauties of working with VBA.
Figure 13.15 shows the tblProjects table created by the Access.mpp routines.
Going from other Office products to Access is as easy as going from Access to others. It's just a matter of knowing which Data Access Objects to use, as well as which objects and methods to use with VBA. Now it's time to check out how to drive one of the newest members of the Office products—Outlook—from Access.
18.222.196.175