Hi Sirritys,
I hope I was clearer this time =)
Yep, much clearer!
The easiest method to start with will be to use a hard-coded folder. Once
you get that much working, then you can replace a hard-coded path with API
code that allows you to select the appropriate file(s) within a given folder,
or a folder to process all .txt files. Let's work on the easy part first,
where we will use a hard-coded path, and we will process all text files.
Therefore you should only have valid text files, containing data to import,
saved to the folder.
Add a command button to a form. Name it cmdImportFiles. Add the following
code (untested) to the click event of this form. The code is untested but I
think it will work. I have used named arguments for the TransferText method,
as opposed to positional arguments. You can highlight DoCmd.TransferText with
your mouse and press the F1 button to open context sensitive help on this
topic.
The TransferType argument specifies a delimited text file, such as a comma
separated variable (csv) file. Replace YourTableName with the appropriate
name of your table in the database that you want to import the records into.
This value should be enclosed in quotes, as indicated below. Set the
HasFieldNames argument to either True or False, whichever is appropriate for
your text files.
Option Compare Database
Option Explicit
Private Sub cmdImportFiles_Click()
On Error GoTo ProcError
Dim strFolder As String
Dim strFile As String
strFolder = "C:\Temp\" '<---Hard-coded path
' strFolder = CurrentProject.Path & "\" '<---Path is same as .mdb file
strFile = Nz(Dir$(strFolder & "\*.txt"), "")
Do While Len(strFile) > 0
'Debug.Print strFolder & strFile
DoCmd.TransferText TransferType:=acImportDelim, _
SpecificationName:="Table1 import specification", _
TableName:="YourTableName", _
Filename:=strFolder & strFile, HasFieldNames:=True
strFile = Dir$
Loop
ExitProc:
Exit Sub
ProcError:
MsgBox "Error " & Err.Number & ": " & Err.Description, _
vbCritical, "Error in procedure cmdImportFiles_Click..."
Resume ExitProc
End Sub
Click on Debug > Compile ProjectName, where ProjectName is the name of your
VBA project. Hopefully the code will compile without any errors.
If you want to later expand on this code, here is a good article to serve as
a primer:
http://advisor.com/doc/16279
Tom Wickerath
Microsoft Access MVP
http://www.access.qbuilt.com/html/expert_contributors.html
http://www.access.qbuilt.com/html/search.html
__________________________________________