Populate an array with a recursive procedure

Posted on 2006-05-11
Last Modified: 2012-08-13
I have a routine that will collect Node.KEY values from a multi-node TreeView using a recursive procedure (ensures all nodes below the selected node are captured - node depth is indeterminate).

Question:  I want to populate the Node.KEY value into an array that I will dump to a Scratch table once the routine is complete.  This procedure is triggered by the .NodeCheck event

I am not quite sure the best way to index and populate the array via a recursive function.


1.  NodeCheck event calls my function
2.  CheckNodes calls itself as follows:

Private Sub TreeStructure_NodeCheck(ByVal Node As Object)
    Dim NodeCount as Integer

    NodeCount = Me!TreeView.Nodes.Count
    CheckNodes Node, Node.Checked
End Sub
Private Sub CheckNodes(ByRef oParentNode As Node, ByVal bChecked As Boolean)

'Checkbox all the child nodes for the checked node

    Dim oNode As Node
    Dim txtNodeKey as String
    txtNodeKey=oParentNode.Key  '---> want to send this value to an array (1 X NodeCount)

    Set oNode = oParentNode.Child
    Do While Not oNode Is Nothing
        oNode.Checked = bChecked
        CheckNodes oNode, bChecked
        Set oNode = oNode.Next
End Sub

 - Should I DIM the array at the NodeCheck event?
 - Set the Array Index = 0 in NodeCheck event?
 - Increment the Index in CheckNodes function?
 - Will the Array and its Index be availble to the CheckNodes function? - Make PUBLIC?

Any suggestions?
Question by:markp99
    LVL 65

    Expert Comment

    urm, what if u created a module level variable

    try something like this

    Dim m_myNodeValues() As String
    Dim m_iNodeCount As Integer

    on top of your form/module

    then just before your initial call to CheckNode, set the variables

        ReDim m_myNodeValues(20)
        m_iNodeCount = 0

    20 would be your node code

    then as you apply a value to your array, increment m_iNodeCount

    useful to put validation checks in to avoid overflows

    if m_iNodeCount = ubound(m_myNodeValues) then
       debug.print "max reached"
    elseif m_iNodeCount > ubound(m_myNodeValues) then
       debug.print "problem"


    Author Comment


    You have me curious about Module Level Variables.  I've not used them before; I susually go PUBLIC.

    Can you elaborate just a bit?  Any special scope/delcaration concerns?

    LVL 65

    Accepted Solution

    ok, public would do

    if u define dim at the top of your form code, then that variable is only visible to that particular form

    prefixing it with public means all forms and all modules can see it

    I dont think thats necessary hence just use dim


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    When you are entering numbers in a speadsheet, and don't remember what 6×7 is, you just type “=6*7" instead. It works in every cell! This is not so in Access. To enter the elusive 42 in a text box, you have to find a calculator, and then copy the re…
    Regardless of which version on MS Access you are using, one of the harder data-entry forms to create is one where most data from previous entries needs to be appended to new records, especially when there are numerous fields and records involved.  W…
    Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
    What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.

    794 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now