[ Team LiB ] |
Recipe 12.2 Print an Access Report from Excel12.2.1 ProblemYou keep and work with your data in Excel, but you'd like to print reports using Access. You know you can use the Access Report Wizard directly from Excel, but you'd like more control over the process. Can you do this using VBA? 12.2.2 SolutionAccess allows you to control its actions using Automation. Anything you can do directly from Access, you can also do from Excel. This solution uses Automation to link your Excel worksheet to an Access database, use that data as the data source for a report, and then remove the linked table. Because you can directly link to an Excel worksheet from Access, this process doesn't need to involve importing the data—you can use it as-is, live, in your Excel environment. To try out the sample database, first load 12-02.XLS into Excel. This workbook includes the data (shown in Figure 12-3) and the VBA code that controls the sample. Next, click the Open Access Report button, which causes Excel to load a copy of Access and then load 12-02.MDB, link the current data to that database, and display the report in print preview mode. Figure 12-3. Use data in Excel to print a report in AccessTo use this technique in your own applications, follow these steps:
Figure 12-4. Use the References dialog to set a reference to Access in Excel
12.2.3 DiscussionThis example uses Automation to control Access directly from Excel. The process of printing the report can be broken down into four steps:
The next few paragraphs discuss these items. The HandleAccessReport procedure in Step 2 includes all the code for this process. To retrieve a reference to Access, you can use the Access Application object. The line of code that does the work looks like this: Dim accApp As Access.Application Set accApp = New Access.Application To open the database, use the OpenCurrentDatabase method of the Application object: With AccApp .OpenCurrentDatabase filepath:=strDatabase, Exclusive:=True Access provides three methods that work with the current database from Automation:
In addition to these three methods, the Access Application object provides two useful properties: UserControl and Visible. The UserControl property returns True if you opened Access under your own power, or False if Automation started Access. The property is read-only and lets your code work differently depending on how the database was loaded. The Visible property allows you to control whether an instance of Access started via Automation is visible or not. If UserControl is True, you cannot change the Visible property. If UserControl is False, the default value for Visible is False, but you can set it to be True with code like this: ' Set the Application's Visible property to True ' if OLE Automation initiated the session. With accApp If Not .UserControl Then .Visible = True End If End With To link the Excel spreadsheet to the Access database, use the TransferSpreadsheet method of the DoCmd object. This method allows you to import or link a spreadsheet to the database, depending on the parameters you set. In this example, the code specifies that the spreadsheet is of type acSpreadsheetTypeExcel9 (this applies to Excel 2000 and later), includes field names in the top row, and is to be linked, not imported: With .DoCmd .TransferSpreadsheet _ TransferType:=acLink, _ SpreadsheetType:=acSpreadsheetTypeExcel9, _ TableName:=conTableName, _ Filename:=strXLS, _ HasFieldNames:=True Once you've executed the TransferSpreadsheet method, your database will include an attached table, with the name stored in strTableName, that retrieves data from the spreadsheet whose name is in strXLS. To print the report, use the OpenReport method of the DoCmd object, as shown in the following code fragment, which opens the report in print preview mode using the acViewPreview constant: .OpenReport conReportName, acViewPreview If you want the report to be sent directly to the printer, use the acViewNormal constant. To clean up once your report has finished printing, the code first deletes the linked table, then closes the database, and finally shuts down the instance of Access that it initiated. To delete the table, it uses the DeleteObject method of the DoCmd object. To close the current database, it uses the CloseCurrentDatabase method of the Application object. Finally, to shut down Access, it uses the Quit method of the Application object. The cleanup code is: With DoCmd ' Do all the work here... .DeleteObject acTable, strTableName End With ' This isn't necessary, but it's neat. .CloseCurrentDatabase ' Quit Access now. .Quit End With Set obj = Nothing You aren't limited to running Access from Excel—you can have any Automation client (including Access itself) start up a new copy of Access to accomplish Access tasks from that host. |
[ Team LiB ] |