Validating an entered directory/filename

  • Thread starter Thread starter Chris
  • Start date Start date
C

Chris

How would one validate a user-entered directory/filename? I know the DIR
function exists, but can't quite get my head around how to employ it to test
for a valid directory string (in one field), a valid filename in that
directory (another field), or a combined value?

Chris
 
Use the Application.FileSearch Method. e.g.

With Application.FileSearch
.Filename = "yourfilename"
.LookIn = "yourdirectory"
If .Execute() > 0 Then
<the file exists>
End If
End With

Hope This Helps
Gerald Stanley MCSD
 
Dir$() returns a non-zero length string if the file exists, so use:
If Dir$(Me.txtFolder & Me.txtFilename) > 0 Then

Here are a couple of functions that demonstrate how to test if a file/folder
exists:

Public Function FolderExists(varPath As Variant) As Boolean
On Error Resume Next
If Len(varPath) > 0& Then
FolderExists = (Len(Dir$(varPath, vbDirectory)) > 0&)
End If
End Function

Public Function FileExists(varFullPath As Variant) As Boolean
On Error Resume Next
If Len(varFullPath) > 0& Then
FileExists = (Len(Dir$(varFullPath)) > 0&)
End If
End Function
 
Back
Top