• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 768
  • Last Modified:

Replacing a node on a TreeView control


Semiconductor material grown in crystals; the crystals are then cut first into sections, then each section into wafers. A crystal of say, 350mm length, could be cut at 100mm and 200mm creating three sections labeled 0/100, 100/200, 200/350. Wafers cut from the 0/100 section are numbered 1, 2,...100; from the 100/200 section they're numbered 101, 102,...,200  and so on.

I am using a treeview control to display the records for the crystals. Top level (parent) is the crystal itself, next level (child) is sections of the crystal, and the last level (grandchild) are the wafers.

A crystal section can be taken from inventory and cut into two pieces. For example, the 100/200 section can be cut at 175 creating two sections: 100/175, and 175/200. Any wafer records for the original section must be edited so that they will stay associated with the appropriate new section (i.e. wafer 120 must end up a child of the 100/175 section, while wafer 180 must end up a child of the 175/200 section).

All of this works fine.  My problem is at the last step where I want to update the treeview to reflect the change. You can't just requery the control. Also (and please correct me if I'm wrong here), the treeview just represents a view of the data - changing the tree doesn't change the data. That is to say, the data manipulation and the treeview manipulation are done separately.  

I am attempting to:

1. delete the selected section from the treeview
2. add the two new sections in its place
3. populate each new section (add the wafers)

I am getting stuck, mainly I think with the treeview syntax.  Here's most of what I have so far...

Dim objTree As TreeView
Dim nCurrentTreeNode, currentnode, pnode As node

Set objTree = Forms!frmFabrication!tvCrystal.Object

<data manipulation code>

' treeview manipulation
        Set pnode = objTree.SelectedItem.Parent
        objTree.Nodes.Remove (objTree.SelectedItem.Key)
        Set currentnode = objTree.Nodes.Add(pnode, tvwChild, "c" & sectionID, sectionNo)
            'Add any wafers for this section.
 (*)   AddGrandChildren currentnode, currentnode.Key
        Set currentnode = objTree.Nodes.Add(pnode, tvwChild, "c" & newsectionID, newsectionNo)
        'Add any wafers for this section.
        AddGrandChildren currentnode, currentnode.Key
        currentnode.Expanded = True
        currentnode.Selected = True

<then come closing statements>

<The AddGrandChildren routine...>

Sub AddGrandChildren(nodBoss As node, CSN)
    On Error GoTo ErrAddGrandChildren

    Dim nodCurrent As node
    Dim objTree As TreeView
    Dim bk As String
    Dim db As Database
    Dim rst As Recordset

    'Create a reference to the TreeView control.
    Set objTree = Me!tvCrystal.Object
    Set db = CurrentDb()
    Set rst = db.OpenRecordset("SELECT * FROM qryFabricationList2 WHERE [XtalSectionID]=" & CSN)
    Do Until rst.EOF
        'Extract the wafer name.
        strText = rst![FabWaferNo]
        'Add as a child node to the tree.
        Set nodCurrent = objTree.Nodes.Add(nodBoss, tvwChild, "g" & _
            rst![FabWaferNo], rst![WaferNumber])
        'Find the next section.
    Set rst = Nothing

    Exit Sub

    MsgBox "Can't add child:  " & Err.Description, vbCritical, _
        "AddGrandChildren(nodBoss As Node) Error:"
    Resume ExitAddGrandChildren
End Sub

<end code>

The code gets to (*) and then I get an error: ByRef argument type mismatch.

Where am I going wrong?


  • 2
  • 2
1 Solution
Dim nCurrentTreeNode As Node, currentnode As Node, pnode As node

At present, your first two vars are dimmed as variants, not node objects. Does this make a difference?
firebird-scAuthor Commented:
Yes! It did make a difference.  I thought the way I had it before it would set all three to Node objects.

thank you very much! I was stuck on that for a while and there's not a lot out there on the TreeView to reference.

It's actually a limitation of VBA. Unless you include the As <data type> against each variable name, the variable is declared as a variant. For example:

Dim I,J,K As Long
will only dim K as long, the other two are variants. You'd need:
Dim I As Long, J As Long, K As Long
firebird-scAuthor Commented:
It's never given me a problem until now - I guess historically most of my variables have been variants!  

In ASP, on the other hand, I get errors when I INCLUDE the "As <datatype>" part, so I write all declarations:

dim var1
dim var2
dim var3

Seems kind of loose and sloppy to me, but it works.

Thanks again.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Train for your Pen Testing Engineer Certification

Enroll today in this bundle of courses to gain experience in the logistics of pen testing, Linux fundamentals, vulnerability assessments, detecting live systems, and more! This series, valued at $3,000, is free for Premium members, Team Accounts, and Qualified Experts.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now