Link to home
Start Free TrialLog in
Avatar of morrisette
morrisette

asked on

Code for Windows Explorer type tree view wanted

Hi
I am trying to create a tree view of departments in a company within each department there are Workcentres.
When I click on a work centre I want the employees to be displayed as files would be in Windows explorer if I clicked on a sub folder. I will be using an Employee Access table which holds all employee details. I am using ADO and VB6.  Currently I am selecting the employees from a dropdown list, but this does not categorise employees in workcentres which would make it easier for the user to browse.

Can the experts help?

Thanks

morrisette  
Avatar of mdougan
mdougan
Flag of United States of America image

The somewhat brief answer is that you will need at least two queries.  One to fill the treeview, and one to select the employees for a given workcenter.

The query for filling the treeview should look something like this:

Select D.DepartmentID, D.DepartmentName,
       W.WorkCenterID, W.WorkCenterName
From Department D, WorkCenter W
Where W.DepartmentID = D.DepartmentID
Order by D.DepartmentName, W.WorkCenterName

Then When filling the TreeView, you code might look like this:

Dim xNode As Node
Dim i As Long
Dim lDeptID as long
   
    On Error Resume Next
   
    Screen.MousePointer = vbHourglass
   
    ' clear out the tree
    tvwObjects.Nodes.Clear
   
    ' set the imagelist to an image list on the form
    Set tvwObjects.ImageList = Nothing
    Set tvwObjects.ImageList = imlObjects
   
    ' assuming that you've already opened the recordset
    While Not RS.EOF
        ' if are now on a new department, add the department node
        if lDeptID <> RS("departmentID").Value Then
           Set xNode = tvwObjects.Nodes.Add , , "D" & RS("departmentid").Value, RS("departmentname").Value, 1)
           lDeptID = RS("departmentID").Value
        End if
        ' now, add the workcenter node don't worry about setting the relationships yet
        Set xNode = tvwObjects.Nodes.Add , , "W" & RS("workcenterid").Value, RS("workcentername").Value, 1)
        xNode.Tag = "D" & RS("departmentID").Value
        RS.MoveNext
    Wend
   
    ' now, go through and set the hierarchical relationships
    For Each xNode In tvwObjects.Nodes
      sParent = xNode.Tag
      If Len(xNode.Tag) > 0 Then  ' Don't try and reparent the Departments!
          Set xNode.Parent = tvwObjects.Nodes(xNode.Tag)
      End If
    Next
   
    ' these numbers correspond to folder and leaf icons respectively
    ' so, set the appropriate icons
    For Each xNode In tvwObjects.Nodes
        If xNode.Children > 0 Then
            xNode.Image = 1
        Else
            xNode.Image = 2
        End If
    Next

    tvwObjects.Refresh
 
    Screen.MousePointer = vbDefault


Then, in the node_click event of the tree, you can use the tvwObjects.SelectedNode.Key (Look at the first character on the left and if it's a W it's a Workcenter else they've clicked on a Department) truncate the first character and you have the workcenter id which you can then use to run the second query that gets all employees that work for that workcenter.  I'd suggest that you use a ListView control for the area to the right of the treeview to display the resulting employees.  The ListView has a report mode that allows you to display the Employee details in a grid-like format, similar to the List view of Windows Explorer, or, there are Icon views that let you display them as Icons, also like Windows Explorer....
   
Avatar of morrisette
morrisette

ASKER

Thanks mdougan

I will try this and let you know. it looks very good.

morrisette
Depending on how many departments and workcenters you have, if it seems to take a long time to load the tree let me know, I have a routine that can be used to lock the treeview window so that it does not update after each node is added which really slows down the loading.  You wont notice it if you're just adding 50 or 100 nodes, but if you have several hundred, you might.
morrisette,

here is a link to a class sample for treeview that may also be helpful...

http://www.planetsourcecode.com/vb/scripts/ShowCode.asp?lngWId=1&txtCodeId=34439

Thanks magoo2. I will check this out

morrisette
Hi mdougan

could I have the routine to lock the tree during update, please.
morrisette
ASKER CERTIFIED SOLUTION
Avatar of mdougan
mdougan
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of DanRollins
Hi morrisette,
It appears that you have forgotten this question. I will ask Community Support to close it unless you finalize it within 7 days. I will ask a Community Support Moderator to:

    Accept mdougan's comment(s) as an answer.

morrisette, if you think your question was not answered at all or if you need help, just post a new comment here; Community Support will help you.  DO NOT accept this comment as an answer.

EXPERTS: If you disagree with that recommendation, please post an explanatory comment.
==========
DanRollins -- EE database cleanup volunteer
per recommendation

SpideyMod
Community Support Moderator @Experts Exchange