Field has no value (1 Viewer)

ClaraBarton

Registered User.
Local time
Today, 08:41
Joined
Oct 14, 2019
Messages
768
On a treeview click I use this for matching the subform to the clickednode:
Code:
Public Sub MatchNode()
    Dim strKey          As String
    Dim ID              As Long
    Dim Children        As Integer

    Children = DCount("*", "tblItems", "flocID = " & Me.txtLocID & " And InUse = True")
    If Children = 0 Then
        subFrmItem.Visible = False
        strKey = "LO" & Me.txtLocID
    Else
        subFrmItem.Visible = True
'ItemID has no value

        strKey = "IT" & Nz(subFrmItem.ItemID, "")
    End If

    tvw.TreeView.Nodes(strKey).Selected = True
    tvw.TreeView.SelectedItem.EnsureVisible
    tvw.TreeView.DropHighlight = Me.tvw.SelectedNode

End Sub
The above code is run on a mainform as a current event for a subform: Private WithEvents subFrmItem As Access.Form
Mostly it works, but occasionally when the Children is greater than 0 and the subform was invisible, it returns the strKey has no value (meaning ItemID has no value). If I debug, and press F8 it continues on correctly.
I tried putting a DoEvents before it. No change.
Is there anything else? There isn't a specific error number I can use to ignore.
 
I'd keep routines like this somehow within scope when you work with treeviews...
Code:
Function GetNodeByKey(Key As String, Nodes As MSComctlLib.Nodes) As MSComctlLib.Node
On Error Resume Next
    Set GetNodeByKey = Nodes(Key)
End Function

Sub SelectNode(nd As MSComctlLib.Node)
    If nd Is Nothing Then Exit Sub
    nd.Selected = True
    nd.EnsureVisible
End Sub

Then you can just do...
Code:
    SelectNode GetNodeByKey(strkey, tvw.TreeView.Nodes)
 
but occasionally when the Children is greater than 0 and the subform was invisible, it returns the strKey has no value (meaning ItemID has no value).
If I read this correctly it does not have to do with the treeview but has to do with reading the ID of the current record in the subform
Code:
If Children = 0 Then
      
    Else
        strKey = "IT" & Nz(subFrmItem.ItemID, "")
    End If
So the question is why would the subFrmItem.itemID return no value? Well that code should never work because a subform control does not have a field. The form in the subform control does.
Code:
Me.SubfrmItem.Form.itemID
 
AAARRRGGGHHHH! I Knew That!
But wait... I screamed too soon
On open of the main form I have: Set subFrmItem = Me!ItemDetail.Form
Most of the time it actually does work. But jumping to make the form visible seems to show no valid ID.
It immediately works after it complains
 
Last edited:
It immediately works after it complains
So that does sound like a timing issue, and do events would be my first guess to. Often the fixes for timing issues are unexplainable. Like changing to focus or simply putting in a debug.print. Often figuring out where to put the dovents is also tricky

What is the real error?
My guess is when you make the subform visible there is a period of time when it is either on a new record with a null ID or no record has focus. Then it renders and the correct record is selected.

These are guesses but try these. Like I said sometimes simply moving focus, adding a debug, or setting focus can help with a timing issue.
Code:
subFrmItem.Visible = True
DoEvents
subfrmItem.ItemID.setfocus
DoEvents
debug.print nz(subfrmItem.itemID,"It is NULL")
....
 
I put both do Events and Set Focus and it seems to be working.
If I put only set focus it says you can't go to the specified record.
Thank you very much for your time.
 
I'm just curious why you need to rely on the data from the subform.

Is it not already contained in the treeview?

Certainly you ought to be able to get the number of clicked node's children directly, without having to perform an extra dlookup()

And aren't the id's required already stored within the nodes' keys?
 
Certainly you ought to be able to get the number of clicked node's children directly, without having to perform an extra dlookup()
It appears the flow is going in the opposite direction. My guess is the main form selects a location and the the sub form is in use items in the selected location. The user selects a location and if that location does not have any items the location node is selected. If the location has in use items then the item in the sub form which is the current record is selected in the tree. Synching the form with the tree.
 

Users who are viewing this thread

Back
Top Bottom