Because you won't see both tabs at the same time anyway you should only have one subform control under the tab control itself (on the main form not on a tab page).
Now add code to the Change event of the tab to change the source object of the subform to the corresponding form.
An alternative is to have code removing the source objects of all the subforms on all the tab pages but the current one, but depending on how many pages you have it could get complicated (I used to have a local table holding the names of the subform controls on each tab and their corresponding source objects).
Here is the code I used for the second option:
Code:
Option Compare Database
Option Explicit
Public Sub vcWriteSourceObject()
On Error Resume Next
Dim ctrl As Control, strSubform As String
Dim rst As DAO.Recordset
Set rst = CurrentDb.OpenRecordset("tmpRecordSourceLookup", dbOpenDynaset, dbSeeChanges)
For Each ctrl In Screen.ActiveForm.Controls
If ctrl.ControlType = acSubform Then
strSubform = ctrl.Name
rst.AddNew
rst("FormName") = Screen.ActiveForm.Name
rst("SubFormName") = ctrl.Name
rst("SourceObject") = ctrl.SourceObject
rst.Update
End If
Next
MsgBox "Done"
End Sub
Public Sub vcShowTabSubforms(strForm As String, strTabControl As String, lngPage As Long)
On Error GoTo vcShowTabSubforms_Error
Dim ctrl As Control
Application.Echo False
For Each ctrl In Forms(strForm).Form.Controls(strTabControl).Pages.Item(lngPage).Controls
If ctrl.ControlType = acSubform Then
ctrl.SourceObject = DLookup("[SourceObject]", "[tmpRecordSourceLookup]", "[FormName]= '" & strForm & "' AND [SubformName]= '" & ctrl.Name & "'")
End If
Next
Application.Echo True
vcShowTabSubforms_Exit:
Exit Sub
vcShowTabSubforms_Error:
Application.Echo True
End Sub
Public Sub vcHideTabSubforms(strForm As String, strTabControl As String)
On Error GoTo vcHideTabSubforms_Error
Dim ctrl As Control, pg As Page
Application.Echo False
For Each pg In Forms(strForm).Form.Controls(strTabControl).Pages
For Each ctrl In Forms(strForm).Form.Controls(strTabControl).Pages(pg.Name).Controls
If ctrl.ControlType = acSubform Then
ctrl.SourceObject = ""
End If
Next
Next pg
Application.Echo True
vcHideTabSubforms_Exit:
Exit Sub
vcHideTabSubforms_Error:
Application.Echo True
End Sub
To use if I would have this in the Change event of the tab:
Code:
Private Sub StudentInfoSub_Change() 'this is a tab control
vcUpdateSubforms
End Sub
Private Sub vcUpdateSubforms()
On Error Resume Next
Application.Echo False
Call vcHideTabSubforms(Me.Name, "StudentInfoSub")
Call vcShowTabSubforms(Me.Name, "StudentInfoSub", Me.StudentInfoSub.Value)
Application.Echo True
End Sub
Cheers,