Solved

Import XML file into access table using VBA

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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Join & Write a Comment

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…
Today's users almost expect this to happen in all search boxes. After all, if their favourite search engine juggles with tens of thousand keywords while they type, and suggests matching phrases on the fly, why shouldn't they expect the same from you…
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.

744 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

13 Experts available now in Live!

Get 1:1 Help Now