EziData Solutions - Brisbane's Access Specialists
FileSystemObject Tutorials

Introducing the FileSystemObject

The FileSystemObject class contains the methods and properties required when interacting with your computers file system and is the primary tool we will use to read and write text files. The FileSystemObject is a class within the Microsoft Scripting Runtime library, contained in the scrrun.dll. The FileSystemObject methods and properties

Adding a Reference to the FileSystemObject

To help you get starting using the FileSystemObject it is a good idea to add a reference to the Microsoft Scripting Runtime library in your Access project (using Tools\References on the menu bar). That way you can browse the various methods and properties available in the Object Browser as well as taking advantage of IntelliSense, which helps avoid typos. We will use this method in our examples, however if you are deploying your finished database it is always best to avoid superfluous references, so we recommend you use late binding, which we will demonstrate later in the article.

Late Binding the FileSystemObject Class

As mentioned previously, if you are deploying your database to others, it is best to limit the number of references your database uses. That is unless you have a particularly sadistic attraction to pain and suffering. To use late binding, paste the code below into the relevant section of the examples.

'declare the variables for use with the Scripting library

'Dim fso As New Scripting.FileSystemObject

'Dim fsoStream As Scripting.TextStream


Dim fso As Object

Dim fsoStream As Object

'initialise the FileSystemObject

Set fso = CreateObject("Scripting.FileSystemObject")

You will also need to replace the IOMode constants used in the OpenTextFile function with their integer values.
ForWriting = 2
ForAppending = 8
ForReading = 1

After you have done this, remove the reference to the Microsoft Scripting Library and make sure your project compiles. Now you are free to explore the many facets of this powerful object.

Using the FileSystemObject

Getting Information About Your Drives

Getting Information About Your Folders

Getting Information About Your Files

Working with Text Files

Working with Text Files

Reading from Text Files