473,385 Members | 1,693 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Batch Import text files to Access

Hi, I need some help. The routine below runs when a command button is clicked. A prompt appears to enter the directory path to the folder containing delimited text files to be imported into an Access 97 table. The test folder contains 9 files. The problem is that the 1st file is imported 9 times into the table. How do I correct the routine so it loops properly and imports each file? Thanks in advance.


Gary


Private Sub Command0_Click()
Dim InputDir, ImportFile As String, tblName As String
Dim InputMsg, Name As String

InputMsg = "Type the pathname of the folder that contains "
InputMsg = InputMsg & "the files you want to import."
InputDir = InputBox(InputMsg)
' Change the file extension on the next line for the
' type of file you want to import.
ImportFile = Dir(InputDir & "\*.txt")
Name = ((InputDir) & ("\") & (ImportFile))
Do While Len(ImportFile) > 0
' Use the import file name without its extension as the table
' name.
tblName = "Expenses"

DoCmd.TransferText [acImportDelim], "Batch", "ExpBatch", Name
ImportFile = Dir
Loop
End Sub
Jun 23 '06 #1
3 25748
PEB
1,418 Expert 1GB
Hi,
When your file is inserted in your database it'll be good to change its extension from txt to tx1 for exemple! So this file won't apear in your file list to insert in the database

To do this you have to use:

Name OldName As NewName :)
Sep 3 '06 #2
cactusdata
214 Expert 128KB
This works for me:
Expand|Select|Wrap|Line Numbers
  1. Private Sub ImmportFiles_Click()
  2.  
  3.     Dim InputDir        As String
  4.     Dim ImportFile      As String
  5.     Dim InputMsg        As String
  6.     Dim Name            As String
  7.  
  8.     InputMsg = _
  9.         "Type the pathname of the folder that contains " & _
  10.         "the files you want to import."
  11.     InputDir = InputBox(InputMsg, "Import text files")
  12.  
  13.     ' Change the file extension on the next line for the
  14.     ' type of file you want to import.
  15.     ImportFile = Dir(InputDir & "\*.txt", vbNormal)
  16.     Do While Len(ImportFile) > 0
  17.         Name = InputDir & "\" & ImportFile
  18.         ' Use the import file name without its extension as the table name.
  19.         DoCmd.TransferText acImportDelim, "Batch", "ExpBatch", Name
  20.         ImportFile = Dir
  21.     Loop
  22.  
  23. End Sub
Feb 28 '24 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

12
by: Moosebumps | last post by:
So, after reading some messages about os.system, and looking at the popen stuff and trying it a bit, I still have not found a way to keep a command window open for several commands (on Windows...
7
by: erniedude | last post by:
Hi, I'm a newbie and I was wondering if anyone knew a (Python) script to run 4 batch files, one after the other (assuming the directories are known). It would be better if all 4 batch files...
1
by: Dan | last post by:
Could someone please help me with auto importing a series of data files into an Access table. I tried to follow code given below in a previous messagebut i'm getting error messages. Here's my...
4
by: ScoobyDoo | last post by:
Anyone know how I can create and edit batch files from MS Access 2002?
12
by: Selva Chinnasamy | last post by:
Hi I am using batch commands against Ms-Access and getting an error Message "Characters found after end of SQL statement." String Here is my sql Dim str_screens As String = "Select * from Screens...
1
by: Child of His | last post by:
I have been through every trick I know, or has been suggested. I have a one to two million line fixed field database in text format. I want to bring it into Access 97. When I use the external...
7
by: Pete | last post by:
I need to import multiple tab delimited files for which I do not have the formats. All files have column headers in the the line. The files must be dynamic. The only common value is that the...
0
by: brendan | last post by:
I'm trying to write a code that batch renames photos. In the end each photo should be named: AABBB1111YYYYMM222 where AABBB1111 defines a given place. (i.e. CAMTL = montreal and four digits...
6
by: provor | last post by:
Hello, I have the following code that I am using when a user presses a button to import an excel file into a table. The code is hard coded to point to the correct table. This works great for this...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.