I actually accomplished this using the FileSystem Object along with a
recordset object to put the text file's contents into the table. My sample
code is below:
Dim streamCurrent As TextStream
Dim fso As FileSystemObject
Dim objFolder As Folder
Dim objTextFilesIncoming As Files
Dim objTextFile As File
Dim rst As DAO.Recordset
Dim sPathText As String
sPathText = "C:\TEMP\VBAPDFTester\Export Text"
Set rst = CurrentDb.OpenRecordset("Table1")
' Loop through the Text files to insert the data into the Table
For Each objTextFile In objTextFilesIncoming
' Open the file as a stream object
Set streamCurrent = objTextFile.OpenAsTextStream(ForReading,
TristateUseDefault)
' Insert the Data from the Text File. Use a Recordset so that quotes
get inserted properly.
rst.AddNew
rst!FullText = streamCurrent.ReadAll
rst.Update
' Close the Text Stream object so we can delete the file after we
inserted the text into the database
streamCurrent.Close
Set streamCurrent = Nothing
'objTextFile.Copy sProcessedFileName
'objTextFile.Delete True
Next
I actually accomplished this using the FileSystem Object along with a
recordset object to put the text file's contents into the table. My
sample code is below:
Dim streamCurrent As TextStream
Dim fso As FileSystemObject
Dim objFolder As Folder
Dim objTextFilesIncoming As Files
Dim objTextFile As File
Dim rst As DAO.Recordset
Dim sPathText As String
sPathText = "C:\TEMP\VBAPDFTester\Export Text"
Set rst = CurrentDb.OpenRecordset("Table1")
' Loop through the Text files to insert the data into the Table
For Each objTextFile In objTextFilesIncoming
' Open the file as a stream object
Set streamCurrent = objTextFile.OpenAsTextStream(ForReading,
TristateUseDefault)
' Insert the Data from the Text File. Use a Recordset so that
quotes get inserted properly.
rst.AddNew
rst!FullText = streamCurrent.ReadAll
rst.Update
' Close the Text Stream object so we can delete the file after we
inserted the text into the database
streamCurrent.Close
Set streamCurrent = Nothing
'objTextFile.Copy sProcessedFileName
'objTextFile.Delete True
Next
Yes, that's a perfectly valid way of doing it. If I'd known exactly
what you had in mind, I'd have written similar code. I would use the
simple VB I/O statements I posted, in preference to the scripting
objects, in the interest of simple efficiency and
reference-independence, but it's no big deal.
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.