by Sudhir Kesharwani(Pune) (1 Submission)
Category: Databases/Data Access/DAO/ADO
Compatability: VB 6.0
Difficulty: Unknown Difficulty
Originally Published: Thu 20th September 2001
Date Added: Mon 8th February 2021
Rating: (1 Votes)
Code for searching a record using Treeview control and ADODB, Records can be searched by any value in any field,
API Declarations
Dim fld, fname
'declare variables of type connection,recordset
Dim cnn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim rss As ADODB.Recordset
I've user a database which is having two fields Name a brch for storing record of a student, u can do it in a different way. Okay happy programming
'In the click of next button move to the next available record
Private Sub cmdnext_Click()
rss.MoveNext
If rss.EOF Then rss.MoveLast
disp rss
End Sub
'Move to the previous record
Private Sub cmdprev_Click()
rss.MovePrevious
If rss.BOF Then rss.MoveFirst
disp rss
End Sub
Private Sub Form_Activate()
'Assign field name as null
fname = Null
'Set connection to the specified database
Set cnn = New ADODB.Connection
cnn.Provider = "Microsoft.jet.oledb.3.51"
cnn.Open "G:\sudhirvb\samp.mdb"
'Set a recordset for the table
Set rs = New ADODB.Recordset
rs.Open "samp", cnn, adOpenDynamic, adLockOptimistic, adCmdTable
'Set the line style of treeview control to root line
tv.LineStyle = tvwRootLines
'Clear the defaults nodes of treeview control
tv.Nodes.Clear
i = 0
'Set a loop for the available Fields of table
For Each fld In rs.Fields
'Add the name of the field as parant node
tv.Nodes.Add , tvwFirst, rs.Fields(i).Name, rs.Fields(i).Name
rs.MoveFirst
While rs.EOF <> True
'Add the values at the field as child node for that field
tv.Nodes.Add rs.Fields(i).Name, tvwChild, , rs.Fields(i).Value
rs.MoveNext
Wend
i = i + 1
Next fld
End Sub
Private Sub tv_NodeClick(ByVal Node As MSComctlLib.Node)
'If the user clicks on the parant node then save
'the field name into a variable
If Node.Children > 0 Then
fname = Node.Text
Else
'Else search for the record in the table
If fname <> "" Then
Set rss = New ADODB.Recordset
rss.CursorLocation = adUseClient
sql = "select * from samp where " & fname & " = '" & Node.Text & "';"
rss.Open sql, cnn, adOpenDynamic, adLockOptimistic, adCmdText
'Show the records in the data grid control
Set dg.DataSource = rss
'If number of records is greater than 1 then allow the user to
'navigate through records
If rss.RecordCount > 1 Then
FRANAV.Visible = True
Else
FRANAV.Visible = False
End If
disp rss
End If
End If
End Sub
'Fuction used for displaying the records
Public Sub disp(rs As ADODB.Recordset)
lname.Caption = rs!Name
lbrch.Caption = rs!brch
End Sub
No comments have been posted about Code for searching a record using Treeview control and ADODB, Records can be searched by any value . Why not be the first to post a comment about Code for searching a record using Treeview control and ADODB, Records can be searched by any value .