Often when you need two applications to cooperate most, compatibility problems emerge. Microsoft Access supports Extensible Markup Language (XML), which makes it easier to import and export data and schema information. Here's how it works.
When it comes to sharing, applications often act more like squabbling children than professional tools of the trade. Sometimes the solution is to find a third application that's compatible to both formats. Unfortunately, there isn’t always a compatible format available. The good news is that Microsoft Access supports Extensible Markup Language (XML), and XML almost effortlessly exports and imports data and schema information. That equates to a lot less work for you.
XML and Access
Access exports several XML file types. The three XML export options produce the following XML file types:
- Data (XML) produces an XML file.
- Schema of the Data produces an XSD file.
- Presentation of Your Data produces three files: an XML, an XSL, and an HTML file.
XML can share data, information on presenting that data, and schemas. In a relational database, schema refers to the tables. Specifically, schema identifies the fields and the relationships between fields and tables. Importing a schema can make creating a replica of a table much easier. In fact, you could quickly re-create the entire table structure using schema data.
You can export data or schema using the Access user interface or Visual Basic for Applications (VBA) code. To export schema manually:
- Select a table in the Database window—for this example, we'll export the Customers (a table in Northwind, the sample database that comes with Access) schema.
- Select Export from the File menu.
- Name the XML file CustomersSchema.
- Select XML as the file type in the Save As Type control. Don't type the XSD extension; XML will assign the right extension.
- Click Export and Access will display three export options, as shown in Figure A: Data (XML), Schema Of The Data, and Presentation Of Your Data. The first two options are selected by default.
- Choose Schema Of The Data; you're really just deselecting Data (XML).
- Click the Advanced button. (You can omit details from the exported information, as shown in Figure B.) Don't make any changes right now, but you should know that you can omit primary key and index information from the exported file.
- Click the Data tab and click OK.
|Choose the Schema Of The Data option to export schema to an XML file.|
|The Advanced options let you omit details from the exported information.|
You can learn a lot about an XSD schema file by viewing it in a browser. Figure C shows CustomerSchema.xsd in Word. The file is really just text that contains details about the Customers table structure.
|Open the XSD file in Word to see what it contains.|
Automating the Process with ExportXML
While it's easy to export schema manually, most likely you'll want to automate the process. To do so, use the ExportXML function in this form:
Application.ExportXML(objecttype, datasource[, datatarget][,
schematarget][, presentationtarget][, imagetarget][, encoding][,
All but the first two arguments are optional, but you'll need to include at least one of the target arguments. Table A defines the arguments. Table B lists constants for the objecttype argument. Table C lists values used by the otherflags argument.
The simple procedure in Listing A exports Access schema to an XSD file. Simply pass the procedure the necessary information—the name of the table you're exporting, the path (including the drive specification) that identifies where you want to store the exported file, and a name for the newly exported file—and the ExportXML function does the rest. The only glitch in the process is that you must identify the file as an XSD file in the schematarget argument. That's why the following line concatenates the XSD extension onto the path and target filename:
target = path & target & ".xsd"
Keep in mind that ExportXML will write over existing files (except when exporting a data access page object). The procedure doesn't include any error-handling, so you'll want to account for that possibility if you don't want to delete existing files. If the path you specify isn't a valid path, VBA will return error 2950.
After entering the procedure into a standard module, execute it once from the Immediate window to see how it works. For instance, if you wanted to export schema for the Customers table to a folder named Temp on your local system, your statement might resemble the following, where CustomersSchema is the name of the newly exported XSD file:
Be sure to include the final backslash character (\) in the path argument. You could also include error-handling to check for that character and add it if missing.
If the procedure is successful, it returns the value True. The resulting file will be exactly like the one you saw in Figure B.
Schema to go
Schema information is full of details about a table's structure and you might think exporting that information is difficult. To the contrary, using XML, you can quickly export the information to an XSD file—ready to import in any XML supporting application.
Susan Sales Harkins is an independent consultant and the author of several articles and books on database and Web technologies. Her most recent books are SQL: Access to SQL Server by Apress, Mastering Dreamweaver MX Databases by Sybex, and Absolute Beginner's Guide to Microsoft Access 2002 by Que.