I programmatically create a Tab Control with a few tabs. Each tab contains a different instance (I believe) of the same subform. I want each instance to have a diffent RecordSource, but they all seem to end up with the same recordsource. Not sure where to look.
-----------------------------------------
Where would we be if we didn't try?
Code:
Public Function CreateMyControl(iStation As Integer, iGroupSite As Integer) As Boolean
Const adhcTwipsPerInch As Long = 1440
' Dim frm As Form
Dim intWidth As Integer
Dim intHeight As Integer
Dim intTop As Integer
Dim intLeft As Integer
Dim intTopLabel As Integer
Dim intHeightLabel As Integer
Dim intWidthLabel As Integer
Dim intTabWidth As Integer
Dim intTabHeight As Integer
Dim intTabTop As Integer
Dim intTabLeft As Integer
Dim rst As ADODB.Recordset
Dim strSQL As String
Dim MyControlType As AcControlType
Dim frm As Form
Dim ctl As Control
Dim iSite As Integer
Dim sABC As String
Dim sA As String
Dim cTabCtl As TabControl
Dim cSubFrm As SubForm
Dim sLabelCaption As String
Set frm = Forms!frm_Data_Entry_Main!sf_frm_T_Checklist.Form
intWidth = 7.5833 * adhcTwipsPerInch
intHeight = 2.5729 * adhcTwipsPerInch
intTop = 0.3889 * adhcTwipsPerInch
intLeft = 0.1493 * adhcTwipsPerInch
intTopLabel = intTop - (0.1875 * adhcTwipsPerInch)
intHeightLabel = 0.1875 * adhcTwipsPerInch
intWidthLabel = 4 * adhcTwipsPerInch
'If first item in the recordset, remove TabCtl.
'If NOT first item, create TabCtl, subform controls and define source object.
strSQL = "SELECT * FROM LU_Site WHERE FK_Station = " & iStation
Set rst = New Recordset
rst.Open strSQL, CurrentProject.Connection, _
adOpenKeyset, adLockPessimistic
If ControlExists("TabCtlSites", frm) Then
Set cTabCtl = frm!TabCtlSites
End If
If iGroupSite = 1 Then
'Show Sites All Together (if more than one), so no Tab Control needed. _
Delete the Tab Control and _
Put subform here instead of tab control.
If ControlExists("TabCtlSites", frm) Then
Call RemoveControl(frm.Name, "TabCtlSites")
Else
If DCount("ID", "LU_Site", "FK_Station = " & iStation) = 1 Then
frm!lblSubform.Caption = "Checklist"
Else
frm!lblSubform.Caption = "Checklist for all Sites combined"
End If
Exit Function
End If
With CreateControl( _
FormName:=frm.Name, _
ControlType:=acSubform, _
Section:=acDetail, _
Left:=intLeft, _
Top:=intTop, _
Width:=intWidth, _
Height:=intHeight)
' Set the name, SourceObject and recordsource.
.Name = "sf_subfrm_Temp_Bird0"
.SourceObject = "subfrm_Temp_Bird"
' On Error Resume Next
.Form.RecordSource = "SELECT * FROM Q_T_Bird_Data_Entry_Tabs"
End With
If ControlExists("sf_subfrm_Temp_Bird0", frm) Then
Set cSubFrm = frm!sf_subfrm_Temp_Bird0
End If
sLabelCaption = "Checklist for all Sites combined"
If rst.RecordCount = 1 Then sLabelCaption = "Checklist"
With CreateControl( _
FormName:=frm.Name, _
ControlType:=acLabel, _
Section:=acDetail, _
Parent:=cSubFrm.Name, _
Left:=intLeft, _
Top:=intTopLabel, _
Width:=intWidthLabel, _
Height:=intHeightLabel)
' Set the name and caption.
.Name = "lblSubform"
.Caption = sLabelCaption
End With
Else
'Show Sites separately, so we need Tab Control. _
Delete the one Subform and _
Create tab control.
If ControlExists("sf_subfrm_Temp_Bird0", frm) Then
Call RemoveControl(frm.Name, "sf_subfrm_Temp_Bird0")
Else
Exit Function
End If
intTabWidth = 7.8229 * adhcTwipsPerInch
intTabHeight = 3.4167 * adhcTwipsPerInch
intTabTop = 0 * adhcTwipsPerInch
intTabLeft = 0 * adhcTwipsPerInch
With CreateControl( _
FormName:=frm.Name, _
ControlType:=acTabCtl, _
Section:=acDetail, _
Left:=intTabLeft, _
Top:=intTabTop, _
Width:=intTabWidth, _
Height:=intTabHeight)
' Set the name
.Name = "TabCtlSites"
End With
sABC = "A"
iSite = 1
While Not rst.EOF
If rst.AbsolutePosition > 2 Then
Call AddPage(rst.AbsolutePosition, sABC, rst.Fields("TheName"))
'Add new pages
Else
'Edit existing pages.
Call RenamePage(rst.AbsolutePosition, sABC, rst.Fields("TheName"))
End If
'Add the Subform control.
sABC = AutoIncr(sABC)
With CreateControl( _
FormName:=frm.Name, _
ControlType:=acSubform, _
Section:=acDetail, _
Parent:="Page" & rst.AbsolutePosition, _
Left:=intLeft, _
Top:=intTop, _
Width:=intWidth, _
Height:=intHeight)
' Set the name, SourceObject and recordsource.
.Name = "sf_subfrm_Temp_Bird" & rst.AbsolutePosition
.SourceObject = "subfrm_Temp_Bird"
' On Error Resume Next
[COLOR=#ff0000].Form.RecordSource = "SELECT * FROM Q_T_Bird_Data_Entry_Tabs WHERE Site_ID = " & iSite[/color]
End With
rst.MoveNext
iSite = iSite + 1
Wend
rst.Close
End If
CreateMyControl = True
End Function
-----------------------------------------
Where would we be if we didn't try?