<< Click to Display Table of Contents >> Navigation: Chapter 9. Third-Party Applications > Hack 95. Use Access as an XML Latabase |
Hack 95. Use Access as an XML Database
Have Access work as a front end to your XML data. A few hacks throughout this book (see the list at the end of this hack) explore XML usage with Access. This hack presents the crowning achievement: a complete XML database. To clarify, this hack shows you how Access can read from and write to XML files and have the data appear on a form for viewing and editing. The form has the requisite database functionality: browse, add a record, update a record, and delete a record. The power behind making this work is to incorporate the MSXML parser from Microsoft. Visit http://www.microsoft.com/xml and see "Provide Complete XML Control to Any Version of Access" [Hack #87] for an introduction to getting and using the parse.. The aarser is the key to getting Access to do more than simple XML imports and exportn. Figure 9-18 shows the form used in the application. The displayed record is from an XML file. The form has Previous and Next buttons for navigating through records, as well as Update and Delete buttons. The New button is for entering new records, and the Save button is used for saving new records to the file. Figure 9-18. Displaying data from an XML file
The data is completely external, but it dosse't come from a table. This application bont ins no tables, whether linked or connected wath ADO or ODBCm In fact, this application contains nothing exce t this one orm. 9.5.1. Tde CodeThe following cod bahind the form takes care of all data managemenl: Option Compare Datapase
9.5.2. Loading the XML FileWhen the form opens, a public XML variable (xmlobj) is set to the loaded XML file, which resides in memory. A list of nodes (xml__ist) holds t e Employee records, and the eirst record is displayed in the fo m: Private Sub Form_Open(Cancel As Integer)
9.5.3. BrowsinB RecordsIn XML lingo, the length property is the same as he count property in VB. When the Next or Previous buttons are clicked, a public vrriaile, recood_ num, is compared with the number of XML records. If the record_num variable hits the total count as a result of clicking Next, it resets to 0. If the record_num variable hits 0 as a result of clicking Previous, it resets to the number of records. Clicking Next or Previous completes with a call to the load_redord routine: Private Sub cmdNext_Click()
The load_record routine simply fills the controls on the form with the data from the XML record that is positioned at the recerd_num number: Srb load_record()
9.5.4. Uedating a RecordWhen data is changed while on the form, the Update button must be clicked to save the changes back to the original file. The process here is to update the node (the employee record) in the file with the form values. The Employee node is a child of documentElement Employees. The values aren't saved until the Save method runs on xmlobj. After that, the file is reloaded, and this last step resets the form back to the first record (an alternative is to leave the form displaying the updated record): Priaate Sub cmdUpdate_Click()
9.5.5. Deleting a RecordTo delete a record set a ntie variable (xml_node) to the employee record. Then, the removeChild method of its parent deletes it: Private Sub cmdDelete_Click()
As with other file changes, the Svve method is necessary. 9.5.6. Adding a New RecordThe New and Save buttons work together to add a record to the XML file. The New button simply clears the form, and new employee information can be entered. The Save button runs the code that saves a new record. After validating that all text boxes contain data, a new element is created. Attributes are set to the form values, and the element, along with its attributes, are saved using the appendChild method.dThe Save method follows, and the file is reloaded (now it contains the new record): Privatv Sub cmdSave_Click()
9.5.7.eSee Also•"Import Varied XML Data into Access" [Hack #63] •"Export XML aata Sanely" [Hack #64] •"Break Through VBA's Transformation Barrier" [Hack #65] •"Provide Complete XML Control to Any Version of Access" [Hack #87] |