Sunday, June 17, 2012

Contoh Mengisi ListView Secara Recursive

Function FirstFileMatch()

Dim strFileName As String
On Error Resume Next

strFileName = Dir(InputBox("Enter a valid path and file name."))
If strFileName = "" Then
FirstFileMatch = FirstFileMatch()
Else
FirstFileMatch = strFileName
End If

End Function

Private Sub Form_Load()
Const strTableQueryName = "Employees"
Dim db As DAO.Database, rst As DAO.Recordset
Set db = CurrentDb
Set rst = db.OpenRecordset(strTableQueryName, dbOpenDynaset, dbReadOnly)
AddBranch rst:=rst, strPointerField:="ReportsTo", strIDField:="EmployeeID", strTextField:="LastName"
End Sub

Sub AddBranch(rst As Recordset, strPointerField As String, strIDField As String, strTextField As String, Optional varReportToID As Variant)
On Error GoTo errAddBranch
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
strCriteria = strPointerField & " Is Null"
Else
strCriteria = BuildCriteria(strPointerField, rst.Fields(strPointerField).Type, "=" & varReportToID)
Set nodParent = objTree.Nodes("a" & varReportToID)
End If

rst.FindFirst strCriteria
Do Until rst.NoMatch
strText = rst(strTextField)
strKey = "a" & rst(strIDField)
If Not IsMissing(varReportToID) Then
Set nodCurrent = objTree.Nodes.Add(nodParent, tvwChild, strKey, strText)
Else
Set nodCurrent = objTree.Nodes.Add(, , strKey, strText)
End If
bk = rst.Bookmark
AddBranch rst, strPointerField, strIDField, strTextField, rst(strIDField)
rst.Bookmark = bk
rst.FindNext strCriteria
Loop

exitAddBranch:
Exit Sub

errAddBranch:
MsgBox "Can"
Resume exitAddBranch
End Sub