Saturday, November 27, 2010

Write XML in MS Access 2010 Using VBA

There is an XML theme for everything that I am writing and this post is no different, except this time, I want to meet you, so I am going to give you the opportunity to meet me. While you're waiting, you can try a game below or just wait for my twitter/LinkedIn notifcation. You can go right to my twitter page by clicking on my name right below the sympathy donation button.

Instead of a cartoon before the show, I will use video games. The channel that I will be broadcasting on is just below the games.





Preparing to go live now.

Create XML in VBA

'Author: Chris Sergent


'Date Created: November 27, 2010

'Purpose: To provide a basic example on how to create an XML document with VBA in MS Access.



Private Sub btnCreateXml_Click()

'Open the vbSkeleton.xml document if it exists and replace all contents. If

'the file does not exsist create it.

Open "c:\Templates\vbaSkeleton.xml" For Output As 1

'Declares the document as an XML document. This element is required at the top of the XML document.

'The declaration is required and must go at the top and only needs to be referenced at the top of the

'document. After the root element, XML documents require beginning and ending elements.

'That is to say that if we have an element or a tag such as , it must have a corresponding

'tag of
even if there is no content.

Print #1, "

Print #1, ""

Print #1, ""

'An XML document requires one and only one root or parent node is allowed and it must be empty.

'spatialDeveloperKnowledge is our parent node.

Print #1, ""

'Although this is like a parent node, you could add an attribute to the Developer node such as id.

'I don't recommend it. We'll use XPATH to navigate the XML DOM in later examples.

'There are no rules for this data, but that will be taken care of in a future XSD example.

Print #1, vbTab & ""

Print #1, vbTab & ""

'Set the focus on the txtFirstName control to obtain information for writing

txtFirstName.SetFocus

Print #1, vbTab & vbTab & "" & Me.txtFirstName.Text & ""



'Set the focus on the txtLastName control to obtain information for writing

txtLastName.SetFocus

Print #1, vbTab & vbTab & "" & Me.txtLastName.Text & " "



'Set the focus on the txtCity control to write the city data to file

txtCity.SetFocus

Print #1, vbTab & vbTab & ""

Print #1, vbTab & vbTab & "" & Me.txtCity.Text & " "



'Set the focus on the txtState control to write the name of the state

txtState.SetFocus

Print #1, vbTab & vbTab & "" & Me.txtState.Text & " "



'Set the focus on the txtCountry control to write out the name of the country

txtCountry.SetFocus

Print #1, vbTab & vbTab & "" & Me.txtCountry.Text & " "



Print #1, vbTab & vbTab & ""

'Set the focus on txtX1 to obtain the X coordinate

txtX1.SetFocus

Print #1, vbTab & vbTab & "" & Me.txtX1.Text & ""



'Set the focus on txtY1 to obtain the Y coordinate

txtY1.SetFocus

Print #1, vbTab & vbTab & "" & Me.txtY1.Text & ""



'Add measuring value

Print #1, vbTab & vbTab & ""

Print #1, vbTab & vbTab & "NA"



'Add elevation coordinate

Print #1, vbTab & vbTab & ""

Print #1, vbTab & vbTab & "0"





'Set the focus on the txtSkills control to write out the list of skills the developer has

txtSkills.SetFocus

'I added this with an array of skills, because I want to see, not just a developer meetup;

'but a meetup mashup. A developer that limits their access to knowledge, limits their potential

'to provide a solutions and skill dilutes the potential skills in creativity and problem solving.

'Even for the most skilled, a novice can still teach the elite in our field how to communicate with a

'novice. Knowing how to apply what you have learned is not as important as being able to enable others

'with the knowledge and the ability to apply it without help.

Print #1, vbTab & vbTab & "" & Me.txtSkills.Text & " "



Print #1, vbTab & "
"

Print #1, "
"

'Closes file. If you don't do this, you might receive a message that this file is in use. Don't leave it out.

'If you open a file in code; close a file in code. The same thing holds true with database connections.

Close #1



'This displays once the click event completes. There is no error checking for this example

'If you receceive an error and, ensure you have a folder named Templates in your root folder or "C:\" drive

'or modify the code to so that there is a folder to write to

MsgBox "vbaSkeleton.xml file has been created.", vbOKOnly, "File Written"





End Sub

 
Sample of the Output Document
 
Sample of a Resume with Inline CSS

No comments:

Post a Comment

I have had the opportunity to share this with you and I encourage you to share your comments with me.