Solved

Code for Windows Explorer type tree view wanted

Posted on 2002-05-14
9
671 Views
Last Modified: 2008-03-10
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  
0
Comment
Question by:morrisette
9 Comments
 
LVL 18

Expert Comment

by:mdougan
ID: 7009498
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....
   
0
 

Author Comment

by:morrisette
ID: 7009517
Thanks mdougan

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

morrisette
0
 
LVL 18

Expert Comment

by:mdougan
ID: 7009608
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.
0
 
LVL 1

Expert Comment

by:magoo2
ID: 7009866
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

0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

Author Comment

by:morrisette
ID: 7010548
Thanks magoo2. I will check this out

morrisette
0
 

Author Comment

by:morrisette
ID: 7015716
Hi mdougan

could I have the routine to lock the tree during update, please.
morrisette
0
 
LVL 18

Accepted Solution

by:
mdougan earned 300 total points
ID: 7016382
Here is the same code above (more or less) with the additional routine to lock the window.  Let me know if you need any samples of loading the listview control in report mode when someone clicks on the tree:

Private Declare Function LockWindowUpdate Lib "user32" (ByVal hwndLock As Long) As Long

Dim CN As ADODB.Connection
Dim RS As ADODB.Recordset

Private Sub Command1_Click()
Dim xNode As Node
Dim i As Long
   
    On Error Resume Next
   
    Screen.MousePointer = vbHourglass
   
' Here, you need to Lock the Tree
    TreeRedraw tvwObjects.hwnd, False

    tvwObjects.Nodes.Clear
   
    Set tvwObjects.ImageList = Nothing
    Set tvwObjects.ImageList = imlObjects
   
    Set xNode = tvwObjects.Nodes.Add(, , "ROOT", "ROOT", 1)
     
    While Not RS.EOF
        Set xNode = tvwObjects.Nodes.Add("ROOT", tvwChild, "C" & RS("name_object_no").Value, RS("name").Value, 1)
        xNode.Tag = "C" & RS("parent_name_object_no").Value
        RS.MoveNext
    Wend
   
    For Each xNode In tvwObjects.Nodes
      sParent = xNode.Tag
      If Len(xNode.Tag) > 0 Then        ' Don't try and reparent the ROOT !
          If xNode.Tag = "C0" Then
              xNode.Tag = "ROOT"
          End If
          Set xNode.Parent = tvwObjects.Nodes(xNode.Tag)
      End If
    Next
   
    For Each xNode In tvwObjects.Nodes
        If xNode.children > 0 Then
            xNode.Image = 1
        Else
            xNode.Image = 3
        End If
    Next
   
    tvwObjects.Nodes("ROOT").Expanded = True
    tvwObjects.Refresh

' Here you need to unlock the Tree
    TreeRedraw tvwObjects.hwnd, True

    Screen.MousePointer = vbDefault
   
End Sub

Private Sub TreeRedraw(ByVal lHwnd As Long, ByVal bRedraw As Boolean)

If bRedraw Then
        LockWindowUpdate 0
    Else
        LockWindowUpdate lHwnd
    End If
End Sub
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 7765462
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
0
 

Expert Comment

by:SpideyMod
ID: 7837976
per recommendation

SpideyMod
Community Support Moderator @Experts Exchange
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now