I was reviewing all the posts from yesterday. I didn't get the nomenclature correct when I was describing the problem. There Is a folder that contains CSV files. Each CSV file can have one or more "rows" of data. I need to get each of the CSV files into one table table.What does that mean? A file can't contain another file. Are you saying the file has multiple rows? #17 probably solves your problem. It looks like pseudo code and so probably isn't tested. Try it and see what happens.
Basically the process is to browse through the folder with the files and for each .csv in the folder run an append query that to append the contents of the file to the specified table. The sample code uses an unusual method that embeds the text file name in the query rather than linking the text file and then running a more common table to table append query.
Example:
A partial picture of the contents of the folder
I am looking at this code provided by ebs17 as a possible solution
Code:
Sub Import_CSV()
Dim db As DAO.Database
Dim sPath As String
Dim sFile As String
Dim sSQL As String
Set db = CurrentDb
sPath = "X:\AnyWherde\Importfiles\"
sFile = Dir(sPath & "*.csv")
Do While sFile > vbNullString
'Debug.Print sFile
sSQL = "INSERT INTO TableX (Field1, Field2, Field3)" & _
" SELECT T.FieldA, T.FieldB, T.FieldC" & _
" FROM [Text;DSN=NameSpecification;FMT=Delimited;HDR=yes;IMEX=2;CharacterSet=850;" & _
"DATABASE=" & sPath & "].[" & sFile & "] AS T"
db.Execute sSQL, dbFailOnError
sFile = Dir
Loop
Set db = Nothing
End Sub
But, I was wondering that since all the CSV files are exactly alike, and the table already has column headers that match the CSV files, couldn't a solution look something like this:
Code:
Option Compare Database
Option Explicit
Dim rs As DAO.Database
Private Sub AppenCallCSVFiles_Click()
rs.MoveFirst
Do Until rs.EOF
DoCmd.TransferText acImportDelim, , "LogExpanded", "C:\PathToTheCVSFiles"", False"
rs.MoveNext
Loop
rs.Close
Set rs = Nothing
End Sub
My immediate problem is the container the code is in: Dim rs As DAO.Database is wrong, and I think I remember you saying that I shouldn't use a recordset. Been reading and still don't have that part straight. Any advice would be appreciated.
I just realized what you said earlier that in the second bit of code that the record being read would never change.
I am going to step back and let the fog clear.
Hope I didn't stretch your patients too much.
Thanks Pat.
Last edited: