|
|
Question : Sync multiple subforms
|
|
I have a main form related to a table containing client information [tblClientInfo] PK is [infSSN]. On the main form I am using tab control with three seperate subforms containing different areas of information [sfrmClientInfo] [sfrmLegalInfo] [sfrmClassInfo]. The three subforms are all related to a second table with PK [infCaseNumber]. The tables have a one-to-many relationship. My problem is that the subforms do not sync up. I need for the second and third subforms to reflect the record selected on the first subform. I have read about using a hidden text box but can't get it to work.
|
Answer : Sync multiple subforms
|
|
That's right.
Create a textbox on the main form. Set its ControlSource =SyncSubs([sub1]![ID],[sub2]![ID]) where sub1 and sub2 are the names of the subform controls (not the subformes themselves) and ID is the primary key of each subform.
Then under menu Tools, References establish a reference to DAO 3.60.
Now, create this function in the code module of the main form:
Private Function SyncSubs( _ ByVal varID1 As Variant, _ ByVal varID2 As Variant) _ As Variant
Dim rst As DAO.Recordset Static varIDc1 As Variant Static varIDc2 As Variant Dim bmk As Variant Dim varID As Variant If IsNull(varID1) Or IsNull(varID2) Then ' New record. Don't sync. ElseIf varID1 = varID2 Then ' Initial setting. varIDc1 = varID1 varIDc2 = varID2 Else If varID1 <> varIDc1 Then ' sub1 has moved. Sync sub2. Set rst = Me!sub2.Form.RecordsetClone rst.FindFirst "ID = " & Me!sub1.Form!ID If Not rst.NoMatch Then bmk = rst.Bookmark Me!sub2.Form.Bookmark = bmk varID = varID1 End If rst.Close End If If varID2 <> varIDc2 Then ' sub2 has moved. Sync sub1. Set rst = Me!sub1.Form.RecordsetClone rst.FindFirst "ID = " & Me!sub2.Form!ID If Not rst.NoMatch Then bmk = rst.Bookmark Me!sub1.Form.Bookmark = bmk varID = varID2 End If rst.Close End If End If If Not IsEmpty(varID) Then varIDc1 = varID varIDc2 = varID End If Set rst = Nothing SyncSubs = varID
End Function
Of course, adjust the names of the subform controls and the IDs to those of yours.
/gustav
|
|
|
|
|