目录。我要把三个表导入treeview中,请问怎么实现阿?
name表的结构为:
id name nameid
1 河北省 110000
2 江苏省 120000
name1表的结构为:
id name nameid anameid bnameid
1 石家庄市 110100 110000 110100
2 南京市 120100 120000 120100
name2表的结构为:
id name nameid anameid bnameid
1 长安区 110101 110000 110100
2 鼓楼区 120101 120000 120100
anameid是根目录,bnameid是一级子目录
我是菜鸟,麻烦各位解释详细点,在下先谢过了!
12 个解决方案
#1
给你一个实例参考:
Private Sub TestView()
Set mcnn = New ADODB.Connection
mcnn.Open "provider=Microsoft.jet.oledb.4.0;persist security info=false;data source=" & App.Path & "\inc\mm.mdb"
Set rs = New ADODB.Recordset
Set nodX = tvwTest.Nodes.Add(, , "r", "心理测量系统")
TestViewList "r1", "智力类"
TestViewList "r2", "情感类"
TestViewList "r3", "健康类"
TestViewList "r4", "个性类"
TestViewList "r5", "能力类"
TestViewList "r6", "职业类"
mcnn.Close
End Sub
Private Sub TestViewList(root As String, name As String)
sql = "select 名称 from " & name
rs.Open sql, mcnn, , , adCmdText
Set nodX = tvwTest.Nodes.Add("r", tvwChild, root, name)
nodX.EnsureVisible
Do While Not rs.EOF
Set nodX = tvwTest.Nodes.Add(root, tvwChild, , rs("名称"))
rs.MoveNext
Loop
rs.Close
End Sub
Private Sub TestView()
Set mcnn = New ADODB.Connection
mcnn.Open "provider=Microsoft.jet.oledb.4.0;persist security info=false;data source=" & App.Path & "\inc\mm.mdb"
Set rs = New ADODB.Recordset
Set nodX = tvwTest.Nodes.Add(, , "r", "心理测量系统")
TestViewList "r1", "智力类"
TestViewList "r2", "情感类"
TestViewList "r3", "健康类"
TestViewList "r4", "个性类"
TestViewList "r5", "能力类"
TestViewList "r6", "职业类"
mcnn.Close
End Sub
Private Sub TestViewList(root As String, name As String)
sql = "select 名称 from " & name
rs.Open sql, mcnn, , , adCmdText
Set nodX = tvwTest.Nodes.Add("r", tvwChild, root, name)
nodX.EnsureVisible
Do While Not rs.EOF
Set nodX = tvwTest.Nodes.Add(root, tvwChild, , rs("名称"))
rs.MoveNext
Loop
rs.Close
End Sub
#2
你的结构有点模糊,好象是看懂了
长安区是石家庄的吗?
长安区是石家庄的吗?
#3
1.先遍历加载name表的数据到treeview,节点都在同一层
2.遍历加载name1表的数据到treeview,加载前先判断当前name1的当前记录在treeview中一级节点(即name表对应的节点)是否存在,如存在则增加为其子节点,否则忽略跳过;
3.遍历name2,方法与第二步一样.
备注:节点的text属性值为每条记录的name字段值,节点key属性值为nameid字段值,因为key属性值第一位不能数字,请在前面加一个英文字母,判断父节点是否存在用
treeview.nodes(key) is nothing 来判断父节点是否存在
2.遍历加载name1表的数据到treeview,加载前先判断当前name1的当前记录在treeview中一级节点(即name表对应的节点)是否存在,如存在则增加为其子节点,否则忽略跳过;
3.遍历name2,方法与第二步一样.
备注:节点的text属性值为每条记录的name字段值,节点key属性值为nameid字段值,因为key属性值第一位不能数字,请在前面加一个英文字母,判断父节点是否存在用
treeview.nodes(key) is nothing 来判断父节点是否存在
#4
用三重循环可以搞定。
#5
Public Sub TVWCreat()
dim sql as string
Dim i,j As Integer
Dim nodX As Node
Set nodX = TVW.Nodes.Add(, , "Root", "中国")
'........连接数据库
'加入省份名称
'查询sql="select * from name" 得到Rs记录集
Rs.MoveFirst
For i = 1 To Rs.RecordCount
Set nodX = TVW.Nodes.Add("Root", tvwChild, "A" & Rs.Fields("nameid"), Rs.Fields("name"))
Rs.MoveNext
Next i
Rs.close
Set Rs = Nothing
'加入城市名称
'查询sql="select * from name1" 得到Rs记录集
For i = 1 To TVW.Nodes.Count
If TVW.Nodes(i).Children = 0 And TVW.Nodes(i).Key Like "A*" Then
Rs.MoveFirst
For j = 1 To Rs.RecordCount
Set nodX = TVW.Nodes.Add(TVW.Nodes(i).Key, tvwChild, "B" & Rs.Fields("nameid"), Rs.Fields("name"))
Rs.MoveNext
Next j
End If
Next i
Rs.close
Set Rs = Nothing
'加入地区名称
'查询sql="select * from name2" 得到Rs记录集
For i = 1 To TVW.Nodes.Count
If TVW.Nodes(i).Children = 0 And TVW.Nodes(i).Key Like "B*" Then
Rs.MoveFirst
For j = 1 To Rs.RecordCount
Set nodX = TVW.Nodes.Add(TVW.Nodes(i).Key, tvwChild, Rs.Fields("nameid"), Rs.Fields("name"))
Rs.MoveNext
Next j
End If
Next i
Rs.close
Set Rs = Nothing
TVW.Nodes("Root").Expanded = True '展开
'........关闭数据库
End Sub
#6
Option Explicit
'name表的结构为:
'id name nameid
'1 河北省 110000
'2 江苏省 120000
'
'name1表的结构为:
'id name nameid anameid bnameid
'1 石家庄市 110100 110000 110100
'2 南京市 120100 120000 120100
'
'name2表的结构为:
'id name nameid anameid bnameid
'1 长安区 110101 110000 110100
'2 鼓楼区 120101 120000 120100
Private Sub Form_Load()
Dim Conn As ADODB.Connection
Dim Rs As ADODB.Recordset
Dim SQL As String
Dim CurName As String
Dim CurID As String
Dim ParentID As String
Set Conn = New ADODB.Connection
Conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\a.mdb"
Conn.Open
SQL = ""
SQL = SQL & "Select * "
SQL = SQL & " From "
SQL = SQL & " ("
SQL = SQL & " Select [name] as NodeName,[nameid] as NodeKey,'Root' as ParentKey From [name]"
SQL = SQL & " UNION "
SQL = SQL & " Select [name] as NodeName,[nameid] as NodeKey,[anameid] as ParentKey From [name1]"
SQL = SQL & " UNION "
SQL = SQL & " Select [name] as NodeName ,[nameid] as NodeKey,[bnameid] as ParentKey From [name2]"
SQL = SQL & " )"
SQL = SQL & " Order By NodeKey"
Set Rs = Conn.Execute(SQL, , adCmdText)
If Rs.State <> 0 Then
If Not Rs.EOF And Not Rs.BOF Then
While Not Rs.EOF
Debug.Print Rs.Fields(0).Value, Rs.Fields(1).Value, Rs.Fields(2).Value
ParentID = Rs.Fields(2).Value
CurID = Rs.Fields(1).Value
CurName = Rs.Fields(0).Value
With Me
With .TreeView1
With .Nodes
If ParentID = "Root" Then
.Add , , "A_" & CurID, CurName
Else
.Add "A_" & ParentID, 4, "A_" & CurID, CurName
End If
End With
End With
End With
Rs.MoveNext
Wend
End If
End If
Rs.Close
Set Rs = Nothing
Conn.Close
Set Conn = Nothing
End Sub
正确答案!!!
'name表的结构为:
'id name nameid
'1 河北省 110000
'2 江苏省 120000
'
'name1表的结构为:
'id name nameid anameid bnameid
'1 石家庄市 110100 110000 110100
'2 南京市 120100 120000 120100
'
'name2表的结构为:
'id name nameid anameid bnameid
'1 长安区 110101 110000 110100
'2 鼓楼区 120101 120000 120100
Private Sub Form_Load()
Dim Conn As ADODB.Connection
Dim Rs As ADODB.Recordset
Dim SQL As String
Dim CurName As String
Dim CurID As String
Dim ParentID As String
Set Conn = New ADODB.Connection
Conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\a.mdb"
Conn.Open
SQL = ""
SQL = SQL & "Select * "
SQL = SQL & " From "
SQL = SQL & " ("
SQL = SQL & " Select [name] as NodeName,[nameid] as NodeKey,'Root' as ParentKey From [name]"
SQL = SQL & " UNION "
SQL = SQL & " Select [name] as NodeName,[nameid] as NodeKey,[anameid] as ParentKey From [name1]"
SQL = SQL & " UNION "
SQL = SQL & " Select [name] as NodeName ,[nameid] as NodeKey,[bnameid] as ParentKey From [name2]"
SQL = SQL & " )"
SQL = SQL & " Order By NodeKey"
Set Rs = Conn.Execute(SQL, , adCmdText)
If Rs.State <> 0 Then
If Not Rs.EOF And Not Rs.BOF Then
While Not Rs.EOF
Debug.Print Rs.Fields(0).Value, Rs.Fields(1).Value, Rs.Fields(2).Value
ParentID = Rs.Fields(2).Value
CurID = Rs.Fields(1).Value
CurName = Rs.Fields(0).Value
With Me
With .TreeView1
With .Nodes
If ParentID = "Root" Then
.Add , , "A_" & CurID, CurName
Else
.Add "A_" & ParentID, 4, "A_" & CurID, CurName
End If
End With
End With
End With
Rs.MoveNext
Wend
End If
End If
Rs.Close
Set Rs = Nothing
Conn.Close
Set Conn = Nothing
End Sub
正确答案!!!
#7
这是我专门为你写的一个例子,
大哥,如果想到源程序的话,找我联系吧,
flying_zfx@hotmail.com
大哥,如果想到源程序的话,找我联系吧,
flying_zfx@hotmail.com
#8
Dim Cnn As New ADODB.Connection
Dim Rs1 As New ADODB.Recordset, Rs2 As New ADODB.Recordset, Rs3 As New ADODB.Recordset
Dim i, j As Integer
Dim Record_text As String
Dim Record_node, Record_node1, Record_node2 As Node
Dim Str As String
'连接字符串
Cnn.ConnectionString = "Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Initial Catalog=hanrui;Data Source=."
Cnn.Open
Str = "select * from name order by id"
Set Rs1 = Cnn.Execute(Str)
If Not Rs1.EOF Then
TreeView1.Nodes.Clear
Do Until Rs1.EOF '添加根节点 name表
Record_text = ""
Record_text = Rs1.Fields(1).Value
' Add the record node.
Set Record_node = TreeView1.Nodes.Add(, , , Record_text, 1)
Str = "select name,nameid from name1 where anameid =" & "'" & Rs1.Fields(2).Value & "'" & " order by id"
Set Rs2 = Cnn.Execute(Str)
If Not Rs2.EOF Then
Do Until Rs2.EOF
Record_text = Rs2.Fields(0).Value
Set Record_node1 = TreeView1.Nodes.Add(Record_node, tvwChild, , Record_text)
Str = "select name from name2 where anameid =" & "'" & Rs1.Fields(2).Value & "'and bnameid=" & "'" & Rs2.Fields(1).Value & "'" & " order by id"
Debug.Print Str
Set Rs3 = Cnn.Execute(Str)
If Not Rs3.EOF Then
Do Until Rs3.EOF
Record_text = Rs3.Fields(0).Value
Set Record_node2 = TreeView1.Nodes.Add(Record_node1, tvwChild, , Record_text)
Rs3.MoveNext
Loop
End If
Rs2.MoveNext
Loop
End If
' Record_node.EnsureVisible
' Get the next record.
Rs1.MoveNext
Loop
End If
Set Rs1 = Nothing
Cnn.Close
Set Cnn = Nothing
End Sub
#9
谢谢,非常感谢~!怎么给你们加分啊?
#10
flyingZFX大哥,我用你的程序运行了一下,怎么发现错误啊?
实时错误‘35601’
未发现元素
实时错误‘35601’
未发现元素
#11
点击本网页右下角"管理"
#12
问题解决了吧!
呵呵,高效率……
呵呵,高效率……
#1
给你一个实例参考:
Private Sub TestView()
Set mcnn = New ADODB.Connection
mcnn.Open "provider=Microsoft.jet.oledb.4.0;persist security info=false;data source=" & App.Path & "\inc\mm.mdb"
Set rs = New ADODB.Recordset
Set nodX = tvwTest.Nodes.Add(, , "r", "心理测量系统")
TestViewList "r1", "智力类"
TestViewList "r2", "情感类"
TestViewList "r3", "健康类"
TestViewList "r4", "个性类"
TestViewList "r5", "能力类"
TestViewList "r6", "职业类"
mcnn.Close
End Sub
Private Sub TestViewList(root As String, name As String)
sql = "select 名称 from " & name
rs.Open sql, mcnn, , , adCmdText
Set nodX = tvwTest.Nodes.Add("r", tvwChild, root, name)
nodX.EnsureVisible
Do While Not rs.EOF
Set nodX = tvwTest.Nodes.Add(root, tvwChild, , rs("名称"))
rs.MoveNext
Loop
rs.Close
End Sub
Private Sub TestView()
Set mcnn = New ADODB.Connection
mcnn.Open "provider=Microsoft.jet.oledb.4.0;persist security info=false;data source=" & App.Path & "\inc\mm.mdb"
Set rs = New ADODB.Recordset
Set nodX = tvwTest.Nodes.Add(, , "r", "心理测量系统")
TestViewList "r1", "智力类"
TestViewList "r2", "情感类"
TestViewList "r3", "健康类"
TestViewList "r4", "个性类"
TestViewList "r5", "能力类"
TestViewList "r6", "职业类"
mcnn.Close
End Sub
Private Sub TestViewList(root As String, name As String)
sql = "select 名称 from " & name
rs.Open sql, mcnn, , , adCmdText
Set nodX = tvwTest.Nodes.Add("r", tvwChild, root, name)
nodX.EnsureVisible
Do While Not rs.EOF
Set nodX = tvwTest.Nodes.Add(root, tvwChild, , rs("名称"))
rs.MoveNext
Loop
rs.Close
End Sub
#2
你的结构有点模糊,好象是看懂了
长安区是石家庄的吗?
长安区是石家庄的吗?
#3
1.先遍历加载name表的数据到treeview,节点都在同一层
2.遍历加载name1表的数据到treeview,加载前先判断当前name1的当前记录在treeview中一级节点(即name表对应的节点)是否存在,如存在则增加为其子节点,否则忽略跳过;
3.遍历name2,方法与第二步一样.
备注:节点的text属性值为每条记录的name字段值,节点key属性值为nameid字段值,因为key属性值第一位不能数字,请在前面加一个英文字母,判断父节点是否存在用
treeview.nodes(key) is nothing 来判断父节点是否存在
2.遍历加载name1表的数据到treeview,加载前先判断当前name1的当前记录在treeview中一级节点(即name表对应的节点)是否存在,如存在则增加为其子节点,否则忽略跳过;
3.遍历name2,方法与第二步一样.
备注:节点的text属性值为每条记录的name字段值,节点key属性值为nameid字段值,因为key属性值第一位不能数字,请在前面加一个英文字母,判断父节点是否存在用
treeview.nodes(key) is nothing 来判断父节点是否存在
#4
用三重循环可以搞定。
#5
Public Sub TVWCreat()
dim sql as string
Dim i,j As Integer
Dim nodX As Node
Set nodX = TVW.Nodes.Add(, , "Root", "中国")
'........连接数据库
'加入省份名称
'查询sql="select * from name" 得到Rs记录集
Rs.MoveFirst
For i = 1 To Rs.RecordCount
Set nodX = TVW.Nodes.Add("Root", tvwChild, "A" & Rs.Fields("nameid"), Rs.Fields("name"))
Rs.MoveNext
Next i
Rs.close
Set Rs = Nothing
'加入城市名称
'查询sql="select * from name1" 得到Rs记录集
For i = 1 To TVW.Nodes.Count
If TVW.Nodes(i).Children = 0 And TVW.Nodes(i).Key Like "A*" Then
Rs.MoveFirst
For j = 1 To Rs.RecordCount
Set nodX = TVW.Nodes.Add(TVW.Nodes(i).Key, tvwChild, "B" & Rs.Fields("nameid"), Rs.Fields("name"))
Rs.MoveNext
Next j
End If
Next i
Rs.close
Set Rs = Nothing
'加入地区名称
'查询sql="select * from name2" 得到Rs记录集
For i = 1 To TVW.Nodes.Count
If TVW.Nodes(i).Children = 0 And TVW.Nodes(i).Key Like "B*" Then
Rs.MoveFirst
For j = 1 To Rs.RecordCount
Set nodX = TVW.Nodes.Add(TVW.Nodes(i).Key, tvwChild, Rs.Fields("nameid"), Rs.Fields("name"))
Rs.MoveNext
Next j
End If
Next i
Rs.close
Set Rs = Nothing
TVW.Nodes("Root").Expanded = True '展开
'........关闭数据库
End Sub
#6
Option Explicit
'name表的结构为:
'id name nameid
'1 河北省 110000
'2 江苏省 120000
'
'name1表的结构为:
'id name nameid anameid bnameid
'1 石家庄市 110100 110000 110100
'2 南京市 120100 120000 120100
'
'name2表的结构为:
'id name nameid anameid bnameid
'1 长安区 110101 110000 110100
'2 鼓楼区 120101 120000 120100
Private Sub Form_Load()
Dim Conn As ADODB.Connection
Dim Rs As ADODB.Recordset
Dim SQL As String
Dim CurName As String
Dim CurID As String
Dim ParentID As String
Set Conn = New ADODB.Connection
Conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\a.mdb"
Conn.Open
SQL = ""
SQL = SQL & "Select * "
SQL = SQL & " From "
SQL = SQL & " ("
SQL = SQL & " Select [name] as NodeName,[nameid] as NodeKey,'Root' as ParentKey From [name]"
SQL = SQL & " UNION "
SQL = SQL & " Select [name] as NodeName,[nameid] as NodeKey,[anameid] as ParentKey From [name1]"
SQL = SQL & " UNION "
SQL = SQL & " Select [name] as NodeName ,[nameid] as NodeKey,[bnameid] as ParentKey From [name2]"
SQL = SQL & " )"
SQL = SQL & " Order By NodeKey"
Set Rs = Conn.Execute(SQL, , adCmdText)
If Rs.State <> 0 Then
If Not Rs.EOF And Not Rs.BOF Then
While Not Rs.EOF
Debug.Print Rs.Fields(0).Value, Rs.Fields(1).Value, Rs.Fields(2).Value
ParentID = Rs.Fields(2).Value
CurID = Rs.Fields(1).Value
CurName = Rs.Fields(0).Value
With Me
With .TreeView1
With .Nodes
If ParentID = "Root" Then
.Add , , "A_" & CurID, CurName
Else
.Add "A_" & ParentID, 4, "A_" & CurID, CurName
End If
End With
End With
End With
Rs.MoveNext
Wend
End If
End If
Rs.Close
Set Rs = Nothing
Conn.Close
Set Conn = Nothing
End Sub
正确答案!!!
'name表的结构为:
'id name nameid
'1 河北省 110000
'2 江苏省 120000
'
'name1表的结构为:
'id name nameid anameid bnameid
'1 石家庄市 110100 110000 110100
'2 南京市 120100 120000 120100
'
'name2表的结构为:
'id name nameid anameid bnameid
'1 长安区 110101 110000 110100
'2 鼓楼区 120101 120000 120100
Private Sub Form_Load()
Dim Conn As ADODB.Connection
Dim Rs As ADODB.Recordset
Dim SQL As String
Dim CurName As String
Dim CurID As String
Dim ParentID As String
Set Conn = New ADODB.Connection
Conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\a.mdb"
Conn.Open
SQL = ""
SQL = SQL & "Select * "
SQL = SQL & " From "
SQL = SQL & " ("
SQL = SQL & " Select [name] as NodeName,[nameid] as NodeKey,'Root' as ParentKey From [name]"
SQL = SQL & " UNION "
SQL = SQL & " Select [name] as NodeName,[nameid] as NodeKey,[anameid] as ParentKey From [name1]"
SQL = SQL & " UNION "
SQL = SQL & " Select [name] as NodeName ,[nameid] as NodeKey,[bnameid] as ParentKey From [name2]"
SQL = SQL & " )"
SQL = SQL & " Order By NodeKey"
Set Rs = Conn.Execute(SQL, , adCmdText)
If Rs.State <> 0 Then
If Not Rs.EOF And Not Rs.BOF Then
While Not Rs.EOF
Debug.Print Rs.Fields(0).Value, Rs.Fields(1).Value, Rs.Fields(2).Value
ParentID = Rs.Fields(2).Value
CurID = Rs.Fields(1).Value
CurName = Rs.Fields(0).Value
With Me
With .TreeView1
With .Nodes
If ParentID = "Root" Then
.Add , , "A_" & CurID, CurName
Else
.Add "A_" & ParentID, 4, "A_" & CurID, CurName
End If
End With
End With
End With
Rs.MoveNext
Wend
End If
End If
Rs.Close
Set Rs = Nothing
Conn.Close
Set Conn = Nothing
End Sub
正确答案!!!
#7
这是我专门为你写的一个例子,
大哥,如果想到源程序的话,找我联系吧,
flying_zfx@hotmail.com
大哥,如果想到源程序的话,找我联系吧,
flying_zfx@hotmail.com
#8
Dim Cnn As New ADODB.Connection
Dim Rs1 As New ADODB.Recordset, Rs2 As New ADODB.Recordset, Rs3 As New ADODB.Recordset
Dim i, j As Integer
Dim Record_text As String
Dim Record_node, Record_node1, Record_node2 As Node
Dim Str As String
'连接字符串
Cnn.ConnectionString = "Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Initial Catalog=hanrui;Data Source=."
Cnn.Open
Str = "select * from name order by id"
Set Rs1 = Cnn.Execute(Str)
If Not Rs1.EOF Then
TreeView1.Nodes.Clear
Do Until Rs1.EOF '添加根节点 name表
Record_text = ""
Record_text = Rs1.Fields(1).Value
' Add the record node.
Set Record_node = TreeView1.Nodes.Add(, , , Record_text, 1)
Str = "select name,nameid from name1 where anameid =" & "'" & Rs1.Fields(2).Value & "'" & " order by id"
Set Rs2 = Cnn.Execute(Str)
If Not Rs2.EOF Then
Do Until Rs2.EOF
Record_text = Rs2.Fields(0).Value
Set Record_node1 = TreeView1.Nodes.Add(Record_node, tvwChild, , Record_text)
Str = "select name from name2 where anameid =" & "'" & Rs1.Fields(2).Value & "'and bnameid=" & "'" & Rs2.Fields(1).Value & "'" & " order by id"
Debug.Print Str
Set Rs3 = Cnn.Execute(Str)
If Not Rs3.EOF Then
Do Until Rs3.EOF
Record_text = Rs3.Fields(0).Value
Set Record_node2 = TreeView1.Nodes.Add(Record_node1, tvwChild, , Record_text)
Rs3.MoveNext
Loop
End If
Rs2.MoveNext
Loop
End If
' Record_node.EnsureVisible
' Get the next record.
Rs1.MoveNext
Loop
End If
Set Rs1 = Nothing
Cnn.Close
Set Cnn = Nothing
End Sub
#9
谢谢,非常感谢~!怎么给你们加分啊?
#10
flyingZFX大哥,我用你的程序运行了一下,怎么发现错误啊?
实时错误‘35601’
未发现元素
实时错误‘35601’
未发现元素
#11
点击本网页右下角"管理"
#12
问题解决了吧!
呵呵,高效率……
呵呵,高效率……