PC Review


Reply
Thread Tools Rate Thread

multiple xml imports from local path

 
 
=?Utf-8?B?TWFydGlqbg==?=
Guest
Posts: n/a
 
      5th Sep 2005
I'm struggling with the import of xml files in access.

I receive the files in outlook and save them as client(1).xml, client(2).xml
etc in a local folder.
When I'm using the xml import function in code I only can import a file I
named in the code.

example:
Application.ImportXML DataSource:="D:\Clienten XML\client.xml",
ImportOptions:=acAppendData
(I can put in the same rule with client(2).xml etc, but there are a lot of
files this way).

This works fine but how can I tell the import engine that I want to import
all the *.xml files from "this location" etc.

I think I have to use a loop but I don't know how to define the code.

Can somebody help me?

Thanks
 
Reply With Quote
 
 
 
 
John Nurick
Guest
Posts: n/a
 
      5th Sep 2005
Hi Martijn,

Another MVP, Joe Fallon, posted this code a while ago to show how to use
Dir() to loop through all the files in a folder. You'll need to replace
DoCmd.TransferText with Application.ImportXML and make a few other
changes.

How to Import all Files in a Folder:

Private Sub btnImportAllFiles_Click()
'procedure to import all files in a directory and delete them.
'assumes they are all the correct format for an ASCII delimited import.
Dim strfile As String

ChDir ("c:\MyFiles")
strfile = Dir("FileName*.*")
Do While Len(strfile) > 0
DoCmd.TransferText acImportDelim, "ImportSpecName", "AccessTableName",
"c:\MyFiles\" & strfile, True
'delete the file (consider moving it to an Archive folder instead.)
Kill "c:\MyFiles\" & strfile
strfile = Dir
Loop

End Sub

On Mon, 5 Sep 2005 04:51:09 -0700, "Martijn"
<(E-Mail Removed)> wrote:

>I'm struggling with the import of xml files in access.
>
>I receive the files in outlook and save them as client(1).xml, client(2).xml
>etc in a local folder.
>When I'm using the xml import function in code I only can import a file I
>named in the code.
>
>example:
>Application.ImportXML DataSource:="D:\Clienten XML\client.xml",
>ImportOptions:=acAppendData
>(I can put in the same rule with client(2).xml etc, but there are a lot of
>files this way).
>
>This works fine but how can I tell the import engine that I want to import
>all the *.xml files from "this location" etc.
>
>I think I have to use a loop but I don't know how to define the code.
>
>Can somebody help me?
>
>Thanks


--
John Nurick [Microsoft Access MVP]

Please respond in the newgroup and not by email.

 
Reply With Quote
 
 
 
 
=?Utf-8?B?TWFydGlqbg==?=
Guest
Posts: n/a
 
      6th Sep 2005
Hi John,

Thanks for your replie!
I did copy, paste and changed the code many times to get the result.
Nothing worked, no error was given or files deleted / imported.

The last change I made is written below, so maybe you (or another nice
person can hekp me further out ;-))

I made realy a lot of changes in the application.import code, like
datasource:="strfile" etc, etc.

What code will see the right file?, because it seems that there are no files
to import.
(Ofcourse I checked every way)

The many files I got are named:

client.xml
client1.xml
client2.xml
etc.

the last change I made:
***

Private Sub btnImportAllFiles_Click()

Dim strfile As String

ChDir ("z:\XML_FILES")
strfile = Dir("Client*.xml")
Do While Len(strfile) > 0

Application.ImportXML DataSource:="z:\XML_FILES\" & strfile,
importoptions:=acAppendData

Kill "Z:\XML_FILES\" & strfile
strfile = Dir
Loop

End Sub

***

It is a hard one!

But thanks anyway!

"John Nurick" wrote:

