Page 1 of 3 123 LastLast
Results 1 to 15 of 37
  1. #1
    Join Date
    Dec 2004
    Location
    Madison, WI
    Posts
    3,926

    Unanswered: Dir function - List folders

    I'm having a little difficulty trying to populate a listbox with folder contents. I can't seem to get it to list exactly the way I want it to.

    I have 2 listboxes (i.e. ListFolders and ListFiles)
    What I want is the ListFolders listbox to look like this:
    (I DON'T want it to show filenames in ListFolders listbox - Just Folders)

    .
    ..
    C:\Folder1
    C:\Folder2
    C:\Folder2\SubFolder1
    C:\Folder2\SubFolder1\SubSubFolder
    and so on.

    When the user clicks on c:\Folder2\SubFolder1, a 2nd listbox (i.e. ListFiles) then needs to show just the *.mdb file names in that folder. Once the user clicks on a specific *.mdb file, it will then launch that *.mdb file.

    If the User clicks on the .. I want it to go back to where the user can select the C: drive, D: drive, S: drive, or U: drive. When the user clicks on the S: drive, then it looks like this (same type of view as above):
    .
    ..
    S:\Folder1
    S:\Folder1\SubFolder
    S:\Folder1\SubFolder\SubSubFolder
    S:\Folder1\SubFolder2
    S:\Folder1\SubFolder2\SubSubFolder

    and so on.

    I don't want to use the CommonDialog box - I want the listboxes to be populated instead. I tried working with the following code but I can't seem to get it to view the way I want to:

    txtLookin = "C:\"
    Dim varFNames As Variant
    Dim i As Integer
    Me.ListFolders.RowSource = vbNullString
    With Application.FileSearch
    .NewSearch
    If Not IsNull(txtLookIn) Then
    .LookIn = txtLookIn
    End If
    .SearchSubFolders = True
    Call .Execute(1, 1, False)
    For Each varItem In .FoundFiles
    Me.ListFolders.AddItem varItem
    Next varItem
    End With

    and...

    ' Display the names in C:\ that represent directories.
    MyPath = "c:\" ' Set the path.
    Dim varFolders As Variant
    MyName = Dir$(MyPath & "\*.*", vbDirectory) ' Retrieve the first entry.
    Do While MyName <> "" ' Start the loop.
    ' Ignore the current directory and the encompassing directory.
    If MyName <> "." And MyName <> ".." Then
    ' Use bitwise comparison to make sure MyName is a directory.
    If (GetAttr(MyPath & MyName) And vbDirectory) = vbDirectory Then
    varFolders = varFolders & MyName & ";"
    End If ' it represents a directory.
    End If
    MyName = Dir ' Get next entry.
    Loop
    Me.ListFolders.RowSource = varFolders

    I'm almost wondering if I should possibly look at some of the activeX controls. I'd appreciate any help or links anyone can give me.
    Expert Database Programming
    MSAccess since 1.0, SQL Server since 6.5, Visual Basic (5.0, 6.0)

  2. #2
    Join Date
    Feb 2004
    Location
    One Flump in One Place
    Posts
    14,912
    Sorry Paul but I'm not clear which bit you are struggling with. Getting the lists of folders & sub folders or displaying them in the listbox?
    Testimonial:
    pootle flump
    ur codings are working excelent.

  3. #3
    Join Date
    Dec 2004
    Location
    Madison, WI
    Posts
    3,926
    Displaying the folders correctly in the listbox: ListFolders like the example (so actually both). I can't seem to get the listbox to display like in my example.
    Last edited by pkstormy; 06-12-07 at 17:31.
    Expert Database Programming
    MSAccess since 1.0, SQL Server since 6.5, Visual Basic (5.0, 6.0)

  4. #4
    Join Date
    Feb 2004
    Location
    One Flump in One Place
    Posts
    14,912
    I guess then you just want to sort alphabetically. I tried a couple of things but they did not work. Tried googling sort array vba - http://www.source-code.biz/snippets/vbasic/1.htm.
    Stuff like that may be an option....
    Testimonial:
    pootle flump
    ur codings are working excelent.

  5. #5
    Join Date
    Dec 2004
    Location
    Madison, WI
    Posts
    3,926
    If I utilize the CommonDialog activeX control on the form, can I somehow set a listbox rowsource to the commondialog activex control? Or can I possibly utilize the Treeview activeX control?
    Expert Database Programming
    MSAccess since 1.0, SQL Server since 6.5, Visual Basic (5.0, 6.0)

  6. #6
    Join Date
    Feb 2004
    Location
    One Flump in One Place
    Posts
    14,912
    Dunno.

    I would go more "native" than that. At worst I would populate a table and assign that as the row source with an order by clause. Might not be sexy but will be gotcha free.
    Testimonial:
    pootle flump
    ur codings are working excelent.

  7. #7
    Join Date
    Dec 2004
    Location
    Madison, WI
    Posts
    3,926
    The commondialog activex control has the look I want (and I've used this often) but I want to somehow "embed" this into the form instead of having a separate popup box show. I'm not quite sure how to do it though.
    Expert Database Programming
    MSAccess since 1.0, SQL Server since 6.5, Visual Basic (5.0, 6.0)

  8. #8
    Join Date
    Feb 2004
    Location
    Chicago, IL
    Posts
    1,312
    This was a little harder than I thought at first. Here is some code that will find all of the subfolders in a given folder. You will probably have to modify it because if the subfolders get too deep and/or too numerous then a string varible might not be able to hold all of it. But here is what I have:

    Code:
    Public Sub AddFolders(strPath As String, _
                          strFolderList As String, _
                          strSubFolders() As String, _
                          lngUpperBound As Long)
                          
    
        Dim strFilename As String
        Dim lngFolderCount As Long
        Dim lngCount As Long
        Dim strMoreSubFolders() As String
        
        'strPath = strPath & "\*.*"
        strFilename = Dir(strPath, vbDirectory)
        lngCount = 0
        
        While strFilename <> ""
        
            If IsFolder(strPath, strFilename) Then
                            
                ReDim Preserve strSubFolders(lngUpperBound)
                strFolderList = strFolderList & strPath & strFilename & "\;"
                strSubFolders(lngUpperBound) = strPath & strFilename & "\"
                lngFolderCount = lngFolderCount + 1
                lngUpperBound = lngUpperBound + 1
                
            End If
            
            strFilename = Dir()
            
        Wend
            
        If lngFolderCount <> 0 Then
        
            For lngCount = 0 To lngFolderCount - 1
                
                AddFolders strSubFolders(lngCount), strFolderList, strMoreSubFolders(), 0
                
            Next lngCount
            
        End If
                
    End Sub
    
    Public Function IsFolder(strPath As String, _
                             strFolder As String) As Boolean
    
        'If an error occurs, assume that it is because the folder doesn't exist
        On Error GoTo ErrorHandler
        
        If (GetAttr(strPath & strFolder) And vbDirectory) = vbDirectory Then
            
            If IsValidFolder(strFolder) Then
            
                IsFolder = True
                
            End If
            
        End If
        
        Exit Function
        
    ErrorHandler:
    
        IsFolder = False
        
    End Function
    
    Public Function IsValidFolder(strFolder As String) As Boolean
        
        'Check to make sure it is not '.' or '..'
        
        Select Case strFolder
            
            Case ".", ".."
                
                IsValidFolder = False
            
            Case Else
                
                IsValidFolder = True
            
        End Select
        
    End Function
    Public Sub CreateFolders()
    
        Dim strFolders() As String
        Dim strFolderList As String
        
        strFolderList = "C:\Temp\;"
        
        AddFolders "C:\Temp\", strFolderList, strFolders(), 0
        MsgBox strFolderList
        
    End Sub
    Use the CreateFolders procedure to create a string that can be used in a Value List of a listbox. My Temp folder had a bunch of sub folders.

  9. #9
    Join Date
    Dec 2004
    Location
    Madison, WI
    Posts
    3,926
    Yes - I thought it was easy at first but it ended up being more difficult than I thought. Thank you for the code - I'll give it a try.
    Expert Database Programming
    MSAccess since 1.0, SQL Server since 6.5, Visual Basic (5.0, 6.0)

  10. #10
    Join Date
    Jan 2007
    Location
    UK
    Posts
    11,434
    Provided Answers: 10
    I know you want to embed it into your forms but what's wrong with simply using the FSO? You can "customize" the dialog boxes to only show .mdb files etc.
    I have plenty of example code if you want - just ask
    George
    Home | Blog

  11. #11
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    441
    Be aware that .FileSearch is removed in the 2007 version.

    For other approaches, here's Allen Brownes http://allenbrowne.com/ser-59.htm filling a listbox using Dir. Douglas Steele has a great article on different methods of working with the filesystem - (Dir, FSO and API) also with timing comparisions, check out http://advisor.com/doc/16279.

    Now, they're using Collection. For easy sorting and filtering, I'd considered stuffing the info into a disconnected ADO recordset, which can be assigned directly to the listbox (starting with the 2002 version, I think, which, by looking at your code, shouldn't be a problem), then use .Filter/.Sort and reassign...

    I don't use ActiveX controls at all, but I think the ListView control might be an option for this - dunno if it has the capability of being assigned an ADO recordset, though.
    Roy-Vidar

  12. #12
    Join Date
    Feb 2004
    Location
    One Flump in One Place
    Posts
    14,912
    Quote Originally Posted by RoyVidar
    Now, they're using Collection. For easy sorting and filtering, I'd considered stuffing the info into a disconnected ADO recordset, which can be assigned directly to the listbox (starting with the 2002 version, I think, which, by looking at your code, shouldn't be a problem), then use .Filter/.Sort and reassign...
    That was an approach I tried but I made an ar$3 of it
    Testimonial:
    pootle flump
    ur codings are working excelent.

  13. #13
    Join Date
    Dec 2004
    Location
    Madison, WI
    Posts
    3,926
    I found the Cdlg.RepCdg activex control which is the exact look I want but I don't know how to code it. There's very little help with this control. Anyone know anything about this activex control?
    Last edited by pkstormy; 06-13-07 at 10:34.
    Expert Database Programming
    MSAccess since 1.0, SQL Server since 6.5, Visual Basic (5.0, 6.0)

  14. #14
    Join Date
    Feb 2004
    Location
    One Flump in One Place
    Posts
    14,912
    I really don't think activeX controls are the way to go here. The filesearch stuff is all a red herring. Unless I am missing something, all you really want to do is order the contents of an array or value list listbox.
    Testimonial:
    pootle flump
    ur codings are working excelent.

  15. #15
    Join Date
    Jan 2007
    Location
    UK
    Posts
    11,434
    Provided Answers: 10
    Seriously, what's wrong with using the FSO?
    Code:
    Dim fDialog As Office.FileDialog
    Dim varFile As Variant
    
        'Set up the File Dialog.
        Set fDialog = Application.FileDialog(msoFileDialogFilePicker)
        With fDialog
            'Set the title of the dialog box.
            .Title = "Please select the file"
            
            'Sets default directory
            .InitialFileName = "C:\Documents and Settings\" & Environ("Username") & "\My Documents"
            'Sets the default icon view
            .InitialView = msoFileDialogViewDetails
            'Allow the selection of a single file only
            .AllowMultiSelect = False
            
            'Clear out the current filters, and add our own.
            .Filters.Clear
            .Filters.Add "Microsofrt Access Database", "*.mdb"
            .Filers.Add "All files", "*.*"
            
            'Show the dialog box. If the .Show method returns True, the
            'user picked at least one file. If the .Show method returns
            'False, the user clicked Cancel.
            If .Show = True Then
                'Loop through each file selected and add it to our list box.
                For Each varFile In .SelectedItems
                    MsgBox varFile
                Next
            End If
        End With
    George
    Home | Blog

Posting Permissions

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