Load data using TreeView

xiaoxiao2021-03-06  60

The TreeView control is more used in the program process. Correctly mastering and using this control, you can express data from hierarchical relationships. In the database application system, we can display the tree structure with TreeView, such as displaying the directory tree, displaying class information, etc., has an important role.

See an actual problem below: There are many classes every year, indicating these data levels.

Database: Access

Table: Test

Field: BH (class number, text, primary key), NJ (grade, text)

Function: Data is expressed in hierarchical relationships

Test Data:

BH

NJ

41020

2001

41021

2001

41025

2003

41026

2003

41027

2003

41028

2003

Add TreeView control, 2 buttons

Two buttons used two ways to display data. However, the second method is significantly better than the first.

Can see code carefully

'Quote Microsoft ActiveX Data Object 2.x Library

DIM NDDATA AS NODE

DIM CNN As Adodb.Connection

DIM RS1 AS New Adodb.Recordset

DIM RS2 As New AdoDB.Recordset

Private submmand1_click ()

ON Error ResMe next

Set nddata = TreeView1.nodes.add (, "db", "class information")

nddata.expanded = true

DIM INTCOUNT AS INTEGER

DIM INTTABLE AS INTEGER

DIM INTFIELD AS INTEGER

DIM INTFN AS INTEGER

RS1.Open "SELECT NJ from Test Group By NJ", CNN, 1, 3

INTTABLE = RS1.Recordcount

Do While INTTABLE <> intcount

Set nddata = TreeView1.nodes.add ("db", TVwchild, "f" & rs1.fields ("nj"), RS1.Fields ("nj"))

rs2.open "SELECT BH, NJ from test where nj = '" & r1.fields ("nj") & "'", CNN, 1, 3

INTFIELD = rs2.Recordcount

IF Intfield <> 0 THEN

INTFN = 0

Do While Intfield <> intfn

Set nddata = TreeView1.nodes.Add ("f" & rs1.fields ("nj"), TVWchild, "S" & rs2.fields ("BH"), RS2.Fields ("BH"))

RS2.MOVENEXT

INTFN = INTFN 1

Loop

END IF

RS2.Close

RS1.MOVENEXT

INTCOUNT = INTCOUNT 1

Loop

Rs1.close

End Sub

'The second method

Private sub fascist2_click ()

ON Error ResMe next

Set nddata = TreeView1.nodes.add (, "db", "class information") nddata.expanded = true

RS1.Open "Select * from test", CNN, 1, 3

Do While Not Rs1.eof

IF CA <> rs1.fields ("nj") THEN

Set nddata = TreeView1.nodes.add ("db", TVwchild, "f" & rs1.fields ("nj"), RS1.Fields ("nj"))

Ca = rs1.fields ("bb")

END IF

Set nddata = TreeView1.Nodes.Add ("f" & rs1.fields ("nj"), TVWchild, "S" & rs1.fields ("BH"), RS1.Fields ("BH"))

RS1.MOVENEXT

Loop

Rs1.close

End Sub

Private sub flow_load ()

Set cnn = new adodb.connection

CNN.Open "provider = microsoft.jet.Oledb.4.0; Data Source = f: / 9cbs_vb / database / treeview Node Add / 1/test.mdb"

End Sub

Private Sub Form_Unload (Cancel AS Integer)

Set con = Nothing

End Sub

转载请注明原文地址:https://www.9cbs.com/read-118916.html

New Post(0)