Purpose: To bind data in a database to a tree control
Background: We want to display a hierarchical list of sales customers in a tree control, this is the hierarchy of the sales customer, first press "region", then "province", finally to "customer" we have established three tables in the database, the fields are as follows:
Large Region table: Region ID, region name
Province table: Province id, province name, major area
Customer table: Customer ID, customer name, province
The three tables have established relationships with each other.
1. Create a new form, place two controls on the form, one is the TreeView, and the other is ImageList
2, set the properties of these two controls here and the first hour the only difference is that when we set the ImageList control, we imported two icons, one key is K1, and the other is K2, the node icon of the original tree control can be changed, When we prepare an item is not selected when the icon is a folder that does not open, when selected is an open folder to distinguish.
3, write the code, as follows:
Private Sub Form_Load ()
‘* -----------------------------------------------------------------
' * Populate tree controls with data from database tables (same as queries)
‘* -----------------------------------------------------------------
Dim Rec as New ADODB. Recordset
Dim STRECQL as String
Dim Item as Integer
Dim I as Integer
Dim Nodindex as Node
‘* -----------------------------------------------------------------
' * Define all types of
‘* -----------------------------------------------------------------
' Set the top of the ' ye '
‘* ---------------------------
Set Nodindex = TreeView.Nodes.Add (,, "Ye", "Sales Customer", "K1", "K2")
Nodindex. Sorted = True
‘* -----------------------------------------------------------------
' * The settings here are basically the same as in the first hour.
' * But last more than one "K2" parameter, "K1" represents an unselected icon, "K2" represents the icon after being selected
' * Look closely, you will find that the selected and unselected icons are not the same, one is a folder, one is an open folder
‘* -----------------------------------------------------------------
' Set second level ' parent '
‘* ---------------------------
Rec.open "Large Area table", CurrentProject.Connection, adOpenKeyset, adLockOptimistic, adCmdTableDirect
For i = 0 to Rec.recordcount-1
Set Nodindex = TreeView.Nodes.Add ("Ye", Tvwchild, "Parent" & Rec.fields ("Big zone 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 the data (the query is also OK)
' * Key in the change with the add parameter
' * Everybody look at the third parameter, in the first hour, here is "Parent 1", here with Rec.fields ("Big zone ID") instead of "1", meaning to use the table number instead of manual numbering
' * The fourth parameter is also the same, directly using the Name field in the table to replace the original we manually named
‘* -----------------------------------------------------------------
' 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 of Ownership"), Tvwchild, "Child" & Rec.fields ("Province id"), rec.fields ("Province name ")," K1 "," K2 ")
Nodindex. Sorted = True
Rec.movenext
Next
Rec.close
‘* -----------------------------------------------------------------
' * No more explaining.
' * Note that when defining the first parameter, instead of "Parent" & Rec.fields ("Region ID"), instead use "Parent" & Rec.fields ("Region")
' * This means: Use the field in the list of regions associated with the province table instead of the ID of the scale table directly
‘* -----------------------------------------------------------------
' Set level fourth ' sun '
‘* ---------------------------
Rec.open "Customer table", CurrentProject.Connection, adOpenKeyset, adLockOptimistic, adCmdTableDirect
For i = 0 to Rec.recordcount-1
Set Nodindex = TREEVIEW.NODES.ADD ("Sub" & Rec.fields ("Province"), Tvwchild, "Sun" & Rec.fields ("Customer ID"), Rec.fields ("Customer Name ")," K1 "," K2 ")
Nodindex. Sorted = True
Rec.movenext
Next
Rec.close
‘* -----------------------------------------------------------------
' * You should know it all
‘* -----------------------------------------------------------------
End Sub
End of Second hour
Third hour: Combine a tree control with a form
We do tree control, of course it's impossible to display hierarchical data alone, we want to combine with a form, and when we click on a customer in a tree control, the form can go to that customer's profile accordingly.
Purpose: To combine a tree control with a form
1, we still use the example in the first two hours, but when the form is established, the data source of the form is set to "Customer table" and the fields of the Customer table are placed in the form.
2. Write the following code:
Private Sub Treeview_nodeclick (ByVal Node as Object)
‘* -----------------------------------------------------------------
' * The mouse click event for the tree control is Nodeclick
‘* -----------------------------------------------------------------
Dim Str as String
‘* -----------------------------------------------------------------
' * Define a filter
‘* -----------------------------------------------------------------
If node.text = "Sales Customer" or node.key like "parent *" or node.key like "Sub *" Then
str = ""
‘* -----------------------------------------------------------------
' * In the first hour, we said node has three things, icons, text, indexed values
' * The text is literal and the index value is key
This will mean that when we click on the "Master", "parent" or "child" layers, the form is not filtered
' * This condition can also be written: If node.key = "Ye" or node.key like "parent *" or node.key like "son *" then
‘* -----------------------------------------------------------------
Else
str = "[Customer name]= '" & Node.text & "'"
End If
Me.Form.FilterOn = True
Me.Form.Filter = str
' * Filter The form by the specified criteria
End Sub
Understand, the so-called combination of forms, is actually just a form filter. End of the third hour (5 minutes is enough, haha)
Learning is very fun, but writing articles is very boring, if you learned through this article the basic use of tree control, with a bar, also so that I have a sense of accomplishment.
TreeView calls the database into a tree