I have three related tables -
tblGroups- GroupID(PK), GroupName
tblSubGroups -SubGroupID(PK), SubGroupName,GroupIDFK(FK)
tblItems - ItemID(PK), ItemName,SubGroupIDFK(FK)
I have set the form-subform1-subform2 and they are alright.
I am using a treeview to load all the records in above tables and they are displayed correctly. The treeview node having ItemName as text has its tag set to the ItemID (PK) while other nodes do not have a tag.
I am trying to find a event when clicking on node will search for that record on the subform2. I tried with the nodeclick event. The event returns
the ItemID (Node.Tag) correctly but it required 2 or 3 clicks to navigate to that particular record.
To check if there is any error in my code.
I used the same code in AfterUpdate event of a Combo Box whose row source is the tblItems and it works perfectly (finds and navigates to correct record).
Code:
Private Sub cmbItems_AfterUpdate()
ShowRecord (Me.cmbItems)
End Sub
Private Sub TreeView0_NodeClick(ByVal Node As Object)
If Len(Node.Tag) <> 0 Then
Me.cmbItems = Node.Tag
ShowRecord (Node.Tag)
End If
End Sub
Sub ShowRecord(ItemID As Long)
Dim S As String, grID As Long, subGrID As Long
subGrID = Me.cmbItems.Column(1) ' earlier tried DLookup("SubGroupFK", "tblItems", "[ItemID]=" & ItemID)
grID = Me.cmbItems.Column(2) ' earlier tried DLookup("GroupIDFK", "tblSubGroups", "[SubGroupID]=" & subGrID)
S = "Group " & grID & vbCrLf
S = S & "Sub Group " & subGrID & vbCrLf
S = S & "Item " & ItemID
Msgbox S ' this returns correct PK of all the tables
'find group
Me.RecordsetClone.FindFirst "GroupID=" & Me.cmbItems.Column(2)
If Not Me.RecordsetClone.NoMatch Then
Me.Bookmark = Me.RecordsetClone.Bookmark
End If
'find SUB group
Me.SubForm1.SetFocus
Me.SubForm1.Form.RecordsetClone.FindFirst "SubGroupID=" & Me.cmbItems.Column(1)
If Not Me.SubForm1.Form.RecordsetClone.NoMatch Then
Me.SubForm1.Form.Bookmark = Me.SubForm1.Form.RecordsetClone.Bookmark
End If
'find itemcode
Me.SubForm1.Form!SubForm2.SetFocus
Me.SubForm1.Form.SubForm2.Form.RecordsetClone.FindFirst "ItemID=" & ItemID
If Not Me.SubForm1.Form.SubForm2.Form.RecordsetClone.NoMatch Then
Me.SubForm1.Form.SubForm2.Form.Bookmark = Me.SubForm1.Form.SubForm2.Form.RecordsetClone.Bookmark
End If
End Sub
What event or design changes in case of Treeview will achieve the same ? Your suggestions are welcome.