Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsFormsForms ProgrammingQueriesModules / DAO / VBAReports / PrintingMacrosDatabase DesignSecurityConversionImporting / LinkingSQL Server / ADPMultiuser / NetworkingReplicationSetup / ConfigurationDeveloper ToolkitsActiveX ControlsNew UsersGeneral 1General 2
Access DirectoryToolsTutorialsUser Groups
Related Topics
SQL ServerOther DB ProductsMS OfficeMore Topics ...

MS Access Forum / New Users / June 2006

Tip: Looking for answers? Try searching our database.

Import data from an external source through code

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Chris - 16 Jun 2006 17:03 GMT
Hi All,

We have around 50 txt files that are being imported into access each day.
At the moment it is done manually (file -> get external data -> import ->
etc etc etc.

It is getting imported into an existing table and is the procedure for
importing never changes

Any idea's?
Jeff Boyce - 16 Jun 2006 18:25 GMT
Chris

One alternative to importing a text document is to add a "pointer"/path to
the document's file location.

Regards

Jeff Boyce
Microsoft Office/Access MVP

> Hi All,
>
[quoted text clipped - 6 lines]
>
> Any idea's?
KARL DEWEY - 16 Jun 2006 18:30 GMT
Consider using the DOS command COPY to append the text files into a single
file.

> Hi All,
>
[quoted text clipped - 6 lines]
>
> Any idea's?
Klatuu - 16 Jun 2006 18:52 GMT
Are all the files in the same directory?
Are all the files in the same format?
Do all the files get appended to the same table?
Does the data in the files replace data in a table or tables or is the data
appended to existing tables?

> Consider using the DOS command COPY to append the text files into a single
> file.
[quoted text clipped - 9 lines]
> >
> > Any idea's?
Chris - 19 Jun 2006 11:09 GMT
> Are all the files in the same directory? - Yes
> Are all the files in the same format? - Yes
> Do all the files get appended to the same table? - Yes
> Does the data in the files replace data in a table or tables or is the data
> appended to existing tables? - The table's data is deleted every day, so
the data will be going into an empty table.

> > Consider using the DOS command COPY to append the text files into a single
> > file.
[quoted text clipped - 9 lines]
> > >
> > > Any idea's?
Klatuu - 19 Jun 2006 14:16 GMT
Okay, great.  You can use the Dir function to set up the loop to do the
imports.  First, I would suggest you set up an Import Specification for the
text files.  You do that by manually preforming an import.  When you get the
import dialog, click on Advanced.  There  you can define field names, data
types, etc.  Once you have everything configured the way you want it, click
on Save As and give it a name.  This is the name you will use in your
TransferText method.  In addition to your production table, you will need an
intermediate table.  This table will recieve data from the text file, then be
used to transfer the data to the production table.

Here is the seguence of events:
1. Delete the data in the production table
2. Delete the data in the intermediate table
3. Import the text file to an intermediate table
4. Run an Append query to append the data to the production table
5. Repeat 2 - 4 for all text files in the folder.

The code will look something like this:

Dim strNextFile as String
Dim strFilePath as String
Dim dbf as Database

   dbf = CurrentDb
   strPath = "F:\WhereMyFilesAre\"

   strNextFile = Dir(strPath & "*.txt") ' If extension is not txt, change
this to match
   dbf.Execute("DELETE * FROM MyProductionTable;"), dbFailOnError

   Do While strNextFile <> ""
       dbf.Execute("DELETE * FROM MyIntermediateTable;"), dbFailOnError
       DoCmd.TransferText acImportDelim, ImportSpecName,
           "MyIntermediateTable", strPath & strNextFileName, True
       dbf.Execute("qappAddToProduction"), dbFailOnError
       strNextFile = Dir()
   Loop

> > Are all the files in the same directory? - Yes
> > Are all the files in the same format? - Yes
[quoted text clipped - 20 lines]
> > > >
> > > > Any idea's?
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.