Sub CreateTheForm()
Dim frmNew As Access.Form
Dim ctlNew As Access.Control
Dim intSection As Integer
CreateTheForm_AddForm:
Set frmNew = CreateForm
DoCmd.Maximize
frmNew.HasModule = True
CreateTheForm_InspectSize:
'You can inspect the current form size and change the dimensions
'if you want
Debug.Print "Maximized form inside size (twips) is " & _
Format$(frmNew.InsideWidth, "#,##0") & "w X " & _
Format$(frmNew.InsideHeight, "#,##0") & "h"
'Cycle through the sections in the form to see if they exist
'and what their height is. Sections could also be added and
'resized here.
'NOTE: Use inline error handling to detect if the section is present
On Error Resume Next
For intSection = 0 To 4
If frmNew.Section(intSection).Visible Then
If Err.Number = 0 Then
Debug.Print Choose(intSection + 1, "Detail", "Header", "Footer", "Page Header", "Page Footer") & _
" height is " & _
Format$(frmNew.Section(intSection).Height, "#,##0") & "h"
Else
Debug.Print "Form has no " & _
Choose(intSection + 1, "Detail", "Header", "Footer", "Page Header", "Page Footer")
Err.Clear
End If
End If
Next intSection
On Error GoTo 0
CreateTheForm_AddControl:
'Create a command button in the details section
Set ctlNew = CreateControl(frmNew.Name, acCommandButton, acDetail)
With ctlNew
.OnClick = "[Event Procedure]"
End With
CreateTheForm_AddControlCode:
'Add the OnClick event for this button
frmNew.Module.AddFromString "Private Sub " & ctlNew.Name & "_Click()" & _
vbCrLf & " MsgBox " & Chr(34) & "Hello" & Chr(34) & _
vbCrLf & "End Sub"
CreateTheForm_Cleanup:
DoCmd.Close acForm, frmNew.Name, acSaveYes
Set ctlNew = Nothing
Set frmNew = Nothing
End Sub