This sample code displays employee name in the treeview control from the emp table of Scott schema in oracle database.
To start with
Select Microsoft windows common controls 6.0 (SP6) from components
Add a TreeView and a ListView control to the form.
Add this sample code to the form
==========================
- 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
-
To display the details of the record as selected from the Treeview control.
Add the column headers in the listview from property pallet as the field name from the table if desired.
Add the following sample code
========================
-
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
-