How To Generate A XML File With Access VBA

XML is used to structure your data. You can send a CSV file to another person, but
using XML, you can structure your data in a more elegant format, and get very specific
about your fields, and what data is contained within your tags (your field names).

This is different than a basic CSV file where you don’t have that ability.

You can essentially provide instructions to the receiving tool on how to handle the
information they have received.

Using HTML you have specific tags you can use, so you are limited, but with XML you can customize your tags,
and you can display that data file on a webpage.

In this post you are going to be able to generate an XML file with the data from your database.

For example, I need to transfer my database information to my scanning devices so the field team
can finish their inspections, away from the office. So I’ll generate an XML file which I can email and load
into their scanning device.

Private Sub btnXML_Click()
    Dim strSQL As String
    Dim rst As Recordset
    
    strSQL = "SELECT ID, Description, [Income/Expense], Taxable FROM Categories"
    
    Set rst = CurrentDb.OpenRecordset(strSQL)
    
    strpath = CurrentProject.Path & "\CategoryList.xml"
    
    Set fso = CreateObject("Scripting.FileSystemObject")
    
    Dim objFile As Object
    Set objFile = fso.CreateTextFile(strpath, True, True)
    
    objFile.Write "<?xml version='1.0'?>" & vbCrLf
    objFile.Write "<Categories>" & vbCrLf
    
    Do Until rst.EOF
        
        objFile.Write "<ID>" & rst.Fields("ID") & "</ID>" & vbCrLf
        objFile.Write "<Description>" & rst.Fields("Description") & "</Description>" & vbCrLf
        objFile.Write "<IncomeExpense>" & rst.Fields("Income/Expense") & "</IncomeExpense>" & vbCrLf
        objFile.Write "<Taxable>" & rst.Fields("Taxable") & "</Taxable>" & vbCrLf
        
        rst.MoveNext
    Loop
    
    objFile.Write "</Categories>"
    
    rst.Close
    Set rst = Nothing
    
    MsgBox "Complete"
End Sub

Here is the resulting file CategoryList.xml in same path your database is in:






By the way, if you got or are getting value from the VBA information, please give me a tip, thanks!


These posts may help answer your question too...

How To Parse A Flat File In Excel VBA

In another post I demonstrated how to access a file on your computer using the MS Office Library. Here it is if you don’t know what I’m talking about. In this post, I am going to show you how to access the file and load it into your spreadsheet. I will do the same thing […]

How do I handle errors in Access VBA code?

I am going to give you the answer to “How do I handle errors in Access VBA code?” In my opinion there are 2 ways to handle errors: 1. Avoid the potential for an error to occur. 2. Handle the error in your code. Number 1 – If you have a control on your form […]

How can I interact with other Office applications (Excel) using VBA in Access?

Need to write your Access data or query to an Excel file? Here is the how to do it: Most people are familiar with Excel and know how to use it well (enough), and when you start talking about Access, they get scared off, and don’t know what to do anymore. Well, here you are […]

What Is Microsoft Access Used For?

To those of you who are asking the question of “What is microsoft access used for?” , here is my opinion. I’ve been using this program for well over 15 years, and it’s become fairly easy to deal with. Many people feel that it is hard to work with, but that’s not my experience anymore […]

Previous Post

How To Make An Access Select Query

Next Post

Mouse Click Counter On Access Form