Option Compare Database
Option Explicit
Public Function fCreate_subfrm()
Dim frm As Form
Dim subfrm As Control
Call fDelete_Form 'If subform already exists, delete it
Set frm = CreateForm() 'Create subform in memory
With frm
.Caption = "My Form"
.RecordSource = "qryNDRS_Results"
.DefaultView = 2 'Datasheet View
End With
Call fGet_Result_Columns 'Determine which fields will be populating the subform
DoCmd.Save
DoCmd.Close acForm, "form1", acSaveYes 'Needs to be unloaded to place it on the parent form
Set frm = Nothing
DoCmd.OpenForm "frmResults_Container", acDesign 'Open parent form
Set subfrm = Forms!frmResults_Container!subfrmResults
subfrm.SourceObject = "form1"
DoCmd.Save
Set subfrm = Nothing
End Function
Public Function fGet_Result_Columns()
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim qry As DAO.QueryDef
Dim fldcount, i As Long
Set db = CurrentDb
Dim ctl As Control
Set rs = db.OpenRecordset("SELECT TOP 1 * FROM qryNDRS_Results") 'Get a small recordset
fldcount = rs.Fields.Count 'so that we can Count
For Each qry In db.QueryDefs 'Column Headings
If qry.Name = "qryNDRS_Results" Then 'Find the query, get the Column Names
For i = 0 To (fldcount - 1)
Set ctl = CreateControl("form1", acTextBox, acDetail, , _
db.QueryDefs("qryNDRS_Results").Fields(i).Name) 'Create controls equal to
ctl.Name = db.QueryDefs("qryNDRS_Results").Fields(i).Name 'Number of query fields
ctl.Visible = True
Next
End If
Next
Set ctl = Nothing
Set rs = Nothing
Set db = Nothing
End Function
Public Function fDelete_Form() 'Find subform if it exists by looping through
Dim frm As Object 'the forms collection and then delete it
For Each frm In Application.CurrentProject.AllForms
If frm.Name = "Form1" Then
DoCmd.DeleteObject acForm, "Form1"
Exit For
End If
Next
Set frm = Nothing
End Function