By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,494 Members | 1,079 Online
Bytes IT Community
Submit an Article
Got Smarts?
Share your bits of IT knowledge by writing an article on Bytes.

Does a File Exist?

ADezii
Expert 5K+
P: 8,669
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
Share this Article
Share on Google+
2 Comments


FishVal
Expert 2.5K+
P: 2,653
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
Expert 5K+
P: 8,669
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