Exporting Access Data to Web Formats


As you learned in Chapter 7, you have various ways to export data from Access to other formats. The OutputTo method allows exporting to various formats: HTML, RTF, TXT, XLS, and XLSB. The examples in this section will focus on a Web-centric example using HTML, but you could also use the OutputTo method to export to text files or other supported formats. Consult the Access Help documentation for additional information on these formats. The basic syntax for the OutputTo method is shown in the following code:

 DoCmd.OutputTo ObjectType, ObjectName, OutputFormat, OutputFile, AutoStart, TemplateFile, Encoding

Various parameters can specify how the method should execute. The following table explains the use of each parameter.

Open table as spreadsheet

Parameter

Description

ObjectType

Type of object to be exported. See the help documentation for the complete list of objects.

ObjectName

Name of the object being exported.

OutputFormat

The format to which the object should be exported.

OutputFile

The full name, including the path, of the file to which the object should be exported.

AutoStart

Indication of whether the destination program is to start immediately. False is the default.

TemplateFile

The full name, including the path, of the file to use as a template.

Encoding

The type of character encoding format to use when outputting the text or HTML data.

OutputQuality

Type of output device to optimize for. The AcExportQuality constant specifies the type, and the default value is acExportQualityPrint.

Now that you are familiar with the basic syntax of the OutputTo method, let’s look at how to use it to export data to HTML.

Creating Static HTML Pages

You have no doubt heard of HTML pages and are probably aware that they are typically used to display Web pages. You can use the OutputTo method to export a database table, query, results of a SQL statement, or other data to a static HTML document. A static HTML document is disconnected from the original data source and does not change as the underlying data changes - hence the name static.

To export the results of a report to an HTML document, you might use a statement similar to the following:

  DoCmd.OutputTo acOutputReport, "ReportName", acFormatHTML, "c:\temp\tempReportName.html" 

Now, try it out so you can see how this works.

Try It Out-Exporting the tblComplaints Table to HTML

image from book

This example will export the data contained in the tblComplaints table you created earlier in the chapter to an HTML file.

  1. Add the following procedure to the module you created earlier:

      Sub OutputToHTML() DoCmd.OutputTo acOutputTable, "tblComplaints", acFormatHTML, "c:\temp\tempcomplaints.html", True End Sub 

  2. Run the OutputToHTML procedure from the Immediate Window by typing OutputToHTML and then pressing Enter.

  3. The exported HTML document should open automatically in a Web browser, as shown in Figure 9-6.

    image from book
    Figure 9-6

How It Works

The OutputToHTML procedure uses the OutputTo method with various parameters that indicate what should be exported, in what format, and to where. In this example, the tblComplaints table is exported to an HTML format into a new file called tempcomplaints.html. The True parameter at the end of the statement that follows is for the AutoStart parameter to indicate that the Web browser should automatically launch to display the file.

 Sub OutputToHTML() DoCmd.OutputTo acOutputTable, "tblComplaints", acFormatHTML, "c:\temp\tempcomplaints.html", True End Sub

After you run the preceding procedure, the Web browser does indeed launch and displays the newly created HTML file in a screen similar to Figure 9-6.

image from book




Beginning Access 2007 VBA
Beginning Access 2007 VBA
ISBN: 0470046848
EAN: 2147483647
Year: 2004
Pages: 143

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