473,398 Members | 2,343 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,398 software developers and data experts.

Import multiple txt files and filename?

1
Hi,

I have a question regarding import of multiple Text files.

These files all have different names but will always be found in the same location.

C:\Documents and Settings\computer\Desktop\F FILES

I have little VBA knowledge and am wondering if I can import ALL these files into an existing Access table tbl_Table via a macro?

A number of problems:

1. It does not import directly into the existing table, instead it creates a new table tbl_Table , I'd like to import directly into the existing table without using append or similar.

2. I cannot get it to import ALL files in the folder.

3. I'd like to import the first 7 characters of the filename into a field (KEY).

I've already read the Knowledgebase and found the Batch Import, but this does not help as you have to manually enter the name of each file and I have over 3,700 individual txt files.

I found code but I want to work with a text file.

Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdImport_Click()
  2. On Error GoTo ErrHandler
  3.  
  4. Dim oFSystem As Object
  5. Dim oFolder As Object
  6. Dim oFile As Object
  7. Dim sFolderPath As String
  8. Dim SQL As String
  9. Dim i As Integer
  10.  
  11. sFolderPath = "C:\My Documents\"
  12.  
  13. Set oFSystem = CreateObject("Scripting.FileSystemObject")
  14. Set oFolder = oFSystem.GetFolder(sFolderPath)
  15.  
  16. For Each oFile In oFolder.files
  17. If Right(oFile.Name, 4) = ".dbf" Then
  18. SQL = "Insert into [tblFORMGUIDE]" _
  19. & " Select """ & Left(oFile.Name, 7) & """ as [Key],*" _
  20. & " from " & Left(oFile.Name, Len(oFile.Name) - 4) _
  21. & " IN """ & sFolderPath & """ ""dBASE 5.0;"""
  22.  
  23. DoCmd.SetWarnings False
  24. DoCmd.RunSQL SQL
  25. DoCmd.SetWarnings True
  26. i = i + 1
  27. End If
  28. Next
  29.  
  30. MsgBox i & " dbf files were imported."
  31. Exit Sub
  32.  
  33. ErrHandler:
  34. MsgBox Err.Description
  35. End Sub
  36.  
  37.  
Any help or examples would be greatly appreciated.



Thanks.
Attached Files
File Type: zip Import Access 2000.zip (30.8 KB, 225 views)
Apr 14 '12 #1
1 6167
twinnyfo
3,653 Expert Mod 2GB
mbc321,

The code below should get you started, but your real challenge is going to be transferring the KEY to the table you want to import to.

It might be possible to keep that field blank during the import, then create a recordset which queries the table, retrieving only those records that have a blank KEY, then updating that field for every record. I am not comfortable that would be the best way, but it might work.

Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2. Option Explicit
  3.  
  4. Private Sub ImportTextFiles()
  5. On Error GoTo EH:
  6.     Dim strFileName As String
  7.     Dim strKey
  8.     strFileName = Dir(DirectoryName)
  9.     Do While strFileName <> ""
  10.         strKey = Left(strFileName,7)
  11.         DoCmd.TransferText acImportDelim, , tblTableName, DirectoryName & "/" & strFileName, False
  12.         strFileName = Dir()
  13.     Loop
  14.     'Here is where you could create your recordset to update KEY
  15.     Exit Sub
  16. EH:
  17.     MsgBox Err.Number & " " & Err.Description
  18.     Exit Sub
  19. End Sub
  20.  
Jul 25 '12 #2

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

Similar topics

1
by: Ezekiël | last post by:
Hi, How can i import multiple accessfiles in sql server 2000 by using a dts package? The dts package should import files with the most current date in a directory (not today's date). The date...
0
by: ano1optimist | last post by:
I'm trying to import multiple files that contain the following directory structure: c:\main\subfolder1\sample.mdb c:\main\subfolder2\sample.mdb c:\main\subfolder3\sample.mdb where the...
1
by: Primo | last post by:
Hello, I am building a data management application with the following processes: Process 1 is a Windows service which uses FileSystemWatcher to monitor a directory. Process 2 opens a file...
17
by: Kerr | last post by:
Hi all, I am sure that this is quite simple but can't find any good examples anywhere. I need a method of copying multiple files from a source directory to a destination directory. The file...
1
by: Chicagoboy27 | last post by:
Is there a way to import multiple csv files from a directory into sql 2005? The situation I have right now is that I have a folder with multiple csv files that i need to import into sql 2005. I can...
0
by: leen | last post by:
hola.. i need a help...this is my coding for uploading multiple files...the problem is..i want the files that i have been upload will show in the same page but using only one text box..can anyone...
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...
8
by: SAIRAAM | last post by:
hi all i am right now examing one of the project thats avaliable.in which they have taken single log file as input. the code for it is given below. Private Sub...
6
by: skyy | last post by:
Hi.. i would like to know how to handle multiple upload files with the cgi module in perl script.. With single file, my $my_cgi new CGI; my $upload_filehandle = cgi ->upload('file1'); my...
7
by: cannunzi | last post by:
I'm trying to import multiple files in one access table all at once but i keep hitting an error stating incorrect file path. The code below works when i change the * to the exact file name but then...
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: 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: 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
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.