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?
 
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
 
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
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
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
 
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 PrewCommented:
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 PrewCommented:
Whoops, a lot of activity between when I read the question, and when I was able to reply, sorry about that.

~bp
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.