The
CreateForm() and
CreateReport() functions are the Visual Basic code equivalents of creating a new form or report in Design view. When you run these functions, they create a new blank form or report in a minimized state.
Both functions return an object value that you can use for further
manipulation, and neither function requires parameters.
The sample code in the paragraphs that follow applies to creating new forms
using the
CreateForm() function; however, the same information also
applies to creating reports with the
CreateReport() function.
To use the
CreateForm() function, first define a form object variable, and then assign the variable to the function name, as in the following sample code:
Public Function MakeNewForm()
Dim MyForm As Form
Set MyForm = CreateForm()
End Function
After the form is created, it is open in Design view. You can set or
change its properties, such as the
RecordSource property, at the same time as you create the form by adding a line to the function:
Public Function MakeNewForm()
Dim MyForm As Form
Set MyForm = CreateForm()
MyForm.RecordSource = "Categories"
End Function
You can also access and change the properties of each of the form's
sections using the
Section property. The
Section property is actually an array, with each array value referencing a section on the form. Form sections are stored in the
Section property array as follows:
Section(0) - Detail Section
Section(1) - Form Header
Section(2) - Form Footer
Section(3) - Page Header
Section(4) - Page Footer
Report sections are stored in the
Section property array as follows
Section(0) - Detail Section
Section(1) - Report Header
Section(2) - Report Footer
Section(3) - Page Header
Section(4) - Page Footer
Section(5) - Group Level 1 Header
Section(6) - Group Level 1 Footer
Section(7) - Group Level 2 Header
and so on.
With this information, you can customize the design of a form section
programmatically. The following sample code creates a new form and sets the
Height and
KeepTogether properties of the Detail section:
Public Function MakeNewForm()
Dim MyForm As Form
Set MyForm = CreateForm()
MyForm.Section(0).Height = 1760
MyForm.Section(0).KeepTogether = True
End Function