Microsoft Office Online
Sign in to My Office Online (What's this?) | Sign in

Warning: You are viewing this page with an unsupported Web browser. This Web site works best with Microsoft Internet Explorer 6.0 or later, Firefox 1.5, or Netscape Navigator 8.0 or later. Learn more about supported browsers.

Email this linkEmail this link Printer-Friendly VersionPrinter-Friendly Version Bookmark and ShareShare
Including XML in data access pages in Access
 
Applies to
Microsoft Office Access 2003
Microsoft Access 2002

In Access, you can include Extensible Markup Language (XML) data in your data access page in one of three ways. You can link the page to an XML data file by taking the page offline in Microsoft Internet Explorer; you can set data access page properties inside Access to allow the page to bind to an external XML data file; or you can set page properties to embed the XML data directly inside the page. By using these methods, you can include read-only data in your data access page.

ShowIntroduction

A data access page is a special type of Web page designed for viewing and working with data from the Internet or from an intranet utilizing data that is stored in an Access database, Access project, or a Microsoft SQL Server database. The page may also include data from other sources, such as XML data files. The page is a separate .htm file that is stored outside Access. However, when you create the file, Access automatically adds a shortcut to the file in the Database window.

XML is the standard language for describing and delivering data on the Web, just as Hypertext Markup Language (HTML) is the standard language for creating and displaying Web pages. Access provides ways to both import and export XML data as well as transform the data to and from other formats using XML- related files. HTML, while well suited for providing text and image display information for Web browsers, is limited in its ability to define data and data structures. HTML describes how a Web page should look, whereas XML defines the data and describes how the data should be structured. XML separates the data from the presentation so that the same XML data can be presented in multiple ways by using different presentation files.

Typically, a data access page gets its data from an Access database or an Access project connected to a Microsoft SQL Serverâ„¢ version 6.5 or later database. The page is designed while connected to the database. If you have already opened an Access database or an Access project that's connected to a SQL Server database, the data access page you create automatically connects to the current database and stores that path in the ConnectionString property of the page. When a user views the page in Internet Explorer 5 or later, it displays current data from the underlying database by using the path defined in the ConnectionString property.

When you add XML to a data access page, the ConnectionString property is ignored and the data for the page is either located internal to the document or linked via an external file. Setting the UseXMLData property to False or the OfflineType property to dscOfflineNone (both explained later in this article) returns data source control to the ConnectionString property.

Adding XML data to offline data access pages

One of the easiest ways to include XML data in your data access page is to take the page offline by using offline caching in Internet Explorer 5 or later. When you view the page in Internet Explorer and choose to take the page offline, Internet Explorer disconnects the page from its original data source and, based on settings made in the pages property sheet, either relinks the offline page using read/write SQL Server 2000 Desktop Engine (previously called the Microsoft Data Engine [MSDE]) data or to a read-only XML data file. For this article, we will discuss pointing to read-only XML data.

ShowOffline caching and synchronization in Internet Explorer

When Microsoft Office 2000 or later is installed on a client computer, Internet Explorer 5 or later supports offline caching — also called replication. You can work offline on cached copies of published documents such as data access pages. When you go online again, the Synchronization Manager in Internet Explorer copies the locally modified document to the server and reconciles differences in the local and published copies.

For more information on offline caching and synchronization, search for these and related terms on the Microsoft Developer Network (MSDN) Web site.

ShowA typical scenario for using offline data access pages with XML

When a user is viewing a page in Internet Explorer and takes the page offline:

  • Internet Explorer automatically downloads the page to the user's computer, and, if a Channel Definition Format (CDF) file is defined, all other pages and HTML-related files are also downloaded. A CDF file allows users to subscribe to and have .htm files downloaded automatically to the desktop for offline viewing, among other features.
  • Assuming the OfflineType property of the page is set to dscOfflineXMLDataFile, an offline data handler in Access calls the ExportXML method to persist the XML to a file. The file name is specified by the page author by using the OfflineSource property in the page property sheet. If the page consists of two or more pages, or if the page contains additional controls or images, then the OfflineCDF property points to a CDF file which is linked to the page.

    The following example shows a sample CDF file and a LINK element HTML tag added to the page:

    --applicationfilename.cdf

    <?XML VERSION="1.0" ENCODING="UTF=8"?>

    <CHANNEL HREF="default.htm"
    PRECACHE="YES"
    LEVEL="0">
    <ITEM HREF="Products.htm"></ITEM>
    <ITEM HREF="Categories.htm"></ITEM>
    <ITEM HREF="CategoryDetail.htm"></ITEM>
    <ITEM HREF="Orders.htm"></ITEM>
    <ITEM HREF="OrderDetails.htm"></ITEM>
    <ITEM HREF="xmldata\Categories.XML"></ITEM>
    <ITEM HREF="bitmaps\CorpLogo.gif"></ITEM>
    <ITEM HREF="bitmaps\ShippedItem.gif"></ITEM>
    <ITEM HREF="bitmaps\BackorderedItem.gif"></ITEM>
    <ITEM HREF="bitmaps\Discontinued.gif"></ITEM>
    </CHANNEL>
    --HTML tag in data access page
    <LINK REL="Offline" HREF="/filename.cdf">

  • The data handler points the page to the XML data file. Again, the data file is read-only.

ShowSteps to take a data access page offline

To take a data access page offline, do the following:

  1. Open a data access page in Internet Explorer 5 or later.
  2. On the Favorites menu, click Add to Favorites.
  3. In the Add Favorite dialog box, select Make available offline.

     Note    If the Make available offline check box is unavailable, you won't be able to take a data access page offline. Contact the designer of the page for assistance.

  4. Click OK.

The page and its associated XML data are saved to your computer.

ShowEmbed or link XML data to your data access page

Other ways to include XML data in your data access page are to embed the XML data and tags inside the page or to refer to an external XML file from within the page. This is done through the use of "data islands" inserted inside the page. Data islands are created by inserting XML data between <XML> tags within the page. The information inside the tags is formatted in XML syntax.

Embedding XML data

In this technique, the actual XML data and tags are embedded in the body of the page between <XML> tags. To include the XML data and tags into a data island, you set the XMLLocation property of the page to dscXMLEmbedded and the UseXMLData property to True, and then call the ExportXML method to embed the XML. For example, if you wanted to embed the XML data once, as opposed to every time you open the page, you could call the ExportXML method from the Immediate Window (CTRL+G from within Access) in the Microsoft Visual Basic Editor and execute:

DataAccessPages(0).MSODSC.ExportXML

where:
DataAccessPages(0) refers to the current page,
and MSODSC refers to the Microsoft Office Data Source Control.

To embed the XML data every time you open the page, you could include the code in an event procedure for the page such as the OnOpen event of the page. The following is an example of XML data and tags embedded in an .htm page:

<HTML>

...

<BODY>
   <p>This is a segment of a data access page</p>
   <!-- The XML tag starts the data island -->
  <XML ID=OrderData> 
   <ORDERS>
    <ITEM ID=1> 
      <NAME>Aniseed Syrup</NAME> 
      <PRICE>10.95</PRICE>
     </ITEM>
   </ORDERS>
  </XML>
  </BODY>
 </HTML>



Linking to external XML data

In this technique, the <XML> tag is used again in the body of the document to separate the XML data from the presentation data. However, instead of including the XML data between the tags, an SRC attribute is used to point to an external XML data file on a Web server. To bind the page to an XML file, you set the XMLLocation property of the page to dscXMLDataFile, the UseXMLData property to True, and the XMLDataTarget property to the location of the XML data file. The following is an example of an .htm file with a link to an external XML file:

<HTML>
  ...
   <BODY>
    <p>This is a portion of a data access page</p>
    <!-- Again, the XML tag starts the data island
    which contains a link to an external XML file --> 
    <XML ID=xmlData1 SRC="orders.xml"></XML> 
   </BODY>
 </HTML>



Notice that in this case, the SRC attribute points to a static data file. The XML data island could also point to an ASP file that generates XML output data such as:

<XML ID=xmlData2 SRC="getorders.asp"></XML>

You can also dynamically change the SRC attribute of XML data islands by using script, as in the following example:

<HTML>
  ...
   <BODY>
   <SCRIPT LANGUAGE=javascript>
    function getXML(id) {
    xmlData1.src = "getorders.asp?userid=" + id;
    }
    </SCRIPT>
    <p>This is a portion of a data access page</p>
    <XML ID=xmlData1"></XML>
    <INPUT TYPE=button VALUE=User1 ONCLICK="return
 getXML(1)">
    <INPUT TYPE=button VALUE=User2 ONCLICK="return
 getXML(2)">
   </BODY>
 </HTML>



In this example, the user presses a button which determines which set of XML data is returned. If the user presses button btn_User1, the userid "1" is passed to the function getXML which, in turn, calls the getorders.asp and returns the set of data for "User 1", and so forth.

Conclusion

XML has become the preferred method of exchanging data between applications and across Web pages. By including XML data in your data access pages, you make the data in your pages available to more applications.

For more information about programming in Access, visit the Office Developer Center on the Microsoft Developer Network (MSDN).

© 2009 Microsoft Corporation. All rights reserved.