65.9K
CodeProject 正在变化。 阅读更多。
Home

使用 TreeView 和 TextBox 显示 MDB 文件中的记录

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.22/5 (8投票s)

2009 年 10 月 26 日

CPOL

2分钟阅读

viewsIcon

45746

downloadIcon

2146

使用 TreeView 和 TextBox 控件显示、添加、编辑、删除记录

引言

我之前写过一篇文章,关于使用 TreeView 控件、ListView 控件和 ADO 显示来自数据库文件 (*.mdb) 的数据,使用 TreeView 显示表名和字段名,ListView 显示用户从 TreeView 中选择该表的任何表的所有记录。如果您点击这里,您可以阅读这篇文章。

在这篇新文章中,我用一组文本框替换 ListView,以逐个显示来自数据库文件 (MyPhone.mdb) 的记录,该数据库文件包含一个表 (Phone),其中包含三个字段 (Name, Address, PhoneNumber) 作为电话索引。

现在,我尝试使用 TreeView 控件和 TextBox 控件显示我的电话索引,并且我尝试向我的电话索引添加新记录,或编辑任何记录的任何字段,或从我的电话索引中删除不需要的记录。

背景

我的项目 (prjPhoneIndex) 有一个窗体 (frmPhone)。我将以下控件添加到我的窗体

  • TreeView (tvData) 显示(Name)和(PhoneNumber
  • 三个作为数组的 TextBoxes (txtField(0), txtField(1), txtField(2)) 显示任何记录的字段
  • ImageList (ImageList1) 为我们的 TreeView 加载一些图标
  • 七个按钮
    • cmdLoadData 用数据库文件填充 TreeView
    • cmdAdd 向数据库文件添加新记录并将此记录添加到 TreeView
    • cmdEdit 重写任何字段
    • cmdUpdate 在添加新记录或编辑任何记录后保存记录
    • cmdCancel 取消添加或取消编辑
    • cmdDelete 从数据库文件删除记录并从 TreeView 中删除此记录
    • cmdClose 结束显示

Using the Code

第一步:连接数据库文件

' You must define other variables in the Declarations part (see the code in my Form):
Dim strCon As String
Dim strSql As String

   ' In (Form_Load) procedure, we shall connect with database file:
    MyDataFile = App.Path & "\DataFile\" & "MyPhone.mdb"
    strCon = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source=" & MyDataFile & ";"
    strSql = "SELECT * FROM Phone ORDER BY Name"
    Set cn = New ADODB.Connection
    cn.CursorLocation = adUseClient
    cn.Open strCon
    Set oCmd = New ADODB.Command ' for delete undesirable records
    Set oCmd.ActiveConnection = cn
    oCmd.CommandType = adCmdText
    Set rs = New ADODB.Recordset
    rs.Open strSql, cn, adOpenStatic, adLockOptimistic

第二步:将 TreeView 控件与数据绑定(一个节点用于客户姓名,另一个节点用于电话号码)。

' Other variables are defined in Declarations part (see the code in my Form):
Dim CustName As String
Dim n As Integer
Dim c As Integer
Dim i As Integer
Dim r As Long

   ' See this code in (cmdLoadData_Click) procedure:
   ' set root node of TreeView.
   Set dbNode = tvData.Nodes.Add(, , "RootDB", "Phone Index", "imgIndexClose")
   dbNode.Tag = "RootDB"
   ' set table node:
   Set tabNode = tvData.Nodes.Add("RootDB", tvwChild, "PhoneTable", "Hello...", _
      & "imgPhoneClose")
   tabNode.Tag = "Table"
   ' start to set customer name as children of table node:
   c = tvData.Nodes("RootDB").Children
   ' first (Name)
   n = tvData.Nodes("RootDB").Child.FirstSibling.Index
   ' is database file empty?
   If rs.RecordCount = 0 Then
      Screen.MousePointer = vbDefault
      MsgBox "No records in database file, use (Add) to add rcords."
      Exit Sub  ' if database file empty exit sub.
   End If
   rs.MoveFirst
   For i = n To c + n
      ' get the field (Name of customer)
      Do Until rs.EOF
         CustName = rs.Fields("Name").Value
         ' display the field (Name of customer) as new Node
         Set fldNameNode = tvData.Nodes.Add("PhoneTable", tvwChild, "R-" & CustName, _
            & CustName, "imgRecord")
         fldNameNode.Tag = "Records"
         ' display phone number as new Node:
         r = rs.AbsolutePosition ' use this number as Key if we need.
         If rs.Fields("PhoneNumber").Value <> "" Then
            Set fldPhoneNode = tvData.Nodes.Add("R-" & CustName, tvwChild, _
               "N_" & Str(r), rs.Fields("PhoneNumber").Value, "imgPhoneOpen")
         End If
         rs.MoveNext
      Loop
   Next i
   rs.MoveFirst

第三步:当用户单击客户姓名节点时会发生什么?

Dim nodTag As String
Dim CustName As String
Dim i As Integer

   ' In (tvData_NodeClick) procedure we shall fill TextBox controls 
   ' with three Fields (Name, Address and PhoneNumber):
   nodTag = Node.Tag
   If nodTag = "Records" Then ' user click the node of customer name
      'read customer name
      CustName = Node.Text
      ' find customer name
      rs.MoveFirst
      rs.Find ("Name = " & "'" & CustName & "'")
      ' put the fields in TextBoxes
      For i = 0 To rs.Fields.Count - 1
         If rs.Fields(i).Value <> "" Then
            txtField(i).Text = rs.Fields(i).Value
         Else
            txtField(i).Text = ""
         End If
      Next i
   End If

您可以在 Form (frmPhone) 中阅读完整的代码

  • Form_Load Sub:连接数据库文件
  • cmdLoadData_Click Sub:设置 TreeView 的节点
  • tvData_Collapse Sub:更改图标
  • tvData_Expand Sub:更改图标
  • tvData_NodeClick Sub:查看如何用字段填充 TextBoxes(您可以在展开时看到电话号码,无需单击)
  • cmdAdd_Click Sub:添加新记录
  • cmdEdit_Click Sub:重写任何字段
  • cmdDelete_Click Sub:删除记录
  • cmdUpdate_Click Sub:在添加或编辑后保存记录
  • cmdCancel_Click Sub:取消添加或编辑

备注

当您创建一个新项目时,您必须从“组件”对话框中将 TreeViewImageList 控件添加到 ToolBox
Microsoft Windows Common Controls 6.0
您必须添加引用:“Microsoft ActiveX Data Objects 2.x”。

最后的话

我希望这篇文章对您有用,并帮助您使用 TreeViewTextBox 显示数据库中的记录。
如果您有任何想法或发现任何问题,请告诉我。感谢 The Code Project,感谢大家。

历史

  • 2009 年 10 月 26 日:初始发布

Mostafa Kaisoun
M_Kaisoun@hotmail.com

© . All rights reserved.