treeview calls the database into a tree

Purpose: Bind the data in the database to the tree control
Background: We want to display the hierarchical list of sales customers in the tree control. The sales customer hierarchy is like this, first press "region", then press "province", Finally, in the "customer" database, we created three tables with the following fields:
Region table: Region ID, Region name
Province table: Province ID, Province name, Region
Customer table: Customer ID, Customer name,
The three tables that belong to the province establish a relationship with each other

 

1. Create a new form and place two controls on the form, one is Treeview and the other is Imagelist
2. Set the properties of these two controls. The only difference between this and the first hour is that when we set the Imagelist control, Two icons are imported, one KEY is K1 and the other is K2. The Node icon of the original tree control can be changed. The icon we prepared when an item is not selected is an unopened folder. When selected, it is an open folder. folder to distinguish.
3. Write the code as follows:

 

Private Sub Form_Load()
'* -------------------------------------------------------- ----------------------
'* Fill the tree control with data from the database table (as well as the query)
'* ----------- -------------------------------------------------- ----
    Dim Rec As New ADODB.Recordset
    Dim stRecQL As String
    Dim Item As Integer
    Dim i As Integer
    Dim nodindex As Node
'* --------------------- --------------------------------------------
'* define types
' * ------------------------------------------------- ---------------- 
   
    'Set the top "Master"
'* ------------------------ ---
    Set nodindex = TreeView.Nodes.Add(, , "Master", "Sales Customer", "K1", "K2")
    nodindex.Sorted = True
'* ------------------------------------------------ -----------------
'*The settings here are basically the same as in the first hour
'* but at the end there is a parameter "K2", "K1" represents The icon when it is not selected, "K2" represents the icon after it is selected
'* Look carefully, you will find that the selected and unselected icons are different, one is a folder, the other is an open folder
'* ------------------------------------------------ -----------------
    
    'Set the second level "parent"
'* ----------------------- ----
    Rec.Open "Region Table", CurrentProject.Connection, adOpenKeyset, adLockOptimistic, adCmdTableDirect
    For i = 0 To Rec.RecordCount - 1
        Set nodindex = TreeView.Nodes.Add("Master", tvwChild, "Parent" & Rec.Fields("Region ID"), Rec.Fields("Region Name"), "K1", "K2")
        nodindex.Sorted = True
        Rec.MoveNext
    Next
    Rec.Close
'* ------------------------------------------------ -----------------
'*The first line means to open a table to find data (query is also possible)
'*The key is the change with the Add parameter
'*Everyone see the first Three parameters, in the first hour, here is "parent 1", here use Rec.Fields("region ID") to replace "1", which means to use table number instead of manual number
'* fourth The same is true for parameters, directly use the name field in the table to replace the original manual naming
'* ------------------------------ -----------------------------------

 

    'Set the third level "sub"
'* ---------------------------
   Rec.Open "province table", CurrentProject.Connection, adOpenKeyset , adLockOptimistic, adCmdTableDirect
    For i = 0 To Rec.RecordCount - 1
        Set nodindex = TreeView.Nodes.Add("Parent" & Rec.Fields("Region"), tvwChild, "Child" & Rec.Fields("Province ID"), Rec.Fields("province name"), "K1", "K2")
        nodindex.Sorted = True
        Rec.MoveNext
    Next
    Rec.Close
'* -------------- -------------------------------------------------- -
'*Don't explain it any more
'*It should be noted that when defining the first parameter, instead of using "parent" & Rec.Fields("region ID"), use "parent" & Rec.Fields ("Area")
'*This means:Use the fields associated with the large area table in the province table, instead of directly using the ID of the large area table
'* ---------------------------- -------------------------------------

 

    'Set the fourth level "grandchild"
'* ---------------------------
    
Rec.Open "Customer table", CurrentProject.Connection, adOpenKeyset , adLockOptimistic, adCmdTableDirect
    For i = 0 To Rec.RecordCount - 1
        Set nodindex = TreeView.Nodes.Add("Child" & Rec.Fields("Province"), tvwChild, "Grandchild" & Rec.Fields("Customer ID "), Rec.Fields("Customer Name"), "K1", "K2")
        nodindex.Sorted = True
        Rec.MoveNext
    Next
    Rec.Close
    
'* --------------- --------------------------------------------------
'*At this point you should fully understand
'* --------------------------------------- --------------------------

 

End Sub

 

End of second hour

The third hour: Combining the tree control with the form
We make a tree control. Of course, it is impossible to display hierarchical data only. We hope to combine it with the form. When we click a customer in the tree control, the form can respond accordingly. to go to this customer's profile.

 

Purpose: Combining the tree control with
the form fields of the table.
2. Write the following code:

 

Private Sub Treeview_NodeClick(ByVal Node As Object)
'* --------------------------------------- --------------------------
'*The mouse click event of the tree control is NodeClick
'* ------------ -------------------------------------------------- ---
Dim str As String
'* ----------------------------------------- ------------------------
'*Define a filter
'* ------------------- ----------------------------------------------
If Node.Text = "Sales Account" Or Node.Key Like "Parent*" Or Node.Key Like "Child*" Then
str = ""
'* -------------------- ---------------------------------------------
'* in the first In the hour, we said that Node has three things, icon, text, index value
'* text is text, index value is Key
' here will mean that when we click on the "father", "parent" or "child" layer,Do not filter the form
'* This condition can also be written as: If Node.key = "Master" Or Node.Key Like "Father*" Or Node.Key Like "Child*" Then
'* ------------- -------------------------------------------------- --
Else
str = "[Customer Name]='" & Node.Text & "'"
End If
Me.Form.FilterOn = True
Me.Form.Filter = str
'*Filter the form according to the specified conditionsEnd
Sub

 

Understand, the so-called combined form is actually just a form screening. The third hour is over (5 minutes is enough, haha)

 

Learning is fun, but writing articles is boring. If you have learned the basic use of tree controls through this article, please post it, and it will give me a sense of accomplishment.

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325208577&siteId=291194637