Solved

Import XML file into access table using VBA

Posted on 2013-12-03
3
9,945 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

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Dsum Function for List Box Data 7 45
Normalization of a table 19 74
VBA Access 2016 syntax 6 43
Trying to open FORM in specific record !! 6 45
Overview: This article:       (a) explains one principle method to cross-reference invoice items in Quickbooks®       (b) explores the reasons one might need to cross-reference invoice items       (c) provides a sample process for creating a M…
Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

773 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