TreeView of directory (1 Viewer)

supercharge

Registered User.
Local time
Yesterday, 23:59
Joined
Jun 10, 2005
Messages
215
Hi All,

I've been trying to use TreeView control to display my computer's directories and folders just like Windows Explorer does.

Thanks to Popeye for reposting all the Treeview samples (samples posted before the site was hacked). Most or all of these samples, TreeView is used to display data out of tables. Mr. Ghuson posted a great example for browsing directory but it pops up a separate windows for browsing.

Now I'd like to somehow combine the two into one; browsing thru directories and folders using TreeView Control. Yes that I can browse and save all paths and directories to different tables and add them to the TreeView control but I'm trying to stay away from saving data to tables first.

Can this be accomplished? Can Windows Explorer be somehow embeded on an Access form or called? Any ideas, experts out there?

Thanks all in advance.
 

FoFa

Registered User.
Local time
Today, 01:59
Joined
Jan 29, 2003
Messages
3,672
You can use FileSystem Object to transverse your hard drive and load a treeview control, but if you try to load the entire thing all at once it maybe a little slow, and if you load as required, there is a delay from click to display.
What about embedding IE and using it's FILE functionality to do it?
Just throwing out some thoughts is all.
 

supercharge

Registered User.
Local time
Yesterday, 23:59
Joined
Jun 10, 2005
Messages
215
I'm not sure how IE's FILE functionality will do the trick. Can you please give details?

Thanks
 

ghudson

Registered User.
Local time
Today, 02:59
Joined
Jun 8, 2002
Messages
6,195
Not sure if this is what you are looking for but check out the Explorer download for it does allow you to browse and open files from within an Access form. Nothing fancy but it works. ;)

Download Index - E's
Explorer
 

supercharge

Registered User.
Local time
Yesterday, 23:59
Joined
Jun 10, 2005
Messages
215
Thanks Ghuson for the pointers. I tried that already. It used two listboxes to display parents and children. It would be nice to use just one (TreeView type). Nice try though!

I wonder if any Access MVP outthere can give some thoughts to this. I don't think it's not doable, just difficult.

Thanks.
 

supercharge

Registered User.
Local time
Yesterday, 23:59
Joined
Jun 10, 2005
Messages
215
More information: Similar to Windows Explorer, this code uses a recursive technique to read parent nodes from a table named Tree Table, and call the function AddChildren from the same table. The Function AddList populates the Listview control in the right pane, and the click event of the Listview Item opens the associated form/reports from the table named TreeSub.

Thanks FoFa but again, it displays data out of tables which I'm trying to stay away from doing.
 

Bodisathva

Registered User.
Local time
Today, 02:59
Joined
Oct 4, 2005
Messages
1,274
I was looking for the same thing...just decided to save time and write it. The following will recursively populate a treeview control on your form (tvwList in this case) using all subdirectories and files from within a start directory that you specify from a file dialog. I didn't add the file Icons, but that's for later. I originally designed it for my MP3 tool, but I'm sure you can figure out how to modify it as needed: ( And yes FoFa if you turn this thing loose on a high level directory, it will take some time...:eek: )

Code:
Private WithEvents tvw As MSComctlLib.TreeView

Private Sub cmdLoad_Click()
    Dim fileDialog As fileDialog
    
    Set fileDialog = Application.fileDialog(msoFileDialogFolderPicker)
    With fileDialog
        .AllowMultiSelect = False
        .Title = "Choose Directory"
        If (.Show) Then loadList (.SelectedItems(1))
        Set tvw = tvwList.Object
        tvw.Nodes(1).Expanded = True
    End With
End Sub

Public Function loadList(topFolder As String)
On Error GoTo Handler
    Dim fsObject As New FileSystemObject
    Dim fsFolder As Folder
    Dim fsFolders As Folder
    Dim subFolders As Folders
    Dim subFolder As Folder
    Dim fsFile As File
    Dim fsFiles As Files
    Dim message As String
    Dim parentFolder As String
    Dim targetLoc As Integer
    
    Set tvw = Me.tvwList.Object
    With tvw
    Set fsFolder = fsObject.GetFolder(topFolder)
    
    If tvw.Nodes.Count < 1 Then
        'initialize first node
        .Nodes.Add , , fsFolder.Path, fsFolder.Name
    End If
        
    Set subFolders = fsFolder.subFolders
    For Each subFolder In subFolders
        For targetLoc = Len(subFolder.Path) To 1 Step -1
            If Mid(subFolder.Path, targetLoc, 1) = "\" Then
                parentFolder = Left(subFolder.Path, targetLoc - 1)
                targetLoc = 1
            End If
        Next targetLoc
        .Nodes.Add parentFolder, tvwChild, subFolder.Path, subFolder.Name
        loadList (subFolder.Path)
    Next subFolder
    
    Set fsFiles = fsFolder.Files
    For Each fsFile In fsFiles
        .Nodes.Add topFolder, tvwChild, fsFile.Path, fsFile.Name
    Next fsFile
    End With
    

