此示例代码在oracle数据库中Scott模式的emp表的treeview控件中显示员工姓名。
从开始
从组件中选择Microsoft Windows公共控件6.0(SP6)
将TreeView和ListView控件添加到窗体。
将此示例代码添加到表单
=========================
Dim con As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim rs1 As New ADODB.Recordset
Private Sub Form_Load()
con.Open "Provider=OraOLEDB.Oracle.1;Password=tiger;Persist Security Info=True;User ID=scott;Data Source=das"
call TREE
End Sub
Public Sub TREE()
tv1.Visible = False
tv1.Nodes.Clear
tv1.Visible = True
Dim scontactname As String
Dim currentalpha As String
rs1.Open "select * from emp order by ename", con, adOpenDynamic, adLockOptimistic
If (rs1.RecordCount > 0) Then
rs1.MoveFirst
End If
For indx = Asc("A") To Asc("Z")
currentalpha = Chr(indx)
Set contactnode = tv1.Nodes.Add(, , currentalpha, currentalpha)
If (Not rs1.EOF) Then
Do While UCase(Left(rs1!ename, 1)) = currentalpha
With rs1
If (Not IsNull(!ename)) Then
scontactname = !ename
End If
End With
DoEvents
Set contactnode = tv1.Nodes.Add(currentalpha, tvwChild, rs1!ename, scontactname)
rs1.MoveNext
If (rs1.EOF) Then
Exit Do
End If
Loop
End If
Next
rs1.Close
DoEvents
End Sub
显示从Treeview控件中选择的记录的详细信息。
如果需要,在属性面板的列表视图中将列标题添加为表中的字段名称。
添加以下示例代码
========================
Private Sub tv1_NodeClick(ByVal Node As MSComctlLib.Node)
LV1.ListItems.Clear
rs.Open "select * from EMP where ENAME = '" & tv1.SelectedItem.Key & "'", con, adOpenDynamic, adLockOptimistic
Dim li1 As ListItem
Set li1 = LV1.ListItems.Add()
If Not rs.EOF Then
If IsNull(rs(0)) Then
li1.Text = ""
Else
li1.Text = rs(0)
End If
If IsNull(rs(1)) Then
LV1.ListItems(1).ListSubItems.Add , , ""
Else
LV1.ListItems(1).ListSubItems.Add , , rs(1)
End If
If IsNull(rs(2)) Then
LV1.ListItems(1).ListSubItems.Add , , ""
Else
LV1.ListItems(1).ListSubItems.Add , , rs(2)
End If
If IsNull(rs(3)) Then
LV1.ListItems(1).ListSubItems.Add , , ""
Else
LV1.ListItems(1).ListSubItems.Add , , rs(3)
End If
If IsNull(rs(4)) Then
LV1.ListItems(1).ListSubItems.Add , , ""
Else
LV1.ListItems(1).ListSubItems.Add , , rs(4)
End If
If IsNull(rs(5)) Then
LV1.ListItems(1).ListSubItems.Add , , ""
Else
LV1.ListItems(1).ListSubItems.Add , , rs(5)
End If
If IsNull(rs(6)) Then
LV1.ListItems(1).ListSubItems.Add , , ""
Else
LV1.ListItems(1).ListSubItems.Add , , rs(6)
End If
If IsNull(rs(7)) Then
LV1.ListItems(1).ListSubItems.Add , , ""
Else
LV1.ListItems(1).ListSubItems.Add , , rs(7)
End If
End If
rs.Close
End Sub
翻译自: https://bytes.com/topic/visual-basic/insights/726143-adding-items-treeview-listview-database