468,727 Members | 1,637 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Share your developer knowledge by writing an article on Bytes.

Does a File Exist?

ADezii
8,800 Expert 8TB
Many times when writing VBA code, we find the need to work with Files in one form or another. Whatever the process may be, we need to be sure that a File is present in a specified location. One Method to guarantee that a File does in fact exist, is to pass the Absolute Path to the File to a Function which will investigate the Path and return a Boolean Value (True/False) indicating whether or not the File exists. The following code segments will do just that. It accepts a single String Argument representing the Path to the File, if it exists, it returns True, and the User can proceed as normal, if not, a Message Box appears indicating to the User that the File does not exist in the location that was passed to it.
  1. Function Definition
    Expand|Select|Wrap|Line Numbers
    1. Public Function fFileExists(strName As String) As Boolean
    2. Dim strTempString As String
    3.  
    4. On Error Resume Next      'will check for an Error condition shortly in the Function assignment
    5.  
    6. 'Search for the File with 'any' attribute set.
    7. strTempString = Dir$(strName, vbHidden Or vbSystem Or vbArchive Or vbReadOnly)
    8.  
    9. 'If the Length of the FilePath > 0 and no Error condition exists, File exists
    10. fFileExists = ((Len(strTempString) > 0) And (Err.Number = 0))
    11.  
    12. Err.Clear     'Clear the Error Object
    13.  
    14. End Function
    15.  
  2. Call to the Function
    Expand|Select|Wrap|Line Numbers
    1. Const strPathToFile As String = "C:\Windows\System32\Import_1.txt"
    2.  
    3. If fFileExists(strPathToFile) Then
    4.   'normal processing of the File occurs here
    5. Else 
    6.   Dim strBaseFileName As String, strFolderPath As String, Msg As String
    7.  
    8.   strBaseFileName = Right$(strPathToFile, Len(strPathToFile) - InStrRev(strPathToFile, "\"))
    9.   strFolderPath = Left(strPathToFile, InStrRev(strPathToFile, "\"))
    10.  
    11.     Msg = strBaseFileName & " was not found in " & strFolderPath & vbCrLf & vbCrLf
    12.     Msg = Msg & "Please verify your FileName and/or Path, then try again."
    13.       MsgBox Msg, vbCritical, "Invalid Path"
    14.  End If
    15.  
  3. Custom Error Message (Text only) if File and/or Path not found

    Expand|Select|Wrap|Line Numbers
    1. Import_1.txt was not found in C:\Windows\System32\
    2.  
    3. Please verify your FileName and/or Path, then try again.
    4.  
  4. Special considerations
    1. If your File exists on a Network Drive, you would probably want to enhance your Error Handling code since the Path and/or File Name may be valid, but there are a host of additional Network related Errors which may occur.
    2. As Access developers, code similar to that listed above should be SOP (Standard Operating Procedure) anytime we attempt to Open, Close, or Manipulate a File in any manner. We should never assume that the File exists, and the Path to it is valid. It would be a bad programming practice to attempt to operate on it prior to verifying its existence.
Oct 29 '07 #1
2 11831
FishVal
2,653 Expert 2GB
A lazy man should take a look at "Microsoft Scripting Runtime" library.

It has class FileSystemObject, which has method FileExists as well as FolderExists and DriveExists and a bunch of other useful methods and properties.

Regards,
Fish
Oct 29 '07 #2
ADezii
8,800 Expert 8TB
A lazy man should take a look at "Microsoft Scripting Runtime" library.

It has class FileSystemObject, which has method FileExists as well as FolderExists and DriveExists and a bunch of other useful methods and properties.

Regards,
Fish
Excellant point, FishVal, thanks. The only advantages that this 'older' approach may have is that it requires no References, and that it's functional across all Access Versions.
Oct 30 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

1 post views Thread by Poppy | last post: by
3 posts views Thread by Olivogt | last post: by
52 posts views Thread by paytam | last post: by
2 posts views Thread by tino | last post: by
3 posts views Thread by Mike | last post: by
8 posts views Thread by Anthony Papillion | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
xarzu
1 post views Thread by xarzu | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.