This is a trial to read records from MDB database file using TreeView control. My project is a phone index to display some fields from Customers table in Northwind database file:
CompanyName, ContactName, Address, City, Country and Phone.
My project has one form (frmPhone), this form has following controls:
- TreeView: tvData
- Four TextBoxes:
txtField(0) for Company Name, txtField(1) for Address, txtField(2) for City and , txtField(3) for Country.
Two Buttons: for load data and exit application.
Code
There is three procedures:
- Private Sub LoadCustomerData()
-
- Dim strCon As String
- Dim strSql As String
- On Error GoTo LoadDataErr
- MyDataFile = App.Path & "\DataFile\" & "Northwind.mdb"
- strCon = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source=" & MyDataFile & ";"
- strSql = "SELECT * FROM Customers ORDER BY ContactName"
- Set cn = New ADODB.Connection
- cn.CursorLocation = adUseClient
- cn.Open strCon
- Set oCmd = New ADODB.Command
- Set oCmd.ActiveConnection = cn
- oCmd.CommandType = adCmdText
- Set rs = New ADODB.Recordset
- rs.Open strSql, cn, adOpenStatic, adLockOptimistic
-
- If rs.RecordCount = 0 Then
- MsgBox "No records in Customer table file!"
- Exit Sub
- End If
- SetTreeViewNodes
- Exit Sub
- LoadDataErr:
- MsgBox Err.Description
- End Sub
-
- Private Sub SetTreeViewNodes()
-
- Dim cName As String
- Dim cPhone As String
- Dim NameKey As String
- Dim PhoneKey As String
- Dim i As Integer
-
- Set dbNode = tvData.Nodes.Add(, , "RootDB", "Phone Index", 3)
- dbNode.Tag = "RootDB"
- rs.MoveFirst
- i = 0
- Do Until rs.EOF
- i = i + 1
-
- cName = rs.Fields("ContactName").Value
- NameKey = "N" & Str(i)
- Set fldNameNode = tvData.Nodes.Add("RootDB", tvwChild, NameKey, cName, 1)
- fldNameNode.Tag = "Name"
-
- PhoneKey = "P" & Str(i)
- cPhone = rs.Fields("Phone").Value
- Set fldPhoneNode = tvData.Nodes.Add(NameKey, tvwChild, PhoneKey, cPhone, 5)
- rs.MoveNext
- Loop
- rs.MoveFirst
- End Sub
-
- Private Sub DisplayRecord(ByVal EmployeeName As String)
-
- rs.MoveFirst
-
- rs.Find ("ContactName = " & "'" & EmployeeName & "'")
-
- txtField(0).Text = rs.Fields(1).Value
- txtField(1).Text = rs.Fields(4).Value
- txtField(2).Text = rs.Fields(5).Value
- txtField(3).Text = rs.Fields(8).Value
- End Sub
Summary
I hope that this article be useful in case of use TreeView control, so I invite you to go to source file to read the code about TreeView events. If you have any idea about this code, please tell me. Thanks for C# Corner team and thanks for all.