求助:VB treeview使用示例代码(从数据库中读入)
我想实现treeview根结点和子节点都从MDB数据库中读入,但从网上找到如下的代码,但出现报错,无法实现请各位帮助解决
'*****定义数据库连接参数
Dim rs As New ADODB.Connection
Dim bs As New ADODB.Recordset
Dim sql As String '查询字符串
Dim filename As String '数据库名称
Dim ctrFi '连接字符串
Dim nodX As Node '树形控件节点类型定义
'数据库中表与字段设置:
'数据库名称:123.mdb
'表一:
'名称: ' usere
'字段:user(字符型),type(字符型)
'表二:
'名称: typer
'字段: usertype (字符型)
Public Sub connectdata()
'打开数据库连接
filename = App.Path + "\" + "123.mdb"
ctrFi = "Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False;Data Source=" & filename
rs.Open ctrFi
End Sub
Private Sub Form_Load()
'添加节点代码:
TreeView1.LineStyle = tvwRootLines
Call connectdata
sql = "select * from typer" '添加根节点
bs.Open sql, rs, 1
If bs.RecordCount <> 0 Then
bs.MoveFirst
Do While bs.EOF = False
Text1.Text = bs.Fields("usertype").Value
'Set nodX = TreeView1.Nodes.Add(, , bs.Fields("usertype").Value, bs.Fields("usertype").Value, 3)
bs.MoveNext
Loop
End If
'***添加子节点***
sql = "select * from usere"
bs.Open sql, rs, 1
If bs.RecordCount <> 0 Then
bs.MoveFirst
Do While bs.EOF = False
'Set nodX = TreeView1.Nodes.Add(, , bs.Fields("type").Value, tvwChild, bs.Fields("user").Value, bs.Fields("user").Value, 2)
Text1.Text = bs.Fields("user").Value
bs.MoveNext
Loop
End If
bs.Close
rs.Close
End Sub
[解决办法]
如果你什么都不会指望复制粘贴一段代码就能运行,这种想法无异于站在100米外把一个足球踢入球门。