It's similar to the windows explorer in that you can open and close folders.
However, behaviors like 'drag and drop' and 'label edit' you would have to trap with event procedures and process your self (e.g. move files, rename file)
The code I posted does work 'on-the-fly'. You would open a form using 'OpenArgs'
Also, the recursive dir() work-around is kinda slow. Instead, one can use API functions that *do* work recursively.
Here is the new code:
1) Create a new module, call it, say FillDirTreeModule, and paste in the following:
Option Compare Database
Option Explicit
Public Const c_MaxPathLen As Long = 260
Public Type FileTimeType
LowerDate As Long
UpperDate As Long
End Type
Public Type FileFindDataType
FileAtr As Long
CreateTime As FileTimeType
LastAccessTime As FileTimeType
LastWriteTime As FileTimeType
UpperFileSize As Long
LowerFileSize As Long
Res1 As Long
Res2 As Long
FileName As String * c_MaxPathLen
AltFileName As String * 14
End Type
Public Declare Function FindClose Lib "kernel32" (ByVal hFindFile As Long) As Long
Public Declare Function FindFirstFile Lib "kernel32" Alias "FindFirstFileA" _
(ByVal lpFileName As String, lpFindFileData As FileFindDataType) As Long
Public Declare Function FindNextFile Lib "kernel32" Alias "FindNextFileA" _
(ByVal hFindFile As Long, lpFindFileData As FileFindDataType) As Long
Public Function FixString(TheStr As String) As String
Dim NullPos As Integer
NullPos = InStr(TheStr, Chr$(0))
If NullPos <> 0 Then
FixString = Left$(TheStr, NullPos - 1)
Else
FixString = TheStr
End If
End Function
Public Sub DoFillDirTree(MyTreeView As Object, SourceDir As String, ParentNode As Node)
Dim SourceSpec As String
SourceSpec = SourceDir & "\*.*"
Dim WFD As FileFindDataType
Dim hFile As Long
'obtain handle to the first filespec match
hFile = FindFirstFile(SourceSpec, WFD)
'if valid ...
If hFile <> -1 Then
Do While True
Dim CurFile As String
'remove trailing nulls
CurFile = FixString(WFD.FileName)
If Not (CurFile = "." Or CurFile = ".."

Then
Dim FullFileName As String
FullFileName = SourceDir & "\" & CurFile
Dim CurNode As Node
Set CurNode = MyTreeView.Nodes.Add(SourceDir, tvwChild, FullFileName, CurFile)
CurNode.Sorted = True
If (GetAttr(FullFileName) And vbDirectory) = vbDirectory Then
DoFillDirTree MyTreeView, FullFileName, CurNode
End If
End If
If FindNextFile(hFile, WFD) = 0 Then Exit Do
Loop
FindClose hFile
End If
End Sub
Public Sub FillDirTree(MyTreeView As Object, SourceDir As String)
Dim ParentNode As Node
Set ParentNode = MyTreeView.Nodes.Add(, , SourceDir, SourceDir)
ParentNode.Sorted = True
DoFillDirTree MyTreeView, SourceDir, ParentNode
End Sub
2) Create the form as described before. Put this in the Form Open event:
Private Sub Form_Open(Cancel As Integer)
FillDirTree Me.TreeCtrl, Me.OpenArgs
End Sub
3) To open the form with any random directory, use code like this:
Sub TestDirForm()
DoCmd.OpenForm "TreeDir", , , , , , "C:\Program Files\DevStudio"
End Sub
4) NOTES: the program takes a LONG time to run if there are a large number of files. I blame the TreeControl. However, for smaller directories it works quite nicely.