Import text files in directory

detrie

Registered User.
Local time
Yesterday, 23:10
Joined
Feb 9, 2006
Messages
113
I have a table - tblText. It has three columns (tblTextID, strFileName and memText)

I need to import the contents of multiple text files from (c:\TextFiles\) into tblText>memeText and place the file name into strFileName (one text file = one table record).

Can anyone offer any help?

TIA
Detrie
 
I have a table - tblText. It has three columns (tblTextID, strFileName and memText)

I need to import the contents of multiple text files from (c:\TextFiles\) into tblText>memeText and place the file name into strFileName (one text file = one table record).

Can anyone offer any help?

TIA
Detrie

Something like below...

You'll need to set a reference to the "Microsoft Scripting Runtime" if you want to declare all the filesystem objects, otherwise declare them as objects

Public Sub importtext()
Dim fso As FileSystemObject, fo As Folder, fi As File, ts As TextStream 'All the filesystem objects you need
Dim rs As Recordset

Set fso = New FileSystemObject
Set fo = fso.GetFolder("c:\textfiles") 'set the folder object to the folder you want

' open a recordset here

For Each fi In fo.Files ' iterate through the files in the folder

'check the filetype
If fi.Type = "Text Document" Then

Set ts = fi.OpenAsTextStream(ForReading)
'add a record to the rs
rs("strFilename") = fi.Name
rs("memtext") = ts.ReadAll
'save the record​

End If​

Next
'close\ set to nothing your objects​

End Sub
 
Thanks for your reply "jib".
I am having trouble with the "open recordset" can you give me an example of what this should look like?
Detrie
 

Users who are viewing this thread

Back
Top Bottom