> Hi Martijn,
>
> Another MVP, Joe Fallon, posted this code a while ago to show how to use
> Dir() to loop through all the files in a folder. You'll need to replace
> DoCmd.TransferText with Application.ImportXML and make a few other
> changes.
>
> How to Import all Files in a Folder:
>
> Private Sub btnImportAllFiles_Click()
> 'procedure to import all files in a directory and delete them.
> 'assumes they are all the correct format for an ASCII delimited import.
> Dim strfile As String
>
> ChDir ("c:\MyFiles")
> strfile = Dir("FileName*.*")
> Do While Len(strfile) > 0
> DoCmd.TransferText acImportDelim, "ImportSpecName", "AccessTableName",
> "c:\MyFiles\" & strfile, True
> 'delete the file (consider moving it to an Archive folder instead.)
> Kill "c:\MyFiles\" & strfile
> strfile = Dir
> Loop
>
> End Sub
>
> On Mon, 5 Sep 2005 04:51:09 -0700, "Martijn"
> <(E-Mail Removed)> wrote:
>
> >I'm struggling with the import of xml files in access.
> >
> >I receive the files in outlook and save them as client(1).xml, client(2).xml
> >etc in a local folder.
> >When I'm using the xml import function in code I only can import a file I
> >named in the code.
> >
> >example:
> >Application.ImportXML DataSource:="D:\Clienten XML\client.xml",
> >ImportOptions:=acAppendData
> >(I can put in the same rule with client(2).xml etc, but there are a lot of
> >files this way).
> >
> >This works fine but how can I tell the import engine that I want to import
> >all the *.xml files from "this location" etc.
> >
> >I think I have to use a loop but I don't know how to define the code.
> >
> >Can somebody help me?
> >
> >Thanks

>
> --
> John Nurick [Microsoft Access MVP]
>
> Please respond in the newgroup and not by email.
>
>

 
Reply With Quote
 
John Nurick
Guest
Posts: n/a
 
      6th Sep 2005
Hi Martijn,

Using Dir() in a loop like that is a long-established standard way of
processing multiple files in a folder in VBA, VB, and earlier versions of
Basic, and your code looks all right (although I have little experience with
Application.ImportXML).

In the VB editor, hit Ctrl-G to open the Immediate pane and then type:

ChDir "z:\XML_FILES"
? Dir("*.xml")

You should see the name of one of the XML files in the folder. Then type

? Dir()

This should produce the name of the next XML file.

If that is not happening, either you have typed the wrong folder name, or
there are no XML files in the folder - or perhaps the files are hidden.



"Martijn" <(E-Mail Removed)> wrote in message
news:C14B6BA3-E5F4-4051-87FE-(E-Mail Removed)...
> Hi John,
>
> Thanks for your replie!
> I did copy, paste and changed the code many times to get the result.
> Nothing worked, no error was given or files deleted / imported.
>
> The last change I made is written below, so maybe you (or another nice
> person can hekp me further out ;-))
>
> I made realy a lot of changes in the application.import code, like
> datasource:="strfile" etc, etc.
>
> What code will see the right file?, because it seems that there are no
> files
> to import.
> (Ofcourse I checked every way)
>
> The many files I got are named:
>
> client.xml
> client1.xml
> client2.xml
> etc.
>
> the last change I made:
> ***
>
> Private Sub btnImportAllFiles_Click()
>
> Dim strfile As String
>
> ChDir ("z:\XML_FILES")
> strfile = Dir("Client*.xml")
> Do While Len(strfile) > 0
>
> Application.ImportXML DataSource:="z:\XML_FILES\" & strfile,
> importoptions:=acAppendData
>
> Kill "Z:\XML_FILES\" & strfile
> strfile = Dir
> Loop
>
> End Sub
>
> ***
>
> It is a hard one!
>
> But thanks anyway!
>
> "John Nurick" wrote:
>
>> Hi Martijn,
>>
>> Another MVP, Joe Fallon, posted this code a while ago to show how to use
>> Dir() to loop through all the files in a folder. You'll need to replace
>> DoCmd.TransferText with Application.ImportXML and make a few other
>> changes.
>>
>> How to Import all Files in a Folder:
>>
>> Private Sub btnImportAllFiles_Click()
>> 'procedure to import all files in a directory and delete them.
>> 'assumes they are all the correct format for an ASCII delimited import.
>> Dim strfile As String
>>
>> ChDir ("c:\MyFiles")
>> strfile = Dir("FileName*.*")
>> Do While Len(strfile) > 0
>> DoCmd.TransferText acImportDelim, "ImportSpecName", "AccessTableName",
>> "c:\MyFiles\" & strfile, True
>> 'delete the file (consider moving it to an Archive folder instead.)
>> Kill "c:\MyFiles\" & strfile
>> strfile = Dir
>> Loop
>>
>> End Sub
>>
>> On Mon, 5 Sep 2005 04:51:09 -0700, "Martijn"
>> <(E-Mail Removed)> wrote:
>>
>> >I'm struggling with the import of xml files in access.
>> >
>> >I receive the files in outlook and save them as client(1).xml,
>> >client(2).xml
>> >etc in a local folder.
>> >When I'm using the xml import function in code I only can import a file
>> >I
>> >named in the code.
>> >
>> >example:
>> >Application.ImportXML DataSource:="D:\Clienten XML\client.xml",
>> >ImportOptions:=acAppendData
>> >(I can put in the same rule with client(2).xml etc, but there are a lot
>> >of
>> >files this way).
>> >
>> >This works fine but how can I tell the import engine that I want to
>> >import
>> >all the *.xml files from "this location" etc.
>> >
>> >I think I have to use a loop but I don't know how to define the code.
>> >
>> >Can somebody help me?
>> >
>> >Thanks

