Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Passing Parameters Help Needed 1

Status
Not open for further replies.

LadyDev

Programmer
Jan 29, 2003
86
US
How can I (or I should say, does Access 2000 have the capability) of creating the dataview/grid on the fly. How can I display a directory (c:, file folder title; plus its' contents) and pass that to a dataview/grid subform that is based on user selection; without having to create a separate dataview subform for each file folder?
 
I mentioned before you might want to try a tree view. I kinda wanted one of those myself, so I implemented one.

What you need to do is:

1) create a form with a tree view control. This control is an ActiveX control, and can be found by clicking the 'more controls' icon on the lower right corner of the toolbox. You want to add a Microsoft TreeView Control v. 6.0

2) after you place the control, set it's properties as follows:
a) double click on the control to bring up properties
b) Style = 6
c) Mouse = 0
d) LineStyle = 0
e) label edit = 0
f) Image list = <none>
g) Border Style = 0
h) appearance = 1
i) OLE Drag = 0
j) OLE Drop = 0
k) check: HideSelection, Sorted, Enabled, SingleSel, Scroll

3) Hit OK

4) Create a form open event, and add the following code:

Private Sub Form_Open(Cancel As Integer)
Set MyTreeView = Me.TreeCtrl

Dim MyDir As String
MyDir = &quot;C:\Sat Tracker&quot;
Dim ParentNode As Node
Set ParentNode = MyTreeView.Nodes.Add(, , MyDir, MyDir)
FillTree MyDir, ParentNode
End Sub

5) Add this subroutine (and declaration)


Dim MyTreeView As Object

Private Sub FillTree(SourceDir As String, ParentNode As Node)
Dim SourceSpec As String
SourceSpec = SourceDir & &quot;\*.*&quot;
Dim CurFile As String
CurFile = Dir(SourceSpec, vbDirectory)
Do While (CurFile <> &quot;&quot;)
If Not (CurFile = &quot;.&quot; Or CurFile = &quot;..&quot;) Then
Dim FullFileName As String
FullFileName = SourceDir & &quot;\&quot; & CurFile
Dim CurNode As Node
Set CurNode = MyTreeView.Nodes.Add(SourceDir, tvwChild, FullFileName, CurFile)
If GetAttr(FullFileName) = vbDirectory Then
Dim SaveName As String
SaveName = CurFile
FillTree FullFileName, CurNode
CurFile = Dir(SourceSpec, vbDirectory)
Do While (CurFile <> SaveName)
CurFile = Dir
Loop
End If
End If
CurFile = Dir
Loop
End Sub

6) if you want to find out if the user double clicks on a tree node, right click on the tree control in the form, and select Build Event. It will create an event procedure for some (random?) event. Select DblClick from the drop down list on the upper right of the module editor. Add the following code:

Private Sub TreeCtrl_DblClick()
Debug.Print &quot;User selected: &quot; & Me.TreeCtrl.SelectedItem.Key
End Sub


Compile, save, open the form.

You should see a functional tree control filled with a file tree.
 
P.S. you will probably want to pass the directory name to your form. Use the Form's openargs property. (see MyDir in FormOpen)
 
Thanks! Is this treeview similiar in style and functionality to the windows explorer? I need that functionality.

With regard to my original question, I was told by another individual that I should be able to do this on the fly by building the directory string based on what the user selects and pass it to the viewer. I just don't know what that code would look like, do you.

 
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 &quot;kernel32&quot; (ByVal hFindFile As Long) As Long

Public Declare Function FindFirstFile Lib &quot;kernel32&quot; Alias &quot;FindFirstFileA&quot; _
(ByVal lpFileName As String, lpFindFileData As FileFindDataType) As Long

Public Declare Function FindNextFile Lib &quot;kernel32&quot; Alias &quot;FindNextFileA&quot; _
(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 & &quot;\*.*&quot;


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 = &quot;.&quot; Or CurFile = &quot;..&quot;) Then
Dim FullFileName As String
FullFileName = SourceDir & &quot;\&quot; & 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 &quot;TreeDir&quot;, , , , , , &quot;C:\Program Files\DevStudio&quot;
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.
 
I guess you'd have to run it and see. The speed may also depend on the network.

Another alternative is to modify the directory tree-building routine so that it only searches one level; when the user tries to expand a folder then additional searching is done. I imagine that's how the IE works. When I have time I'll implement that; right now it's just a tool I'm keeping tucked away in the toolbox for later use.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top