Importing several Text files

  • Thread starter Thread starter Maracay
  • Start date Start date
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
 
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
 
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
 
Back
Top