>>
>> --
>> John Nurick [Microsoft Access MVP]
>>
>> Please respond in the newgroup and not by email.
>>
>>



 
Reply With Quote
 
=?Utf-8?B?TWFydGlqbg==?=
Guest
Posts: n/a
 
      6th Sep 2005
Hi John,

I did what you suggested: and look

ChDir "z:\XML_FILES"
? Dir("*.xml")

>>> nothing happend, but when I did:


?dir("\XML_FILES\*.xml")
Client.xml
?dir()
Client1.xml

So the chdir isn't working.

I found out that if the standard db map is C:\My documents, the ?Dir("*.*")
gives the first file in c:\My documents.

The solving must be that I call the "\XML_FILES\ dir "on every loop

I changed the code to:
***
Private Sub btnImportAllFiles_Click()

Dim strfile As String

ChDir ("z:\XML_FILES")
strfile = Dir("\XML_FILES\Client*.xml")
Do While Len(strfile) > 0

Application.ImportXML DataSource:="& strfile", importoptions:=acAppendData


'delete the file (consider moving it to an Archive folder instead.)
Kill "Z:\XML_FILES\*.xml"
strfile = Dir
Loop

End Sub
***

Then I got a error -2146697203 (800c000d) on running and Method ImportXML
of object_Application failed
(my translated Dutch to Enlish)

So now I am a little bit further, and have to rewrite and puzzle on the code
in that line.

But still I am very happy that you're trying to help me out!

regards, Martijn



"Martijn" wrote:

> I'm struggling with the import of xml files in access.
>
> I receive the files in outlook and save them as client(1).xml, client(2).xml
> etc in a local folder.
> When I'm using the xml import function in code I only can import a file I
> named in the code.
>
> example:
> Application.ImportXML DataSource:="D:\Clienten XML\client.xml",
> ImportOptions:=acAppendData
> (I can put in the same rule with client(2).xml etc, but there are a lot of
> files this way).
>
> This works fine but how can I tell the import engine that I want to import
> all the *.xml files from "this location" etc.
>
> I think I have to use a loop but I don't know how to define the code.
>
> Can somebody help me?
>
> Thanks

 
Reply With Quote
 
John Nurick
Guest
Posts: n/a
 
      6th Sep 2005
Martijn,

