Does a File Exist?  | Expert | | Join Date: Apr 2006 Location: Philadelphia
Posts: 5,219
# 1
Oct 29 '07
| |
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. - Function Definition
-
Public Function fFileExists(strName As String) As Boolean
-
Dim strTempString As String
-
-
On Error Resume Next 'will check for an Error condition shortly in the Function assignment
-
-
'Search for the File with 'any' attribute set.
-
strTempString = Dir$(strName, vbHidden Or vbSystem Or vbArchive Or vbReadOnly)
-
-
'If the Length of the FilePath > 0 and no Error condition exists, File exists
-
fFileExists = ((Len(strTempString) > 0) And (Err.Number = 0))
-
-
Err.Clear 'Clear the Error Object
-
-
End Function
-
- Call to the Function
-
Const strPathToFile As String = "C:\Windows\System32\Import_1.txt"
-
-
If fFileExists(strPathToFile) Then
-
'normal processing of the File occurs here
-
Else
-
Dim strBaseFileName As String, strFolderPath As String, Msg As String
-
-
strBaseFileName = Right$(strPathToFile, Len(strPathToFile) - InStrRev(strPathToFile, "\"))
-
strFolderPath = Left(strPathToFile, InStrRev(strPathToFile, "\"))
-
-
Msg = strBaseFileName & " was not found in " & strFolderPath & vbCrLf & vbCrLf
-
Msg = Msg & "Please verify your FileName and/or Path, then try again."
-
MsgBox Msg, vbCritical, "Invalid Path"
-
End If
-
- Custom Error Message (Text only) if File and/or Path not found
-
Import_1.txt was not found in C:\Windows\System32\
-
-
Please verify your FileName and/or Path, then try again.
-
- Special considerations
- 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.
- 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.
|  | Expert | | Join Date: Jun 2007 Location: Israel
Posts: 2,584
# 2
Oct 29 '07
| | | re: Does a File Exist?
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
|  | Expert | | Join Date: Apr 2006 Location: Philadelphia
Posts: 5,219
# 3
Oct 30 '07
| | | re: Does a File Exist? Quote:
Originally Posted by FishVal 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.
|  | Similar Microsoft Access / VBA bytes | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,510 network members.
|