Import Text Files Into Access 2010 using VBA

I have several '.txt'  files saved in a folder. I am trying to get a code that upon execution will go through this folder and import all data in the text files into 1 access table. I am working with Access 2010.

Assumptions:

Database location: C:\My Documents\ImportData.accdb
Text file locations:  C:\My Documents\TextFiles
Text files: Separated by 1 space (no commas) and has no headers. (example attached)
Access table created: ImportedTextData

There will be hundred of text files in this folder. Any help would be appreciated.

Thank you
EESample.txt
ctownsen80Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

als315Commented:
You sample has space and (or) tab between columns. Can you confirm "Separated by 1 space (no commas)"?
0
ctownsen80Author Commented:
I'm sorry about that. It is separated by a TAB...no comma, between each column,
0
Rey Obrero (Capricorn1)Commented:
to automate this import process, you need an import specification.

'to create an import specification
'
1. File>get external data>import
2. select in the File of types box   Text files (*.txt etc..
3. select the file
4. in the import text wizard window select  delimited/fixed
5. Click advanced
6. in the import specification window
    type the name of the field in the Field Name column
    (here you can use the field names of the destination table, specify data type,
      check the box Skip if you do not want to import the column)


7 click save as, give the specification a name  <-- this is the specification name that you will use in the command line below

Now you can use the code below to import the text file via code

   DoCmd.TransferText acImportFixed, "ImportSpecificationName", "myTableName", "C:\myTextr.txt", false
0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

Rey Obrero (Capricorn1)Commented:
for a2007, a2010

To create the Import Specification
   1) Click on external data> text file which then opens another window called "Get external data - Text file"
   2) Use radio button to select "Import the source data into a new table in the current database"
   3) specify the source of the file using the browse procedure then click OK
   4) Choose radio button to select delimited format and then click next
   5) this window allows you to choose delimiter and text qualifier and if first row contains names - click next
   6) This window allows you to type the name of the field in the Field Name column, choose data type, and if you want field indexed - also can choose to skip field - then click next
   7) This window allows you to add primary key or designate field as such - click next
   8) Click on ADVANCED button
   9. in the import specification window
   type the name of the field in the Field Name column
   (here you can use the field names of the destination table, specify data type,
   check the box Skip if you do not want to import the column)
   10 click save as, give the specification a name <-- this is the specification name that you will use in the command line below


 DoCmd.TransferText acImportDelim, "ImportSpecificationName", "TableName", "C:\folderName\myText.txt" , False
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ctownsen80Author Commented:
The name of the Import specification is 'ImportTextData'.  The table name that it's creating is 'NewTextData'. Below is the VB script I put together......not an expert but took a stab:

Function importdata()
DoCmd.TransferText acImportDelim, "ImportTextData", "TextAnalyticsData", "C:\My Documents\EESample.txt", False
End Function

When I run that, I get this error message: "Run Time Error '3625' The text file specification 'ImportTextData' does not exist. You cannot import, export, or link using the specification".

Where'd I go wrong? :)
0
ctownsen80Author Commented:
@Rey -- sorry, last message was a typo:

The name of the Import specification is 'ImportTextData'.  The table name that it's creating is 'NewTextData'. Below is the VB script I put together......not an expert but took a stab:

 Function importdata()
 DoCmd.TransferText acImportDelim, "ImportTextData", "NewTextData", "C:\My Documents\EESample.txt", False
 End Function

 When I run that, I get this error message: "Run Time Error '3625' The text file specification 'ImportTextData' does not exist. You cannot import, export, or link using the specification".

 Where'd I go wrong? :)
0
Rey Obrero (Capricorn1)Commented:
if you are getting that error, for sure the import specification is not found.

did you follow the instruction i posted about creating an import specification?
0
ctownsen80Author Commented:
I went over again and saw a vital step I missed earlier. Worked like a charm. I actually needed it to loop through and grab several files, BUT I was actually able to adjust the code to do that myself. Many thanks for your help and direction!
0
Bill PrewIT / Software Engineering ConsultantCommented:
Why not just start by appending all the individual tables together into a single text file, and then importing that into the Access table as a single file?  A simple COPY command can merge the files together, like:

copy "C:\My Documents\TextFiles\*.*" "C:\My Documents\ImportTextData.txt"

Then just use the Access import tool to load it.

~bp
0
Bill PrewIT / Software Engineering ConsultantCommented:
Whoops, a lot of activity between when I read the question, and when I was able to reply, sorry about that.

~bp
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.