how to declare a fileSystemObject?

  • Thread starter Thread starter everymn
  • Start date Start date
E

everymn

The Access help provides this code snip for this but doesn't describe
how it is to be declared. I'm trying to use it in a module funtion.
When I try to dim it neither scripting nor FilesystemObject are
available from the autocomplete list. Could someone please explain to
me how I can declare these variables?

Thank You!


Set fs = CreateObject("Scripting.FileSystemObject")
Set a = fs.CreateTextFile("c:\testfile.txt", True)
a.WriteLine("This is a test.")
a.Close
 
Hi,
if you want to use early binding with the FileSystem object then set a
reference to the Microsoft Scripting Runtime library within the VBA editor
(tools--references...).
Then you could declaire it as:

Dim fs As New Scripting.FileSystemObject

Instead of using the CreateObject() method.
HTH
Good luck
 
It's far better to use

Dim fs As Scripting.FileSystemObject

Set fs = New Scripting.FileSystemObject

with early binding. Declaring with the New keyword can lead to unexpected
results.

In answer to the original post, declare fs and a as Objects:

Dim fs As Object
Dim a As Object

Set fs = CreateObject("Scripting.FileSystemObject")
Set a = fs.CreateTextFile("c:\testfile.txt", True)
a.WriteLine("This is a test.")
a.Close

I feel you should always minimize the number of references in an
application, so as to avoid problems with the References collection when
there are version differences between the user's machine and the developer's
machine.

Of course, there's no reason to use FSO to write a file. You can the same as
what you've got there using VBA:

Dim intFile As Integer

intFile = FreeFile()
Open "C:\testfile.txt" For Output As #intFile
Print #intFile, "This is a test."
Close #intFile
 
Of course, there's no reason to use FSO to write a file.

One thing the FSO offers is reliable network availability detection.
That's the only purpose I ever use it for (and, yes, I use late
binding).
 
David W. Fenton said:
One thing the FSO offers is reliable network availability detection.
That's the only purpose I ever use it for (and, yes, I use late
binding).

I prefer using API calls for that. Seems to me I had to use FSO once to get
the LastModifiedDate for a folder, as I couldn't seem to get that using API
calls.
 
I prefer using API calls for that. Seems to me I had to use FSO
once to get the LastModifiedDate for a folder, as I couldn't seem
to get that using API calls.

But I don't have to declare the APIs to use the function in the FSO.
I just need to instantiate an object. That's easier to do, seems to
me.
 
Back
Top