Writing to Sequential Files with Microsoft Access VBA and the FileSystemObject

This is the companion code listing and description for the video:
Writing to Sequential Files with Microsoft Access VBA:
The FileSystemObject

In this article we will look at two methods of writing to text files by using the FileSystemObject.  Again, I’ve built a form to provide input to our text files:

WriteFormFSO

The top textbox will determine what folder will hold our new text file, and the textbox below that will provide the name for the file.  The next two textboxes will provide the strings we will write.  The left button will use the FileSystemObject’s OpenTextFile method.  The right button will use the FileSystemObject’s CreateTextFile method and the File object’s OpenAsTextStream method.

The “Write It ‘1’!” button:

After making sure we have folder and file name, we assemble a complete file and path name using string concatenation and storing that in the filename variable.  Next we create our FileSystemObject called fso.  The next statement opens or creates our text file, the format is:

This statement yields a TextStreamObject you can write to.  The parameters are:

  • filename:  the file to create or open
  • iomode:  ForReading, ForWriting or ForAppending
  • create:  boolean value to determine whether to create the file if it does not exist
  • format:  TristateUseDefault, TristateTrue or TristateFalse

If you open an existing file “ForWriting”, you will destroy the data that was in the original file, and replace it with the data from your current session.  If you open an existing file “ForAppending”, you will add to the end of the file in our current session.  You can open a new file “ForAppending” just fine.

TristateTrue set the format of the text file as Unicode; TristateFalse set the format of the file to be ASCII.  TristateUseDefault uses the system default.

After we have opened or created our text file, we write to it using the WriteLine method.  The WriteLine method takes a string variable as input and writes the variable content to the file and then moves the record pointer to the next record in the file – it gives you a new line automatically.

The above code and form produces a text file with this content:

The “Write It ‘2’!” button:

The button on the right uses the FileSystemObject’s CreateTextFile method and the File object’s OpenAsTextStream method.  The code is below:

This code starts exactly as the previous code did.  After assembling the name for our new file, we try to create a new text file by using the FileSystemObject’s CreateTextFile method.  If the file you are trying to create already exists, this will destroy it and create a new empty file.  I have put this statement inside an “if” that tests if the file already exists.  Next, we get a reference to the file and set it equal to the object named “fil”.

Next, we create a TextStream object by calling the file object’s OpenAsTextStream method.  This method takes the same iomode and format arguments as the previous code did.  After getting a TextStream object, we call it’s Write method.  The Write method does not advance the record pointer, so you will continue to write to the same “line” or record until you write a carriage return character.  The above code and form produce a file containing this:

 

Posted in Code Listings for Videos.

5 Comments

  1. Facebook, , Stumble – Upon and Squidoo are just some of
    the free web services that are widely used by both amateurs and beginners, as well as by Internet marketing professionals.
    Putting the two of them together – Debbie Russell – I learned that there are 195 of us in the United States.

    – Host – Gator is a very reliable service that has been around for many years, it
    has a 99.

  2. Building an inexpensive chicken coop is absolutely essential for your hens.
    This high cost is the main reason people are making
    their own coops instead of buying one. It’s better to have
    a chicken coop that’s too big than a coop that’s too small.

  3. After I initially left a comment I seem to have clicked the -Notify
    me when new comments are added- checkbox and now whenever a
    comment is added I get four emails with the exact same
    comment. Perhaps there is a means you can remove me from that service?
    Cheers!

Leave a Reply

Your email address will not be published.