phone index

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:

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:

  1. Private Sub LoadCustomerData()
  2. ' for open the database file
  3. Dim strCon As String
  4. Dim strSql As String
  5. On Error GoTo LoadDataErr
  6. MyDataFile = App.Path & "\DataFile\" & "Northwind.mdb"
  7. strCon = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source=" & MyDataFile & ";"
  8. strSql = "SELECT * FROM Customers ORDER BY ContactName"
  9. Set cn = New ADODB.Connection
  10. cn.CursorLocation = adUseClient
  11. cn.Open strCon
  12. Set oCmd = New ADODB.Command
  13. Set oCmd.ActiveConnection = cn
  14. oCmd.CommandType = adCmdText
  15. Set rs = New ADODB.Recordset
  16. rs.Open strSql, cn, adOpenStatic, adLockOptimistic
  17. ' if no records then exit
  18. If rs.RecordCount = 0 Then
  19. MsgBox "No records in Customer table file!"
  20. Exit Sub
  21. End If
  22. SetTreeViewNodes
  23. Exit Sub
  24. LoadDataErr:
  25. MsgBox Err.Description
  26. End Sub
  27. Private Sub SetTreeViewNodes()
  28. 'set two fields (ContactName and Phone) as nodes
  29. Dim cName As String
  30. Dim cPhone As String
  31. Dim NameKey As String
  32. Dim PhoneKey As String
  33. Dim i As Integer
  34. ' set root node of TreeView, (image index = 3)
  35. Set dbNode = tvData.Nodes.Add(, , "RootDB", "Phone Index", 3)
  36. dbNode.Tag = "RootDB"
  37. rs.MoveFirst
  38. i = 0
  39. Do Until rs.EOF
  40. i = i + 1
  41. ' Set Contact Name, (image index = 1)
  42. cName = rs.Fields("ContactName").Value
  43. NameKey = "N" & Str(i)
  44. Set fldNameNode = tvData.Nodes.Add("RootDB", tvwChild, NameKey, cName, 1)
  45. fldNameNode.Tag = "Name"
  46. ' Set Customer Phone, (image index = 5)
  47. PhoneKey = "P" & Str(i)
  48. cPhone = rs.Fields("Phone").Value
  49. Set fldPhoneNode = tvData.Nodes.Add(NameKey, tvwChild, PhoneKey, cPhone, 5)
  50. rs.MoveNext
  51. Loop
  52. rs.MoveFirst
  53. End Sub
  54. Private Sub DisplayRecord(ByVal EmployeeName As String)
  55. 'Show some data about customer in TextBoxes:
  56. rs.MoveFirst
  57. 'find record:
  58. rs.Find ("ContactName = " & "'" & EmployeeName & "'")
  59. 'put the fields in TextBoxes:
  60. txtField(0).Text = rs.Fields(1).Value
  61. txtField(1).Text = rs.Fields(4).Value
  62. txtField(2).Text = rs.Fields(5).Value
  63. txtField(3).Text = rs.Fields(8).Value
  64. 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.