loadListExit:
    Exit Function

Handler:
    Select Case Err.Number
        Case Else
            message = "Error #" & Err.Number & " : " & Err.Description
            MsgBox message, vbCritical, "ERROR"
            
    End Select
    Resume loadListExit
    
End Function
 
Last edited:

MarkK

bit cruncher
Local time
Yesterday, 23:59
Joined
Mar 17, 2004
Messages
8,179
Here's a DB with a few ideas you might find useful.
- Full view of file system including all disks
- Tree branches are only populated if you expand the parent, so speed is OK
- Right click a node to show file system info about the clicked item
- Double click file items to open them
Hope this offers some useful options...
Mark
 

Attachments

  • SampleFileTree.zip
    32.9 KB · Views: 1,917

spacepro

Registered User.
Local time
Today, 07:59
Joined
Jan 13, 2003
Messages
715
Lagbolt,

The sample you posted is great. I don't have any knowledge of treeviews and wondered if you could give me an idea on how to amend the code to set the root node as a specific folder and then add nodes for the sub folders and files.

I just want to show the files and sub folders beneath a particular folder, so any advice would be great.

Thanks for any help in advance.

Andy
 

MarkK

bit cruncher
Local time
Yesterday, 23:59
Joined
Mar 17, 2004
Messages
8,179
Here's a modified version...
 

Attachments

  • SampTree_StartFolder.zip
    37.8 KB · Views: 1,950

spacepro

Registered User.
Local time
Today, 07:59
Joined
Jan 13, 2003
Messages
715
lagbolt said:
Here's a modified version...

Thanks Mark. This is a godsend. I didn't have time to start from scratch without using a sample. I've been away from Access for a while and I need to knock a program together asap, so this has been a great help.

Cheers
Andy
 
B

bravefool

Guest
Displaying directory trees

This is a great thread - it is exactly what I need to do. However I downloaded lagbolt's example and I got errors. There seem to some library references missing. The function CreateTree couldn't be found. What references do I need to bring in? Are they standard with Access 2003?

Thanks.

Phil
 

MarkK

bit cruncher
Local time
Yesterday, 23:59
Joined
Mar 17, 2004
Messages
8,179
The samples are what I'd call "fast and dirty" and will need work. Your references should be OK if the forms open at all, but check this in a code window Menu->Tools->References and see what's MISSING. The reference to Lib can be removed.
You'll need references to Microsoft Scripting Runtime (scrrun.dll), and Microsoft Windows Common Controls 6.0 (SP6) (mscomctl.ocx).
 

Tasslehoff

Registered User.
Local time
Today, 02:59
Joined
Jun 1, 2006
Messages
64
Thanks Lagbolt, this is just what I needed. I messed around with it for a couple days and got it to do what I wanted, pretty much. My main problem was that if you put this in as a subform, and want to SetFocus on anything inside the tree subform from the main form, you have to: setfocus to something on the main form, setfocus to the tree subform itself, and then setfocus to the field in the subform. It seems to me that Access is full of these little convolusions. I've had to cheat my way out of a problem more than once...of course it could just be that I don't know what I'm doing! :D
 

rockman

Senior Member
Local time
Yesterday, 23:59
Joined
May 29, 2002
Messages
190
The code is still awesome 2 years later. Treeview with icons! Very cool. Thanks!
 

bwyrwitzke

Registered User.
Local time
Today, 07:59
Joined
Nov 11, 2002
Messages
50
back to life after another year of hibernation...

Great piece of code - does anyone have any ideas on how to modify it for selecting a back-up directory?

Here's the background on what I'm trying to accomplish:

Database is pslit into FE/BE and I'm using a modified backup code that copies and date stamps the BE and copies to an external drive. Things have worked great until someone moved the external drive to a different USB port and the drive letter changed. I'd like to use the TreeView directory to select the correct location for the backup to be copied. Any ideas would be greatly appreciated. Thanks.

Bruce
 

Users who are viewing this thread

Top Bottom