Check to see if Doc exists

  • Thread starter Thread starter Ryan
  • Start date Start date
R

Ryan

I am sure this question has come up a few times before but
i was wondering if someone could tell me how to check to
see if a particular file exists. If this file doesn't
exist I would like to prompt the user to create this file.

Can anyone help?

Thanks,
Ryan
 
Ryan,

If IsNull(Dir(strFilePath)) then
If MsgBox("Would you like to create this file", vbQuestion+vbYesNo) =
vbYes then
' code to create file here
Else
' exit? alert?
End If
Else
' exit? alert?
End If

HTH.

- Assaf
 
Assaf said:
Ryan,

If IsNull(Dir(strFilePath)) then
If MsgBox("Would you like to create this file",
vbQuestion+vbYesNo) = vbYes then
' code to create file here
Else
' exit? alert?
End If
Else
' exit? alert?
End If

There's a small error in that. Dir() returns a zero-length string
(a.k.a. a "null string") if the file doesnt exist, not a Null. So you
have to say

If Dir(strFilePath) = vbNullString Then

Or

If Len(Dir(strFilePath)) = 0 Then

Or

If Dir(strFilePath) = "" Then

I prefer If Len(Dir(...)) = 0 for this sort of thing.
 
ya! I was so pleased to find out you could use WSH within access! no need to
use any of the barely existent VBA filesystem code. WSH makes this stuff a
breeze.

For the orginal poster... you have to add a reference to WSH (windows script
host) to be able to use the fs.FileExists("a;kdjj") method. And you have to
dim the appropriate variables first.
 
djc said:
ya! I was so pleased to find out you could use WSH within access! no
need to use any of the barely existent VBA filesystem code. WSH makes
this stuff a breeze.

For the orginal poster... you have to add a reference to WSH (windows
script host) to be able to use the fs.FileExists("a;kdjj") method.
And you have to dim the appropriate variables first.

But why add all the overhead of Windows Scripting to do something that
is built into VBA? On top of which, if you're distributing your
application, every external reference you add is one that can be broken;
plus there are system administrators out there who disable scripting
within their domains as a security risk.
 
Seems to work fine without explicitly referencing a WSH library. Either the
CreateObject call sets up a reference or maybe Access or VBA take care of it
behind the scenes. All you need is:

Dim fs
Set fs = CreateObject("Scripting.FileSystemObject")

- Assaf
 
Back
Top