Hary,
Cole este código no módulo e veja se é isto:
Option Compare Database
Option Explicit
Dim MsgErro As String
Dim Incluir As Boolean
Dim StrTMP As String
'====================================
'Funções para treeView
'------------------------------------
' Define public objects
Dim rsContas As DAO.Recordset
Dim rsContasDet As DAO.Recordset
Dim rsContasDetSub As DAO.Recordset
Dim StrSQL, StrSQL1 As String
'=====================================
'-------------------------
Private Sub Form_Current()
'-------------------------
' form open with focus in search field
Me.FindTipoConta.SetFocus
End Sub
Private Sub Form_Load()
'===========================================================
'Função para treeview
With Me.myTreeView
' set style property to value that allows pictures
.Style = tvwTreelinesPlusMinusPictureText
' set to automatically expand selected node and collapse previous node
'.SingleSel = True ' Single Selection
End With
' initialize tree
TreeInit
End Sub
'===================================================================================================================
'Funções para treeView
'===================================================================================================================
'----------------------
Private Sub TreeInit()
'----------------------
' Code per example by Robert Kirchner
' microsoft.public.access.activexcontrol
' Help on treeview (detailed)
Dim trvTree As Control
Dim imgList As Control
Dim nodObject As node
Dim I As Integer
Dim rcount As Integer
Dim strContas As String
Dim strContasDetalhe As String
Dim strContasDetalheSub As String
Dim Db As DAO.Database
' set database and recordset objects
Set Db = CurrentDb
Set trvTree = Me.myTreeView
Set imgList = Me.MyImageList
' set Treeview control ImageList property to Image List Object
' treeview object style was previously set to 7-TvwTreelinesPlusMinusPictureText
' and the images were loaded manually into the imagelist control. Rather
' than use the image keys "frame" and "form", the images'
' index numbers are used when adding nodes.
trvTree.ImageList = imgList.Object
' use two recordsets, one for the parent and the other for the
' child. First create parent node with the linking field as
' a root node key. Next find the first and subsequent children
' matching the parent's linking field. Use the parent node's
' node key as the child node's relative value. When you run out
' of children, move to next parent record and then find its children,
' and so forth until there are no more parents. I supose this
' concept could be used with a third recordset to find children of
' the second recordset, and so on.
With trvTree.Nodes
' clear any nodes, if any
.Clear
' open parent and child records
Set rsContas = Db.OpenRecordset("Conta", dbOpenSnapshot)
rsContas.MoveLast
rcount = rsContas.RecordCount
rsContas.MoveFirst
'Set rsContasDet = Db.OpenRecordset("ContaDetalhes", dbOpenSnapshot)
'Set rsContasDetSub = Db.OpenRecordset("ContaDetalhesSub", dbOpenSnapshot)
' loop through recordset and add nodes
Do While Not rsContas.EOF
strContas = rsContas!ID_conta
' concatenate fields to create the parent node text value.
If Len(Trim(Nz(rsContas!PlanoConta))) > 0 Then
strContas = Format(strContas, ">") & ", " & rsContas!PlanoConta
End If
' .. e adiciona o noode do tipo de conta no Treeview
Set nodObject = .Add(, , "A" & CStr(rsContas!ID_conta), strContas, 1)
'StrSQL = "ID_Conta = " & rsContas!ID_conta
'find first child, if any, belonging to this parent
'rsContasDet.FindFirst StrSQL
'on Error GoTo NoContaDetalhe
' concatenate fields to create the child node text value.
'While Not (rsContasDet.NoMatch)
'Set rsContasDet = Db.OpenRecordset("ContaDetalhes", dbOpenSnapshot)
Set rsContasDet = Db.OpenRecordset("SELECT * FROM ContaDetalhes WHERE [ID_Conta] = " & rsContas!ID_conta, dbOpenSnapshot)
Do While Not rsContasDet.EOF
If IsNull(rsContasDet!ID_conta) Then GoTo NoContaDetalhe
'strContasDetalhe = rsContasDet!TipoConta
If Len(Trim(Nz(rsContasDet!TipoConta))) > 0 Then
strContasDetalhe = rsContasDet!TipoConta
End If
' .. and add thee child node to the Treeview
Set nodObject = .Add("A" & rsContas!ID_conta, tvwChild, "C" & rsContasDet!ID_Detalhes, strContasDetalhe, 2)
'StrSQL1 = "ID_Detalhes = " & rsContasDet!ID_Detalhes
'.. and create the node tag property holding key to child's baptism
trvTree.Nodes("C" & rsContasDet!ID_Detalhes).Tag = rsContasDet!ID_Detalhes
' Now find next child, if any
'rsContasDet.FindNext StrSQL
'==========================================================================================================================
Set rsContasDetSub = Db.OpenRecordset("SELECT * FROM ContaDetalhesSub WHERE ID_Detalhes = " & rsContasDet!ID_Detalhes, dbOpenSnapshot)
Do While Not rsContasDetSub.EOF
If IsNull(rsContasDetSub!ID_DetalhesSub) Then GoTo NoContaDetalheSub
If Len(Trim(Nz(rsContasDetSub!Descricao))) > 0 Then
strContasDetalheSub = rsContasDetSub!Descricao
End If
' .. and add thee child node to the Treeview
'Set nodObject = .Add("A" & rsContas!ID_conta, tvwChild, "C" & rsContasDet!ID_Detalhes, strContasDetalhe, 2)
Set nodObject = .Add("C" & rsContasDet!ID_Detalhes, tvwChild, "E" & rsContasDetSub!ID_DetalhesSub, strContasDetalheSub, 3)
'.. and create the node tag property holding key to child's baptism
trvTree.Nodes("E" & rsContasDetSub!ID_DetalhesSub).Tag = rsContasDetSub!ID_DetalhesSub
' Now find next child, if any
NoContaDetalheSub:
'rsContasDetSub.FindNext StrSQL1
rsContasDetSub.MoveNext
' .. and loop back and add next child
'============================================================================================================================
Loop
NoContaDetalhe:
' não move Tipo de Conta, move para proximo plano Conta
rsContasDet.MoveNext
Loop
' não move Tipo de Conta, move para proximo plano Conta
rsContas.MoveNext
Loop
End With
Set rsContas = Nothing
Set rsContasDet = Nothing
End Sub
'----------------------------
Private Sub cmdSearch_Click()
'----------------------------
Dim strSearch As String
' if search field contains a value
If Not IsNull(FindTipoConta) Then
strSearch = Me.FindTipoConta.Value
' .. find a node like the value
MyTreeview_FindNodeLike (strSearch)
End If
'.. and set the focus to Treeciew control
Me.myTreeView.SetFocus
End Sub
'-----------------------------------
Private Sub FindChild_AfterUpdate()
'-----------------------------------
' if search field has a value
If Not IsNull(Me.FindTipoConta) Then
' .. then call search button click event
cmdSearch_Click
End If
End Sub
'-------------------------------
Private Sub MyTreeView_DblClick()
'-------------------------------
' call sub to react to double-click. Number 2 indicates
' that call is being made by a double-click action
Call DisplayForm(2)
End Sub
'-------------------------------
Private Sub MyTreeView_Click()
'-------------------------------
' call sub to react to double-click. Number 2 indicates
' that call is being made by a double-click action
Call DisplayForm(2)
End Sub
'---------------------------------------------
Function MyTreeview_FindNode(strKey As String)
'---------------------------------------------
' define a control
Dim trvTree As Control
' .. and set to Treeview
Set trvTree = Me.myTreeView
' make node selected
trvTree.Nodes(strKey).Selected = True
' .. and insure node is visible
trvTree.Nodes(trvTree.SelectedItem.Index).EnsureVisible
' and expand selected node, if desired
'trvTree.Object.SelectedItem.Expanded = True
End Function
'-----------------------------------------------
Function MyTreeview_FindNodeLike(C As String)
'-----------------------------------------------
' set up SQL string to select a father like search string
StrSQL = "SELECT ID_Detalhes,TipoConta from ContaDetalhes WHERE [TipoConta] LIKE """ & _
Me.FindTipoConta & "*"" ORDER BY TipoConta"
' open recordset
Set rsContasDet = CurrentDb.OpenRecordset(StrSQL)
' .. and it recordset contains no records then exit
If rsContasDet.BOF Or rsContasDet.EOF Then Exit Function
' .. otherwise move to first record
rsContasDet.MoveFirst
' .. and trigger event to find that node
MyTreeview_FindNode ("C" & CStr(rsContasDet!ID_Detalhes))
End Function
'------------------------------------------------------------------------
Private Sub MyTreeview_KeyDown(KeyCode As Integer, ByVal Shift As Integer)
'------------------------------------------------------------------------
' pressing space is same action as double-click
If (KeyCode = vbKeyShift) And acShiftMask Then
' call sub to react to shift key press. Number 1 indicates
' that call is being made by a press of shift key
Call DisplayForm(1) 'MyTreeView_DblClick
End If
End Sub
'----------------------------
Sub DisplayForm(I As Integer)
'----------------------------
' for this Treeview I do not want a double-click on a parent node
' to open the applicable marriage form, but pressing the shift key
' should open the marriage form applicable to the parent node. Shift
' or double-click on a child node should open the child's applicable
' baptism form.
Dim strKey As String
Dim strTag As String
Dim strFilter As String
' get key of selected node
strKey = Me.myTreeView.SelectedItem.Key
' .. then get node's tag proterty
strTag = Nz(Me.myTreeView.Nodes(strKey).Tag, "")
' .. then if there is a tag value
If Len(strTag) > 0 Then
'.. then get the initial letter of node key
Select Case Left(strKey, 1)
' .. and open the the appropriate form filter by tag value
Case "A"
' if parent node then a double-click expands node
' but does not open marriage form
If I = 2 Then Exit Sub
'strFilter = "DoCmd.OpenForm ""frmMarriages"", , , " & _
"RecordNo = """ & strTag & """"
Case "C"
Me.txtIDLst = strTag
Me.txtConta = DLookup("TipoConta", "ContaDetalhes", "ID_Detalhes =" & strTag)
End Select
End If
End Sub