Access交流中心

北京 | 上海 | 天津 | 重庆 | 广州 | 深圳 | 珠海 | 汕头 | 佛山 | 中山 | 东莞 | 南京 | 苏州 | 无锡 | 常州 | 南通 | 扬州 | 徐州 | 杭州 | 温州 | 宁波 | 台州 | 福州 | 厦门 | 泉州 | 龙岩 | 合肥 | 芜湖 | 成都 | 遂宁 | 长沙 | 株洲 | 湘潭 | 武汉 | 南昌 | 济南 | 青岛 | 烟台 | 潍坊 | 淄博 | 济宁 | 太原 | 郑州 | 石家庄 | 保定 | 唐山 | 西安 | 大连 | 沈阳 | 长春 | 昆明 | 兰州 | 哈尔滨 | 佳木斯 | 南宁 | 桂林 | 海口 | 贵阳 | 西宁 | 乌鲁木齐 | 包头 |

请帮忙修改下面的ACCESS递归

向阳路001号  发表于:2008-12-18 10:34:50  
复制

Private Sub Form_Load()
Dim cnn As New ADODB.Connection
Dim rst As New ADODB.Recordset
Set cnn = CurrentProject.Connection
rst.Open "雇员", cnn, adOpenKeyset, adLockOptimistic
AddBranch rststr:=rst, strPointerField:="上级", strIDField:="雇员ID", strTextField:="姓氏"
End Sub

Sub AddBranch(rststr As ADODB.Recordset, strPointerField As String, _
              strIDField As String, strTextField As String, _
              Optional varReportToID As Variant)
   Dim nodCurrent As Node, objTree As TreeView
   Dim strCriteria As String, strText As String, strKey As String
   Dim nodParent As Node, bk As String
   Set objTree = Me!xTree.Object
   If IsMissing(varReportToID) Then  ' Root Branch.
      strCriteria = strPointerField & "=null"
   Else  ' Search for records pointing to parent.
      strCriteria = BuildCriteria(strPointerField, _
           rststr.Fields(strPointerField).Type, "=" & varReportToID)
      Set nodParent = objTree.Nodes("a" & varReportToID)
   End If

      ' Find the first emp to report to the boss node.
   rststr.Find strCriteria
   Do Until rststr.EOF And rststr.BOF
         ' Create a string with LastName.
      strText = rststr(strTextField)
      Debug.Print strText
      strKey = "a" & rststr(strIDField)
      If Not IsMissing(varReportToID) Then  'add new node to the parent
         Set nodCurrent = objTree.Nodes.Add(nodParent, tvwChild, strKey, strText)
      Else    ' Add new node to the root.
         Set nodCurrent = objTree.Nodes.Add(, , strKey, strText)
      End If
         ' Save your place in the recordset so we can pass by ref for speed.
      bk = rststr.Bookmark
      Debug.Print bk
         ' Add employees who report to this node.
      AddBranch rststr, strPointerField, strIDField, strTextField, rststr(strIDField)
      Debug.Print "执行"
      rststr.Bookmark = bk    ' Return to last place and continue search.
      rststr.Find strCriteria, 1, adSearchForward, bk ' Find next employee.
   Loop

exitAddBranch:
   Exit Sub

errAddBranch:
   MsgBox "Can't add child:  " & Err.Description, vbCritical, "AddBranch Error:"
   Resume exitAddBranch
End Sub

 

Top
向阳路001号 发表于:2008-12-18 10:35:58

数据库是ACCESS2003的罗斯文示例数据库



向阳路001号 发表于:2008-12-21 18:59:16

没有回答自己解决!

Private Sub Form_Load()
Dim cnn As New ADODB.Connection
Dim rst As New ADODB.Recordset
Set cnn = CurrentProject.Connection
rst.Open "雇员", cnn, adOpenKeyset, adLockOptimistic
AddBranch rststr:=rst, strPointerField:="上级", strIDField:="雇员ID", strTextField:="姓氏"
End Sub

Sub AddBranch(rststr As ADODB.Recordset, strPointerField As String, _
              strIDField As String, strTextField As String, _
              Optional varReportToID As Variant)
   Dim nodCurrent As Node, objTree As TreeView
   Dim strCriteria As String, strText As String, strKey As String
   Dim nodParent As Node, bk As Variant
   Set objTree = Me!xTree.Object
   If IsMissing(varReportToID) Then  ' Root Branch.
      strCriteria = strPointerField & "=null"
   Else  ' Search for records pointing to parent.
      strCriteria = BuildCriteria(strPointerField, _
           rststr.Fields(strPointerField).Type, "=" & varReportToID)
      Set nodParent = objTree.Nodes("a" & varReportToID)
   End If

      ' Find the first emp to report to the boss node.
   rststr.movefirst

   rststr.Find strCriteria
   Do Until rststr.EOF And rststr.BOF
         ' Create a string with LastName.
      strText = rststr(strTextField)
      Debug.Print strText
      strKey = "a" & rststr(strIDField)
      If Not IsMissing(varReportToID) Then  'add new node to the parent
         Set nodCurrent = objTree.Nodes.Add(nodParent, tvwChild, strKey, strText)
      Else    ' Add new node to the root.
         Set nodCurrent = objTree.Nodes.Add(, , strKey, strText)
      End If
         ' Save your place in the recordset so we can pass by ref for speed.
      bk = rststr.Bookmark
      Debug.Print bk
         ' Add employees who report to this node.
      AddBranch rststr, strPointerField, strIDField, strTextField, rststr(strIDField)
      Debug.Print "执行"
      rststr.Bookmark = bk    ' Return to last place and continue search.
      rststr.Find strCriteria, 1, adSearchForward, bk ' Find next employee.
   Loop

exitAddBranch:
   Exit Sub

errAddBranch:
   MsgBox "Can't add child:  " & Err.Description, vbCritical, "AddBranch Error:"
   Resume exitAddBranch
End Sub



总记录:2篇  页次:1/1 9 1 :