So one of the little tricks that I have deployed ever since my early days as an Informix developer, was to include a some sort of software to export all the tables in the database to text files. For these databases, this was really only a 90% effort as the comma delimited files that I used for storing the data had no intelligence about the structure of the data that was being exported. Also in Access, the export to comma delimited format can be a little problematic as it can round decimal data into integer data upon exporting in some situations. Another really good thing that comes of exporting your data to text is that at a later stage (say 5 years) down the track, when you finally realize that you actually needed that data after all, guess what you have no software that will read it. Exporting to text files means that in all likelihood, you still will be able to read the files in 20 years time. Thatís as long as CD-ROMs and tapes are still readable. And if you think that is unlikely, one project that I was involved had a database of geological data that cost 50 million dollars to put together. Another company bought the project and did nothing with it for many years and in the end couldnít read the backup tapes. Our company recovered the text data backups from our tapes and the software that could read the binary copies of the database was long gone.
But the here and now is that this article describes a self contained Access form that you can readily import into your database. This form will then export every table in the database into XML text files in a subdirectory. Thanks to the self contained nature of these XML files, these text files not only will hold all the data but will also describe adequately the structure of the data tables themselves. This then will provide a more robust text format for storing your important information.
The Download Database Ė Getting Linked
Note: This download database is only available when you purchase The Toolshed
The accompanying database called ExportToXML2000.mdb has two forms inside it that you can import into your own application. In this application, I have linked to all the tables in the Access 2000 Northwind database. To help you to re-linking to your location of Northwind, I have included a form that I utilise in most of my new Access front-end databases. This form has the instructions for re-linking and a button on it fires up the table linker add-in that is supplied with Access using the sendkeys command as follows
For Access 97, the command is
Exporting All Tables To XML format
All the source code that you need for backing up your database to XML is included in the form called "frmUnloadToXML". Figure 1 shows the form in action.
To run this form is pretty simple, click on the "unload to XML Now" button and all the tables in the database will be exported to a subdirectory under your software database location called Backup. The hardest bit about installing this form in your database is that you will probably need to add 2 references to your Access project. The references that you will need are
Microsoft Active X Data Object 2.5 Library
Microsoft ADO Extensions 2.5 for DDL and Security
Figure 1. A Form To Export All Linked Northwind tables.
Why Backup Your Tables Using An XML File
To refresh your memory of what the exported XML file looks like, see the following extracts of the XML export of the Northwind Orders table. At this stage let me inform you that I am not an XML expert. All I have learnt so far is how easy it is to use ADO to save a table as an XML file. And here is the first important bit of the XML file as far as the text backups go. The following XML tells us that field 4 in the recordset is called OrderDate and that it is a date field that allows null values. Now that is a lot more information than a fixed width or comma delimited file will give ever give you.
Now we move on down to the data section of the XML file and here you will find a row of data from the table is described using z:row tag. There are two things to note here. The first is that this file is quite easy to read and the second is that this would have been quite hard to write using your own software. But that is where the ADO helps out.
So in summary, an XML file provides a far more comprehensive description of your data than either fixed width or comma delimited text files. Consequently this a more robust format to use to backup your data externally from your database.
The Export All Tables To XML Function
The software that is demonstrated has been written in Access 2000. To convert it to Access 97, you would need to provided your own connection string for the current database. You would also have to be quite careful with your recordset visual basic and the priority of your external libraries when mixing DAO and ADO libraries. To emphasize this, the following code snippet illustrates the data definitions for code behind this form. For example , recordset is declared as "Dim Rst ADODB.recordset" rather than "Dim Rst as Recordset". If you do not include the complete declarations of these variable types, you are going to run into problems as the DAO and ADO libraries deploy objects with the same names. Thereafter, if you use a shared name such as recordset, the library that is highest in the priority order that uses that object name will be used first. This may not be the library that you were thinking about and debugging the problems is very painful.
Dim cboStr As String, cancel As Integer
strConnect = CurrentProject.Connection
Note that when programming using ADO in an Access database, I will sometimes get an error message saying the "Database has been placed in a state by user Admin that prevents it from being opened or locked." To fix this during development, I find that I have to compact or close the database down and open it again. If anyone knows of a better way to handle this, please let me know.
adoxCat.ActiveConnection = strConnect
Now the software loops through all the Tables and in the process ignores both the system tables and queries which we do not need to export for backups. Note that ADO catalog collection labels all non action queries in an Access database ( eg "select * from table") as Views and bundles them up in the ADOX tables catalog. Therefore we need to skip the all the Views
For Each objT In adoxCat.Tables
' Before adding the table to combo box string
make sure that the
if objT.Type <> "VIEW" Then
Now its time to utilize the File System Object to generate a sub directory folder called Backup under the directory where the Access front-end database exists. To find the current directory, I employ a great little function called GetDBPath that came courtesy of Mike Gunderloy and Issue 4 of the Smart Access Newsletter. This function is illustrated in full as follows
Function GetDBPath() As String
Dim strPath As String
strPath = CurrentDb.Name
GetDBPath = left(strPath, intLastSlash)
Now the following code snippet will generate a subdirectory under the location of the Front End database. If the subdirectory already exists, the method fails and the code just rolls on as per normal.
On Error Resume Next
cachedir = GetDBPath & "backup\"
Finally we only require a very small amount of vb plus the Save method of the ADO recordset object to generate an XML text file for the current Table. Before exporting, it is necessary to delete the existing XML file if it already exists. If you do not wish to add ADO version 2.5 to your Access application, you can use the adPersistADTG constant rather than adPersistXML to save your recordsets in the faster, more compact (but binary) recordset format. This kind of defeats the readability of the files generated by this backup software.
Set rst = New ADODB.RecordSet
On Error Resume Next
Kill cachedir & datasource
Set rst = Nothing
Summing Up The XML Backup
Thatís all the code you need to save all the tables in your database to XML format. I hope this will get you out of a major data recovery issue at some time in the future. Remember that this code is written using the Universal ADO data handler which means that you can easily adapt it for your SQL server or Oracle databases if you like. For a thorough description of ADO/XML exporting and importing, Michael Corning described this process very well in March 1999 edition of Smart Access.
Building A List Of Tables And Views For A Combo Box
When I first wrote this code, it was actually developed for generating a list of Tables and Views (select queries) that I could pass to a combo box. As this can be quite useful for Access programmers, I have kept this code in the software so that you can also generate a combo box that displays the list. Figure 1 demonstrates the combo box in action.
The string becomes visible by making the "row source type" property a "values list" and allocating the following software generated text string to the "rowSource" property of the combo box.
"Table/View Name;Type;Categories;table;Customers;table;Employees;table;Order details;table;Orders;table;Products;table;Shippers;table;Suppliers;table;zWorld;table;
I am happy to inform you that building this list of tables and queries is a lot simpler now than the same code was when I wrote it using the DAO object. It is good to find positive reasons for adopting ADO in your Access application and this and XML exporting are definitely amongst those.
Useful Further Reading and Resources
Valid and Well-Formed XML in the March 1999 edition of Smart Access
http://www.microsoft.com/data/ is the place for your latest MDAC/ADO download
http://www.xmldevelopernewsletter.com/ is where you can find out more about XML.
User Called John Notes:
To fix this, you've got to tack on a password parameter to the Connect string. I just set up a prompt to "please re-enter your password," so the line now looks like this:
strConnect = CurrentProject.Connection & ";Password=" & strPWord
(2) You have to use care if the tables are linked, to ensure that you're exporting ONLY the data you really want to export. In my case, the DB links to a bunch of mainframe Oracle tables, via ODBC -- these are HUGE tables, which the DB application accesses only very selectively, so no way am I going to export those as well. I can think of a couple possible workarounds, e.g. inspecting the Connect string to be sure the function looks only at Access tables.
Author Bio. sign
up for his Access email newsletter here. The web site
features a popular shareware data mining tool written in Access. When Garry is
not sitting at a keyboard, he can be found playing golf or Greco-Roman Wrestling
at one of Sydney's Olympic venues. Contact details Ö
+61 2 9340 7789
sign up for his Access email newsletter here. The web site features a popular shareware data mining tool written in Access. When Garry is not sitting at a keyboard, he can be found playing golf or Greco-Roman Wrestling at one of Sydney's Olympic venues. Contact details Ö www.gr-fx.com +61 2 9340 7789
Other Pages On This Site You Might Like To Read