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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Drag and drop between two Treeviews

Status
Not open for further replies.

Flippertje

Technical User
Mar 12, 2004
118
NL
Hello all,

after many hours of reading about treeviews in TekTips i finally give up and post a question.

I have a form with two treeviews (Treeview0 and Treeview2). Both trees are populated by (difficult) queries. I have build a table in which i have two fields Id_Group and Id_Child. In this table i assign Child Id's to Group Id's.

I'm trying to populate the table with Id_Group and Id_Child thus assigning a persons task to a product that has to be made. A product can exist of more tasks than one.

This assigntable is used in the query to populate Treeview0.

I'm at a total loss why i can't get it all to work.

My trees are like this:

Treeview0:

TasksPlanner:
|- Produce productioninfo
|- Joppie-Produce info machine A (10-04-2006)
|- Jaqueline - Produce info machine G (15-04-2006)

Treeview2:

TasksAll
|- Joppie
|- Produce info machine A (10-04-2006)
|- Produce info machine D (12-04-2006)
|- Produce info machine Q (18-04-2006)
|- Jaqueline
|- Produce info machine C (09-02-2006)
|- Produce info machine G (15-04-2006)

The queries that populate these Treeviews are build on the same table that has these fields:

Id_A
ForeName
InfoDescription

The Id_A is the ID used in The assign table with the Id_Group and Id_Child.

I hope anyone can help me and would take the time to read this.

Many many thanks!!!!!!

Flippertje
 
I'm not sure what exactly you're having a problem with. But here's some code (not clean but works) that will create a treeview that looks like the following. Just modify the queries to suit your needs. May be by looking at the code, you can see how it works. To make it work, add =BuildTree() to the OnOpen event property of your form. Also, use debug to step thru it to see how it works. If your are not sure how to use debug, see below.

x All Departments
x Facility Name1
x Department Name1
x Department Name2
x Facility Name2

Code:
Function BuildTree()

'********************************
'*  Declaration Specifications  *
'********************************

    Dim cnn As ADODB.Connection
    Dim rstD As ADODB.Recordset
    Dim rstF As ADODB.Recordset
    
    
    Dim cat As ADOX.Catalog
    Dim tbl As ADOX.Table
    Dim col As ADOX.Column
    
    Dim frm As Form
    
    Dim nodAll As Node
    Dim nodFacility As Node               'Parent node
    Dim nodDept As Node              'Current node
    Dim nodField As Node              'Current node
    Dim objTree As TreeView             'Working variable
    
    Static k As Integer                    'Working variable
    Dim i As Integer                    'Working variable
    Static j As Integer
    Dim m As Integer
    Dim intSection As Integer
    
    Dim strKey As String                'Unique key for tree structure
    Dim strDbKey As String
    Dim strDbsName As String
    
    Dim strType As String
    Dim strSQL As String
    
    Dim strN As String
    Dim strP As String
    
    Dim var As Variant
    
    Dim bolShowHidden As Boolean
    Dim bolShowSystem As Boolean
    
'****************
'*  Initialize  *
'****************

    On Error GoTo ErrHandler
    
    Set cnn = New ADODB.Connection
    Set rstD = New ADODB.Recordset
    Set rstF = New ADODB.Recordset
    
    rstD.Open "SELECT tblMMC_Dept.lngDeptID, tblMMC_Dept.strName, tblMMC_Dept.lngFacilityID From tblMMC_Dept WHERE ((Not (tblMMC_Dept.strName) Is Null)) ORDER BY tblMMC_Dept.strName;", CurrentProject.Connection, adOpenDynamic
    rstF.Open "SELECT tblMMC_Facility.lngFacilityID, tblMMC_Facility.strAbbrev From tblMMC_Facility WHERE ((Not (tblMMC_Facility.strAbbrev) Is Null)) ORDER BY tblMMC_Facility.strAbbrev;", CurrentProject.Connection, adOpenDynamic
    
    strDbKey = "AllDepartments"
    Set nodAll = TreeView0.Nodes.Add(, , strDbKey, "All Departments")  'CurrentProject.Name)
    nodAll.Checked = True
    
'*************************
'*  Build Database Node  *
'*************************

    While Not rstF.EOF
    
        strDbsName = rstF!strAbbrev
        
        strDbKey = "Facility=[" & strDbsName & "]" & i
        Set nodFacility = TreeView0.Nodes.Add(nodAll, tvwChild, strDbKey, strDbsName)   'CurrentProject.Name)
'        nodFacility.Image = 1
        nodFacility.Tag = rstF!lngFacilityID
        nodFacility.Checked = True
        
        rstD.Filter = "lngFacilityID = " & rstF!lngFacilityID
        
        While Not rstD.EOF
        
            k = k + 1
            strKey = "Dept=[" & rstD!strName & "]" & k            '"TableID= " & CStr(i)
            
            Set nodDept = TreeView0.Nodes.Add(nodFacility, tvwChild, strKey, rstD!strName)
'            nodDept.Image = 2
            nodDept.Tag = rstD!lngDeptID
            nodDept.Checked = True
            
            rstD.MoveNext
        Wend
        
        rstF.MoveNext
        
    Wend
    

'********************
'*  Exit Procedure  *
'********************

ExitProcedure:

    Exit Function
    
'****************************
'*  Error Recovery Section  *
'****************************

ErrHandler:

            MsgBox "BuildTree: " & Err.number & vbCrLf & Err.Description
            Resume ExitProcedure

    
End Function

To use debug, enter the word Stop above the line of code you want to examine. Or, place the cursor on the line of code and press F9 (F9 toggles the breakpoint on/off). Then open your form to execute the code. The program will pause execution when it encounters the word Stop or a line that contains the breakpoint. To examine the value of a variable, either place the mouse pointer over the variable or open debug's Immediate window and type ?TheNameofYourVariable. Press F8 to step thru your code 1 line at a time. Press F5 to have your code run until it encounters the next Stop statement, breakpoint, or exits.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top