Introducing the FileSystemObject
The FileSystemObject class contains the methods and properties required when interacting with your computers file system and is also 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.
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, follow the example below and remove any declarations using the System library, creating instead variable of type Object.
'replace an variables creating an instance of 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 by using the CreateObject method
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.