Importing Text Files

J

Jessie

Hello.

Is there a way to import all .txt files in a specific
folder without typing the names specifically?

Jessie
 
J

John Nurick

Hi Jessie,

This sample code has been posted by my fellow MVP Joe Fallon in answer
to similar questions:

How to Import all Files in a Folder:

Private Sub btnImportAllFiles_Click()
'procedure to import all files in a directory and delete them.
'assumes they are all the correct format for an ASCII delimited import.
Dim strfile As String

ChDir ("c:\MyFiles")
strfile = Dir("FileName*.*")
Do While Len(strfile) > 0
DoCmd.TransferText acImportDelim, "ImportSpecName", _
"AccessTableName", "c:\MyFiles\" & strfile, True
'delete the file (consider moving
'it to an Archive folder instead.)
Kill "c:\MyFiles\" & strfile
strfile = Dir
Loop

End Sub
 
J

JSand42737

"Jessie" said:
Hello.

Is there a way to import all .txt files in a specific
folder without typing the names specifically?

Jessie

Jessie

You can use the Dir function to loop through a folder and get all of the file
names. Below is a very basic example of this:


Public Sub sImportTextFiles(strFolder As String)
On Error GoTo E_Handle
Dim strFile As String
If Right(strFolder, 1) <> "\" Then strFolder = strFolder & "\"
strFile = Dir(strFolder, vbNormal)
Do Until strFile = ""
' Import files here by using DoCmd.TransferText or VBA File I/O Operations
strFile = Dir
Loop
sExit:
On Error Resume Next
Exit Sub
E_Handle:
MsgBox Err.Description & vbCrLf & "sImportTextFiles", vbOKOnly +
vbCritical, "Error: " & Err.Number
Resume sExit
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top