The reason ChDir() seems to fail is (I am pretty sure) that your current
drive is C: and therefore
ChDir "z:\XML_FILES"
is changing the current directory on Z: but leaving the current drive as
C:. This means that
?Dir("*.xml")
will go to the current directory on the current drive (e.g. "C:\...\My
Documents"). You can check this by using
?CurDir()
which returns what Access thinks is the current directory.

So one way to solve the problem is to explicitly change the drive

ChDir "Z:\XML_FILES"
ChDrive "Z:"
strfile = Dir("*.xml")
Do While Len(strfile) > 0
Application.ImportXML strFile, acAppendData
strFile = Dir()
Loop
...

Alternatively, avoid using ChDir and ChDrive altogether. You need to
remember that when you do
?Dir("z:\XML_FILES\*.xml")
you get
Client.xml
but what you need to pass to Application.ImportXML is the full filespec
z:\XML_FILES\Client.xml
so you need to do something like this:

Dim strFolder As String
...
strFolder = "z:\XML_FILES\"
strFile = Dir(strFolder & "*.xml")
Do While Len(strFile) > 0
Application.ImportXML strFolder & strFile, acAppendData
strFile = Dir()
Loop



When you do
?Dir("z:\XML_FILES\*.xml")
you get
Client.xml
but what you need to pass to Application.ImportXML is the full filespec
z:\XML_FILES\Client.xml



On Tue, 6 Sep 2005 06:57:03 -0700, "Martijn"
<(E-Mail Removed)> wrote:

>Hi John,
>
>I did what you suggested: and look
>
>ChDir "z:\XML_FILES"
> ? Dir("*.xml")
>
>>>> nothing happend, but when I did:

>
>?dir("\XML_FILES\*.xml")
>Client.xml
>?dir()
>Client1.xml
>
>So the chdir isn't working.
>
>I found out that if the standard db map is C:\My documents, the ?Dir("*.*")
>gives the first file in c:\My documents.
>
>The solving must be that I call the "\XML_FILES\ dir "on every loop
>
>I changed the code to:
>***
>Private Sub btnImportAllFiles_Click()
>
>Dim strfile As String
>
>ChDir ("z:\XML_FILES")
>strfile = Dir("\XML_FILES\Client*.xml")
>Do While Len(strfile) > 0
>
> Application.ImportXML DataSource:="& strfile", importoptions:=acAppendData
>
>
> 'delete the file (consider moving it to an Archive folder instead.)
> Kill "Z:\XML_FILES\*.xml"
> strfile = Dir
>Loop
>
>End Sub
>***
>
> Then I got a error -2146697203 (800c000d) on running and Method ImportXML
>of object_Application failed
>(my translated Dutch to Enlish)
>
>So now I am a little bit further, and have to rewrite and puzzle on the code
>in that line.
>
>But still I am very happy that you're trying to help me out!
>
>regards, Martijn
>
>
>
>"Martijn" wrote:
>
>> I'm struggling with the import of xml files in access.
>>
>> I receive the files in outlook and save them as client(1).xml, client(2).xml
>> etc in a local folder.
>> When I'm using the xml import function in code I only can import a file I
>> named in the code.
>>
>> example:
>> Application.ImportXML DataSource:="D:\Clienten XML\client.xml",
>> ImportOptions:=acAppendData
>> (I can put in the same rule with client(2).xml etc, but there are a lot of
>> files this way).
>>
>> This works fine but how can I tell the import engine that I want to import
>> all the *.xml files from "this location" etc.
>>
>> I think I have to use a loop but I don't know how to define the code.
>>
>> Can somebody help me?
>>
>> Thanks


--
John Nurick [Microsoft Access MVP]

Please respond in the newgroup and not by email.

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Receiving Error: Unable to copy file "obj\Debug\MyProject.xml" to "bin\Debug\MyProject.xml". Access to the path 'obj\Debug\Aegis.xml' is denied. Aegis CGatto Microsoft VB .NET 4 24th Jul 2010 12:09 AM
Imports System.Data or Imports System.Data.SqlClient? Albert Microsoft ASP .NET 4 10th Jul 2008 10:00 AM
what encoding does system.xml.xmldocument.save(string path) use to save the xml document if there is no <?xml... in the front of the xml document? Daniel Microsoft C# .NET 7 17th Mar 2007 11:29 AM
what encoding does system.xml.xmldocument.save(string path) use to save the xml document if there is no <?xml... in the front of the xml document? Daniel Microsoft Dot NET Framework 5 17th Mar 2007 10:11 AM
what encoding does system.xml.xmldocument.save(string path) use to save the xml document if there is no <?xml... in the front of the xml document? Daniel Microsoft Dot NET 1 15th Mar 2007 10:37 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:02 AM.