Element not found (2 Viewers)

ClaraBarton

Registered User.
Local time
Today, 12:02
Joined
Oct 14, 2019
Messages
764
@MajP I'm sorry but I can only work on this a little at a time so it's dragging out...
Item details (add, edit, remove, etc) are on a subform and the treeview is on the mainform. I don't delete items as such but only clear fields so that the DocNo stays and is used as items new are added or tacked on the end if no blank records. So I needed to rework the delete. I am deleting the node before I delete the item so that I can grab the node key. All seems very logical to me but so far not so much -- the line tvw.Nodes.Remove (ItemNode) returns element not found. The item node debugs the node text so I don't get what can't be found.
Code:
Public Sub DeleteItem()

    Dim strSql              As String
    Dim lDocNo              As Long
    Dim Position            As Long
       
Position = Me.CurrentRecord
strSql = "UPDATE tblItems SET" & _
                " InUse = 0," & _
                " Item = Null," & _
                " Purchased = Null," & _
                " AmountPaid = Null," & _
                " Notes = Null," & _
                " Modified = Date()" & _
                " WHERE ItemID = " & Me.ItemID
DeleteItemNode
SQLExecute (strSql)
    With Me.Recordset
        .Requery
         If Position > 1 Then
            .Move Position - 1
         Else
            .Move Position + 1
         End If
    End With
   
Me.Item.SetFocus
End Sub
Code:
Public Sub DeleteItemNode()

    Dim tvw                 As TreeviewForm
    Dim ParentNode          As Node
    Dim ItemNode            As Node
   
    Set tvw = GetItemTreeView
   
    Set ItemNode = tvw.Nodes("IT" & Me.ItemID)   'element not found
    Set ParentNode = tvw.Nodes("LO" & Me.fLocID)

    tvw.Nodes.Remove (ItemNode)
    ParentNode.Selected = True
    tvw.TreeView.DropHighlight = ParentNode
 
End Sub
 
Last edited:
The way I am able to relate the treeview and the tables and the nodes to the records is as follows:
1. I create the Key from an "Identifier" which tells me what table it came from concatenated to the real ID. So if I have more than one table like a location table and an item table and I would have keys like "LO1" and "IT1". Without this there would not be an easy way to have unique node IDs and know which table record it relates to
2. I store the Identifier part in the Tag Property
3. To get the PK and the table I use the Tag to identify from which table it came. I strip the identifier from the key and get the PK

I would debug this
Debug.print "IT" & Me.ItemID
and see what you get.
If ItemID is null you should see just "IT" and that would not be found.

Or, is "IT" really your identifier? Or Is it something like "IT-" or "Item"? I know in the Item Genie db I used "Item_"
 
Last edited:
IT and LO are my identifiers. So it seems like the string should be "IT57" but instead nodes returns the text name "Joint account papers". I was very confused by this but decided it was a Nodes value. So
Code:
Set ItemNode = tvw.Nodes("IT" & Me.ItemID)
the ("IT" & Me.ItemID) returns IT57
but tvw.Nodes("IT" & Me.ItemID) returns Joint account papers
I avoided the "SelectedNodes" because I'm changing the tree from a subform
 
Last edited:
still returns Joint account papers
Are you sure that it is actually returning the caption - how are you discovering this? Is it by hovering your mouse over the variable in break mode?

If so, the intellisense may just be showing the default property of the object, all the while the actual object is being returned in the code.
 
Although I didn't know there was a difference... You guys harp and harp DEBUG DEBUG DEBUG. I would not attempt a question here without DEBUGGING! :rolleyes:
 
As @cheekybuddha describes the default property of a node is the text property
so if you do debug.print tvw.nodes("somekey")
it should print the text of the node.
So if that is the case and it is printing out the text then it is finding the node.

Sorry, I did not read the full message carefully

Code:
tvw.Nodes.Remove (ItemNode)
It is the remove that is failing. I missed that.
That fails because it should be
Code:
tvw.Nodes.Remove (Some Key/index to a node not a Node object)
Code:
tvw.Nodes.Remove (ItemNode.KEY)
 
And to explain the error
Code:
tvw.Nodes.Remove (ItemNode)
'is the sames as
tvw.Nodes.Remove (ItemNode.defaultProperty)
'is the same as
tvw.Nodes.Remove ("Joint Account Papers")
' No node with a Key 'joint account papers'
 
Thanks guys! On to my next problem... The smallest things can bring everything to a screaming halt! 😜
Actually, now that I check more carefully, it looks as though the item is always a selected node so I may go that route.
 
Actually, now that I check more carefully, it looks as though the item is always a selected node so I may go that route.
I think I would not do that. That seems indirect if the tree and the form get out of synch you can be deleting the wrong node.
You are guranteed to be deleting the current record of the form the way you have it.

Instead I would make these more generic. You should handle all the form operations external to these such as setting focus and getting the item id


Code:
Public Sub DeleteItem(ItemID As Long)

    Dim strSql              As String
    Dim lDocNo              As Long
    Dim Position            As Long
      
    strSql = "UPDATE tblItems SET" & _
                " InUse = 0," & _
                " Item = Null," & _
                " Purchased = Null," & _
                " AmountPaid = Null," & _
                " Notes = Null," & _
                " Modified = Date()" & _
                " WHERE ItemID = " & ItemID
    
   SQLExecute (strSql)

End Sub

Public Sub DeleteItemNode(ItemID As Long)
    Dim tvw                 As TreeviewForm
    Dim ParentNode          As Node
    Dim ItemNode            As Node
  
    Set tvw = GetItemTreeView
    Set ItemNode = tvw.Nodes("IT" & ItemID)
    Set ParentNode = ItemNode.Parent
    
    tvw.Nodes.Remove (ItemNode.Key)
    ParentNode.Selected = True
    tvw.TreeView.DropHighlight = ParentNode
End Sub

Code:
Public Sub SomeFormEvent()
    Dim strSql              As String
    Dim lDocNo              As Long
    Dim Position            As Long
    Dim lItemID As Long
    
    Position = Me.CurrentRecord
    lItemID = Nz(Me.ItemID, 0)
    If ItemID <> 0 Then
      DeleteItem (ItemID)
      DeleteItemNode (ItemID)
      With Me.Recordset
        .Requery
         If Position > 1 Then
            .Move Position - 1
         Else
            .Move Position + 1
         End If
       End With
       Me.Item.SetFocus
    End If

End Sub

Also the deleteItem and deleteitemNode could use error checking and turn them into functions. They should return true if no error so you know the method completes.
 

Users who are viewing this thread

Back
Top Bottom