Link to home
Start Free TrialLog in
Avatar of ChandlerBound
ChandlerBound

asked on

Search for a directory

I want to determine if a particular directory exists while in an Access form (2003).

I am using the below code that works fine but I want to only search for one level of directory not all the sub directories in the folder.  The Main path is P:\Rental Jobs and under that directory is a directory for each job number in our access table  (like P:\Rental jobs\20456.  ) But I dd not want to look for any subfolders beyond the Job Number directory. I am not sure of the different settings available in the code.  Any suggestions ?

Code:
Public Function FindMyFolder(startfolder As String, searchname As String) As String

Static fso As scripting.FileSystemObject
If fso Is Nothing Then Set fso = New scripting.FileSystemObject

Dim folder As scripting.folder
Dim result As String

Set folder = fso.GetFolder(startfolder)

Dim subfolder As scripting.folder
For Each subfolder In folder.SubFolders
  If subfolder.Name = searchname Then
    result = subfolder.path
    GoTo done
  Else
    result = FindMyFolder(subfolder.path, searchname)
    Me.txtSearch = subfolder.path
    MsgBox ("Search Directory: " & txtSearch), vbInformation
    If result <> Empty Then GoTo done
  End If
Next

done:
FindMyFolder = result

End Function
ASKER CERTIFIED SOLUTION
Avatar of Graham Mandeno
Graham Mandeno
Flag of New Zealand image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ChandlerBound
ChandlerBound

ASKER

Worked perfectly and quickly as  my method was way too slow!  Thank you for the quick response.