Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Code for Windows Explorer type tree view wanted

Posted on 2002-05-14
9
Medium Priority
?
683 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 

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 1200 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

722 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