Results 1 to 7 of 7

Thread: Subfolder search

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2005
    Posts
    6

    Red face Subfolder search

    Using this code - is there a way to not have to type the path everytime and be able to search all folders and sub folders for the mdb databases that have the "employees" table? I really need a good example - I am new with VBA and this works fine for me except having to type the path everytime.

    Thanks.

    Private Sub Form_Open(Cancel As Integer)
    Dim strFile As String
    Dim strPath As String

    CurrentDb.Execute "DELETE * FROM tblTables"
    strPath = "x:\dbases_AC\dbases\"
    strFile = Dir(strPath & "*.mdb")

    While strFile <> ""
    CurrentDb.Execute "INSERT INTO tblTables (TableName, DatabaseName) SELECT Name,'" & strPath & strFile & "' FROM MSysObjects IN '" & strPath & strFile & "' WHERE Name = 'employees'"

    strFile = Dir()
    Wend
    Me!Combo0.Requery

    End Sub

  2. #2
    Lively Member
    Join Date
    Jun 2005
    Posts
    112

    Re: Subfolder search

    This should do it for you:

    VB Code:
    1. Function getDirList(startDir As String)
    2.  
    3.     Dim foundDirs()
    4.     Dim subDir As String
    5.     Dim dirCount As Integer
    6.  
    7.     subDir = Dir(startDir, vbDirectory + vbHidden)
    8.     Do While subDir <> ""
    9.         If subDir <> "." And subDir <> ".." Then
    10.             If (GetAttr(startDir & subDir) And vbDirectory) = vbDirectory Then
    11.                 dirCount = dirCount + 1
    12.                 ReDim Preserve foundDirs(dirCount)
    13.                 foundDirs(dirCount) = startDir & subDir & "\"
    14.             End If
    15.         End If
    16.         subDir = Dir
    17.     Loop
    18.  
    19.     If dirCount = 0 Then
    20.         getDirList = False
    21.     Else
    22.         getDirList = foundDirs
    23.     End If
    24.        
    25. End Function
    26.  
    27.  
    28. Private Sub Form_Open(Cancel As Integer)
    29.  
    30.     Call Old_Form_Open("x:\dbases_AC\dbases\")
    31.  
    32. End Sub
    33.  
    34. Sub Old_Form_Open(strPath As String)
    35.  
    36.     Dim strFile As String
    37.     Dim varDirs As Variant
    38.     Dim i As Integer
    39.  
    40.     CurrentDb.Execute "DELETE * FROM tblTables"
    41.     strFile = Dir(strPath & "*.mdb")
    42.     varDirs = getDirList(strPath)
    43.  
    44.     While strFile <> ""
    45.         CurrentDb.Execute "INSERT INTO tblTables (TableName, DatabaseName) SELECT Name,'" & strPath & strFile & "' FROM MSysObjects IN '" & strPath & strFile & "' WHERE Name = 'employees'"
    46.  
    47.         strFile = Dir()
    48.     Wend
    49.     If IsArray(varDirs) Then
    50.         For i = 1 To UBound(varDirs)
    51.             Call Old_Form_Open(varDirs(i))
    52.         Next i
    53.     End If
    54.     Me!Combo0.Requery
    55.    
    56. End Sub

  3. #3
    Lively Member
    Join Date
    Jun 2005
    Posts
    112

    Re: Subfolder search

    I just reread your post, and this will loop through all mdb files in all subdirectories. It makes no distinction whether or not there is an employees table.

  4. #4

    Thread Starter
    New Member
    Join Date
    Jul 2005
    Posts
    6

    Re: Subfolder search

    Soo Close. I have the code to look for the "Employees" table in the second half - but do I need it in your new code you added and if so can I add it????

  5. #5
    Lively Member
    Join Date
    Jun 2005
    Posts
    112

    Re: Subfolder search

    Didn't see that in your code... I just copied yours. Let er rip, I think it will work.

  6. #6

    Thread Starter
    New Member
    Join Date
    Jul 2005
    Posts
    6

    Re: Subfolder search

    I am getting a "ByRef argument type mismatch" error on this line

    Call Old_Form_Open(varDirs(i))

    I am using Access2000 - if that helps.

    Thanks for staying with me.

  7. #7
    Lively Member
    Join Date
    Jun 2005
    Posts
    112

    Re: Subfolder search

    change to Call Old_Form_Open(CStr(varDirs(i)))

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width