0

I am trying to find a way using Access 2003 (Yes I know this is ancient), to search all the sub directories of a folder to determine if a file exists. If it is found it needs to be entered into a sub that turns a button on or off. I would also like to be able to save the path as I would need this button to link to a file. So as a brief explanation, using Access I would like to search a folder a drive which has sub folders which each has their own sub folders. I found many websites, including the following one, but none of the answers seems to work. Loop Through All Subfolders Using VBA

Any help will be appreciated.

Erik A
  • 31,639
  • 12
  • 42
  • 67
Alex Shinn
  • 11
  • 1
  • Welcome to SO! Reading [how to ask a good question](http://stackoverflow.com/help/how-to-ask) will help you get an answer sooner. Remember, this isn't a code-writing service, so post what you've got & somebody will stop by to help you fix it. – FreeMan May 22 '15 at 14:46
  • a hint for your further search - FSO (filesystemobject) – KKowalczyk May 22 '15 at 16:04
  • The question you link to has good answers, so maybe explain why you say they don't work for you. If you've tried to adapt the code, then *post the code you tried* – Tim Williams May 22 '15 at 16:42

1 Answers1

0

`Dim FileSystem As Object Dim HostFolder As String

HostFolder = "C:\"

Set FileSystem = CreateObject("Scripting.FileSystemObject") DoFolder FileSystem.GetFolder(HostFolder)

Sub DoFolder(Folder) Dim SubFolder For Each SubFolder In Folder.SubFolders DoFolder SubFolder Next Dim File For Each File In Folder.Files ' Operate on each file Next End Sub` Gives me an invalid procedure and says the error is in the host folder I used. However, it is the same one I have used for my other codes samples with no problems.

    Public Sub NonRecursiveMethod()
    Dim fso, oFolder, oSubfolder, oFile, queue As Collection

    Set fso = CreateObject("Scripting.FileSystemObject")
    Set queue = New Collection
    queue.Add fso.GetFolder("your folder path variable") 'obviously replace

    Do While queue.Count > 0
        Set oFolder = queue(1)
        queue.Remove 1 'dequeue
        '...insert any folder processing code here...
        For Each oSubfolder In oFolder.SubFolders
            queue.Add oSubfolder 'enqueue
        Next oSubfolder
        For Each oFile In oFolder.Files
            '...insert any file processing code here...
        Next oFile
    Loop

End Sub

Gives me an error because the last next isn't in a for loop according to Access VBA. I found another code that I was able to get working. It is from the website http://www.ammara.com/access_image_faq/recursive_folder_search.html However, this is extremely slow. Takes 30 seconds to run yet, when everything was in a single folder, a simple dir check would be instantaneous. Thanks to those who offered some help.

Alex Shinn
  • 11
  • 1