473,419 Members | 1,839 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,419 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 25777
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: 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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.