Importing several Text files

M

Maracay

Hi guys,

In a directory I have 50 txt files with data, I would like to know if there
is a way to open all the files and load the data, basically what I want is to
click a command bottom and load all the txt files in the directory, the data
in the .txt files have the same structure.

Thanks
 
T

tina

create an Import Specification, if you haven't already done so. then use the
Dir function and the TransferText command to loop through each file in the
directory and import it. somthing along the lines of

Dim str As String

str = "C:\MyFolderName\*.txt"

str = dir(str)

Do
DoCmd.TransferText acImportDelim, _
"SpecificationName", "MyTableName", str
str = dir
Loop Until str = ""

read up on Dir and TransferText in VBA Help, so you'll understand how they
work.

hth
 
P

Piet Linden

Hi guys,

In a directory I have 50 txt files with data, I would like to know if there
is a way to open all the files and load the data, basically what I want is to
click a command bottom and load all the txt files in the directory, the data
in the .txt files have the same structure.

Thanks

To make it a bit more flexible, you could incorporate the BrowseFolder
API code from here:
http://www.mvps.org/access/api/api0002.htm

and then do something like this:

Private Sub cmdGetFolder_Click()
Dim strFile As String
Dim strDirectory As String

'prompts user for folder using the BrowseFolder API
strDirectory = BrowseFolder("Choose a folder")

strFile = Dir(strDirectory & "\*.txt")

Do While strFile <> vbNullString
'DoCmd.TransferText acImportDelim, "MyTextImportSpec",
"DestinationTable", strFile, False
Debug.Print strFile
strFile = Dir()
Loop
MsgBox "Finished"
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