Read / Write To Text Files (1 Viewer)

Not open for further replies.


Jun 22, 2007
The attached example shows how you can get data into and out of a text file by using the File System Object and variations of the Read/Write methods.

This is probably the most common method used, as it writes in append mode, and not in write mode, and it also reads an entire file instead of only the first line...

I will post some streaming examples once in a while here. Most of it will be code that I use or have used.

Code to create a (comma) delimited text file spreadsheet of all files in a windows folder:
    Dim fs, f, f1, fc, s, oFile, folderspec
    folderspec = "FOLDER ADDRESS"
    Set fs = CreateObject("Scripting.FileSystemObject")
    Set f = fs.GetFolder(folderspec)
    Set fc = f.Files
    For Each f1 In fc

s = s & Left(f1.Name, (Len(f1.Name) - 4)) & "," & Round(f1.Size / 1048576, 0) & "mb"
s = s & vbCrLf

   Set oFile = fs.createtextfile("COMPLETE ADDRESS OF NEW TEXT FILE", True)

   oFile.writeline s
    Set f = Nothing
    Set fc = Nothing
    Set fs = Nothing
    Set oFile = Nothing

I just ran into a text file that had no definite delimiter and it wasn't fixed width either (don't ask me where I got it). Interesting format:


My fix-it solution to make it importable into Access:
    Dim fs, s, oFile, strpart, strsplit, retstring
    Set fs = CreateObject("Scripting.FileSystemObject")
   Set oFile = fs.opentextfile("FILE'S PATH", 1, False)

   s =
    For Each strpart In Split(s, vbCrLf)
        If InStr(strpart, "-----") = 0 Then
            For Each strsplit In Split(strpart, "|")
                retstring = retstring & Trim(strsplit) & ","
            Next strsplit
                    retstring = Trim( _
                                Right( _
                                Replace( _
                                    retstring, ",,", ""), _
                                    (Len(retstring) - 1))) & vbCrLf
        End If
    Next strpart
   Set oFile = fs.createtextfile("FILE'S PATH", True)

   oFile.writeline retstring
    Set fs = Nothing
    Set oFile = Nothing


Last edited:
Hi ajetrumpet,

I'm looking to learn about how to write code that reads and writes to text files, I came across your thread and downloaded your zip file, unfortunately I am using a lower version and therefore have been unable to view the contents of your database.

I thought I'd ask, would it be possible for you to provided a way of accessing the information in your database so that I can learn how to read and write to text files.

I'm using Access 2000.

your assistance would be appreciated.

John Lee

I posted a 2000 version. Good luck.
Not open for further replies.

Users who are viewing this thread

Top Bottom