Solved Majp Treeview (1 Viewer)

ClaraBarton

Registered User.
Local time
Today, 06:13
Joined
Oct 14, 2019
Messages
494
Me again...
This is the code that was actually calling the Object Variable Error when the form opens that I was complaining about yesterday. I went back to your treeview and noted that this code is under AfterUpdate on frmE2E. I marked it to step through and couldn't ever get it to run so I changed it to Current and received the same error.
The error comes at Me.TVW.TreeView.Nodes(strKey).Selected = True. The version I'm testing is MajP TreeviewDemo v19_Full_CutPaste.accdb. I don't have to have this event but I like that it ties the treeview to the detail subform when the record changes.
I noted that each line throws the same error.

Code:
Private Sub cmboNode_Current()
  Dim strKey
  On Error GoTo errlbl
  If Not IsNull(Me.cmboNode) Then
    strKey = Me.cmboNode.Value
     Me.TVW.TreeView.Nodes(strKey).Selected = True
     Me.TVW.TreeView.SelectedItem.EnsureVisible
     Me.TVW.TreeView.DropHighlight = Me.TVW.SelectedNode
     Me.cmboNode = Null
  End If
  Exit Sub
errlbl:
  If Err.Number = 35601 Then
    MsgBox "The following key: " & strKey & " is not found or not Expanded yet. You can select Expand Tree."
  Else
    MsgBox Err.Number & " " & Err.Description & " In CmboNode Afterupdate."
  End If
 
End Sub
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 09:13
Joined
May 21, 2018
Messages
8,748
noted that this code is under AfterUpdate on frmE2E.
In the demo CmboNode is a combobox and this event takes place in the after update of the combo and not the form.
This allows you to select a record from the combo and select it in the tree. A combo box does not have a current event so no idea what you are trying to do there.

If this code somehow runs prior to instantiating the class in the forms load event then
Me.TVW.TreeView.Nodes(strKey).Selected = True
could give an object not set error since TVW is set in the forms load event. If some reason this is in a subform and you call it before the main form loads.

But as I said, does not seem to make any sense to me using a current event of a combo box.
 

ClaraBarton

Registered User.
Local time
Today, 06:13
Joined
Oct 14, 2019
Messages
494
The treeview is the form with detail view as the subform. So when I move through the records on the subform, the treeview updates
Code:
Private Sub Form_Current()
On Error GoTo errlbl
Dim strKey As String

  strKey = "IT" & Me.ItemID
     Me.Parent.TVW.Nodes(strKey).Selected = True
     Me.Parent.TVW.SelectedItem.EnsureVisible
     Me.Parent.TVW.DropHighlight = Me.Parent.TVW.SelectedNode
  Exit Sub

errlbl:
  If Err.Number = 35601 Then
    MsgBox "The following key: " & strKey & " is not found or not Expanded yet. You can select Expand Tree."
  Else
    MsgBox Err.Number & " " & Err.Description & " "
  End If
End Sub
1719596623348.png
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 09:13
Joined
May 21, 2018
Messages
8,748
the treeview is the form with detail view as the subform. So when I move through the records on the subform, the treeview updates
I believe that the subform's on current event may happen before the mainform loads. Which means TVW is not yet instantiated.

In the subform you check to see if the main form is loaded.

Code:
If currentProject.allforms("MainFormName").isloaded then
    strKey = "IT" & Me.ItemID
     Me.Parent.TVW.Nodes(strKey).Selected = True
     Me.Parent.TVW.SelectedItem.EnsureVisible
     Me.Parent.TVW.DropHighlight = Me.Parent.TVW.SelectedNode
end if
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 09:13
Joined
May 21, 2018
Messages
8,748
Sometimes you want an event procedure to happen repeatedly, but you do not want it to start until a certain time or you want it to stop happening at a give time. Another trick is to add/remove the event procedure hook.

For example if you want something to take place in the current event of the subform but do not want that to start happening until the main form loads. You can remove "[event procedure]' from the current event.
In the main form you could then set this
me.subformControlName.Form.oncurrent = "[Event Procedure]"
The current event in the subform is not trapped until the main form loads and tells it to trap the event.
 

ClaraBarton

Registered User.
Local time
Today, 06:13
Joined
Oct 14, 2019
Messages
494
Ok. I get it.
Still getting object variable not set whenever the form opens...
Code:
Private Sub Form_Current()
On Error GoTo errlbl
Dim strKey As String

  If CurrentProject.AllForms("frmDetail").IsLoaded Then
    strKey = "IT" & Me.ItemID
       Me.Parent.TVW.Nodes(strKey).Selected = True
       Me.Parent.TVW.SelectedItem.EnsureVisible
       Me.Parent.TVW.DropHighlight = Me.Parent.TVW.SelectedNode
  End If
  Exit Sub

errlbl:
  If Err.Number = 35601 Then
    MsgBox "The following key: " & strKey & " is not found or not Expanded yet. You can select Expand Tree."
  Else
    MsgBox Err.Number & " " & Err.Description & " "
  End If
End Sub
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 09:13
Joined
May 21, 2018
Messages
8,748
Does the error message box come up. If you close out the msgbox does the code continue and work?
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 09:13
Joined
May 21, 2018
Messages
8,748
another trick you can do (assuming this is the problem) is have the main form trap the subform event and move the code into the main form. This is another way to ensure the subform event is not trapped untile the TVW is instantiated in the main form.

at the top of your form add
Code:
private withevents subFrm as access.form

in the main forms on load
Code:
set subFrm = me.nameofsubformcontrol.form
subfrm.oncurrent = "[Event Procedure]"

Now you should be able to add the event in the main form

Code:
Private SubFrm_Current()
        strKey = "IT" & subfrm.ItemID
       TVW.Nodes(strKey).Selected = True
       TVW.SelectedItem.EnsureVisible
       TVW.DropHighlight
= SelectedNode
end sub
 

Gasman

Enthusiastic Amateur
Local time
Today, 14:13
Joined
Sep 21, 2011
Messages
14,718
Why not set some sort of flag when the object is set and test for that?
 

ClaraBarton

Registered User.
Local time
Today, 06:13
Joined
Oct 14, 2019
Messages
494
ok. Folowed your instructions to the T:
Code:
Me.Parent.TVW.Nodes(strKey).Selected = True
Error box message: 91 object variable or with block variable not set. Pressing enter moves on.
This is only when the form is opening. Moving from record to record does not raise this error.
 
Last edited:

ClaraBarton

Registered User.
Local time
Today, 06:13
Joined
Oct 14, 2019
Messages
494
Oh Great! I did it again... spoke without knowing my stuff. I put the current event on the detail form when you meant for me to put it on the Form. I have it fixed and I'm not getting that error anymore and I thank you very much!
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 09:13
Joined
May 21, 2018
Messages
8,748
I am still surprised that checking to see if the main form was loaded did not work. However, I do have a question.
Code:
If CurrentProject.AllForms("frmDetail").IsLoaded Then
Is frmDetail the name of the main form or is that the name of the subform? By the name "Detail" it looks like the subform.
 

ClaraBarton

Registered User.
Local time
Today, 06:13
Joined
Oct 14, 2019
Messages
494
No. the main form of frmDetail. the subform is fsubdetail. Detail means it's not the list view but the detail of each record. It is working great now. Tested and retested. ;)
 

Users who are viewing this thread

Top Bottom