selecting record in parent subform from child continuous subform

driver7408

Registered User.
Local time
Yesterday, 18:42
Joined
Feb 7, 2010
Messages
72
I have two subforms on a main form. Both use similar queries and nearly the same set of records and PK. The first subform is for data entry and the 2nd subform is a continuous form that lists the entries in order that are made from the 1st Subform. (for entering in vacation days and appointments)
The continuous form cannot be edited, it is to be a list for viewing the information only.

I have an edit button next to each record on the continuous form. When the button is clicked, I want it to take the 1st subform to that specific record as well (same PK), so the information can be edited there.

I cannot figure out how to get the 1st subform to go to the record on the continuous form when the button next to that record is clicked on the continuous form.

I tried the DoCmdSearch for record and just keep getting object is not open errors.

Any ideas which method would work the best in this situation?
 
1) Put this routine on the editable subform. It gives the form the capability to receive an ID from a consumer, and do its own navigation . . .
Code:
Public Sub GoToID(RecordID as Long)
   With Me.RecordsetClone
      .FindFirst "RecordID = " & RecordID
      If Not .NoMatch Then Me.Bookmark = .Bookmark
   End With
End Sub
. . . and of course you replace the field name in the .FindFirst line with the name of the ID field on your form.

2) Now, expose that subform as a Public Property of the parent so the other child can see it, like this, so this code goes on the parent form . . .
Code:
Public Property Get MyEditableSubform As Form_MyFormName
[COLOR="Green"]'   Exposes a strongly typed reference to the editable continuous subform
'   inside the subform control
[/COLOR]    Set MyEditableSubform = Me.MyEditableSubformControl.Form
End Property
. . . and you replace the names of the things as required.

3) Finally, the button click on your viewing subform. This subform is a sibling of the editable subform, both hosted by the same parent, and so communication between them must go via that parent. Luckily, the parent exposes exactly the subform you want to talk to, as the Public Property from step 2). Now you can write code for that button click like . . .
Code:
Private Sub cmdEdit_Click()
[COLOR="Green"]'   Call the GoToID method of the other subform exposed by the parent, 
'   passing in the ParentID from the current record in the viewer[/COLOR]
    Me.Parent.MyEditableSubform.GoToID Me.RecordID
End Sub
Let me know if you hit any snags. It should work very smoothly, and it makes it simple for other consumers to run the same task. See how you invoke it with only one line of code on the consumer? And each object in the chain is essentially taking care of its own problems, which is what you want.
 
Thanks. I actually ran nearly the same as that first set of code on the click event of the continuous subform on Parent.RecordsetClone and it worked without needing any separate call function. I will keep that in mind though if I experience any further snags.
 

Users who are viewing this thread

Back
Top Bottom