Solved

Import XML file into access table using VBA

Posted on 2013-12-03
3
9,723 Views
Last Modified: 2013-12-04
Hi Experts,

I am working on an access form that browses for an xml file and then imports it into a table.  I have the browse button complete using VBA, but i am not sure how to write the code to import the XML file into the table.  I would like to import this xml file into a table called "data", and have it create fields for the fields on the XML file.  Here's my code for the browse button if that is any help.  Im not sure how to go about adding the xml file to the table using vba.


Option Compare Database

Option Explicit

Public Function GetFile() As Variant
Dim dialog As Object
Dim pickedfile As Boolean
Set dialog = Application.FileDialog(3)
GetFile = Null
With dialog
    .AllowMultiSelect = False
    .Title = "Please select file for import"
    .Filters.Clear
    .Filters.Add "XML Files", "*.XML"
    pickedfile = False
    pickedfile = .Show
    If pickedfile Then
        GetFile = .SelectedItems.Item(1)
    End If
End With
End Function

Open in new window


Thanks
0
Comment
Question by:Lobb
  • 2
3 Comments
 
LVL 84

Accepted Solution

by:
Scott McDaniel (Microsoft Access MVP - EE MVE ) earned 500 total points
ID: 39694090
Access has the ImportML function:

http://msdn.microsoft.com/en-us/library/office/bb237973%28v=office.12%29.aspx

Essentially you use it like this:

Application.ImportXML "FullPathToYourXMLFile"

In your code above, you'd do this after you've got the file, so:

Dim sFile As String
sFile = GetFile()
Application.ImportXML sFile

You'd then move it over to the table you want.

Here's a TechNet article that might give more insight: http://technet.microsoft.com/en-us/library/ee692914.aspx

That said - generally the builtin method is not full-featured enough to do much, and you often need to do this using the DOM library:

Dim oDoc As MSXML.DOMDocumnet
Set oDoc = New MSXML.DOMDocument

If oDoc.Load("Path to your xml file") Then
  '/ Document is loaded
  Dim oNodes As MXSML.IXMLDOMNodeList
  oNodes = oDoc.Nodes
 
  Dim oNode As MSXML.IXMLDOMNode
  For Each oNode in oNodes
   '/ do something here.
  Next oNode
End If

See this for more information: http://msdn.microsoft.com/en-us/library/aa468547.aspx
0
 

Author Comment

by:Lobb
ID: 39696533
Thanks for your help so far LSMconsulting,

I've created a button on the form to submit this data. I am not sure how to put this file into a specific table.   Could you help me out on importing the data to a table? So far here is the code I have.  Also, it the import options line a built in option or do I need to specify it?

Private Sub Command10_Click()

Dim File As Variant
File = GetFile()
If IsNull(File) Then
    MsgBox "Nothing was selected", vbOKOnly
Else
    Me.FName = File


Application.ImportXML _
    DataSource:=Me.FName, _
    ImportOptions:=acStructureAndData
 
End If
End Sub

Open in new window

0
 

Author Comment

by:Lobb
ID: 39696569
I figured out how to do the import options..  I didn't realize there was a button to make your own options. Thanks for your help LSMconsulting!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

The first two articles in this short series — Using a Criteria Form to Filter Records (http://www.experts-exchange.com/A_6069.html) and Building a Custom Filter (http://www.experts-exchange.com/A_6070.html) — discuss in some detail how a form can be…
It took me quite some time to sort out all the different properties of combo and list boxes available from Visual Basic at run-time. Not that the documentation is lacking: the help pages are quite thorough and well written. The problem was rather wh…
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now