Online Book Reader

Home Category

Access Cookbook - Ken Getz [334]

By Root 1925 0
using an XSLT transform. Using a transform has the added benefit of allowing you to format the data as HTML. Follow these steps to export only the Make and Model data from the Car table and to format the data as an HTML table:

Select the Car table in the database window, right-click and select Export, and choose XML in the Save as type drop-down list at the bottom of the dialog box.

Type a name for the XML file ending with an htm suffix and click the Export button. This example assumes that the output file is named Cars.htm.

Click the More Options button to load the Export XML dialog box shown in Figure 18-15. You can change the output file name here if you didn't change it in the previous dialog box.

Figure 18-15. Selecting the output location and filename

Click the Transforms button. If the transform doesn't show up in the list, click the Add button to browse to it. This example uses a transform named 18-05.xsl. Click OK and OK again. Access will create a Car.htm and a Car.xsd file in the destination directory.

Discussion


The 18-05.xsl file used to transform the data contains two templates. The dataroot template contains code for creating an HTML document with an HTML table. The Car template creates the rows in the HTML table and cells containing only the Make and Model data. This transform works against a hidden XML document that is created from all the data in the table:

xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" >

When you view Car.htm in a browser, you can see that the data is displayed in an HTML table, as shown in Figure 18-16.

Figure 18-16. The output generated by the XSL transform when viewed in a browser

Choose View > Source from the menu and you'll see the following HTML:

Mini CooperS
LexusLS430
PorscheBoxter
FordMustang
ToyotaCamry

This example is very simple and creates just a bare-bones table. You can modify the HTML sections of the XSLT to specify colors, borders, fonts, and so on to create whatever custom formatting you need.

See Also


The following W3C page contains links to many resources on XSLT:

http://www.w3.org/Style/XSL/

18.6. Export Unrelated Tables


Problem


You want to export Access data to a single XML file, but the tables you wish to export are not related to each other. How do you select tables and create a single XML output file?

Solution


You must write VBA code to export multiple unrelated tables to a single XML file. The Access object model provides the ExportXML method, which has an AdditionalData parameter that takes an object of type AdditionalData.

The 18-06.MDB sample database contains three unrelated tables: Car, Customer, and Dealer. There is a single module, basExport, which contains the function ExportUnrelated. The code creates an AdditionalData object, and adds the Customer and Dealer tables to it. The ExportXML method uses the Car table as the DataSource, and uses the AdditionalData object to add the Customer and Dealer data to the output:

Dim adTables As AdditionalData

Set adTables = Application.CreateAdditionalData

adTables.Add "Customer"

adTables.Add "Dealer"

Application.ExportXML _

ObjectType:=acExportTable, _

DataSource:="Car", _

DataTarget:="c:\test\Unrelated.xml", _

AdditionalData:=adTables

Figure 18-17 shows the XML file in a browser, with only one element from each table expanded. All of the data from all of the tables has been exported.

Figure

Return Main Page Previous Page Next Page

®Online Book Reader