Form Object, Forms Collection Example

The following example creates a new form and sets certain properties:

Sub NewForm()
    Dim frm As Form

    ' Create new form.
    Set frm = CreateForm
    ' Set form properties.
    With frm
        .RecordSource = "Products"
        .Caption = "Products Form"
        .ScrollBars = 0
        .NavigationButtons = True
    End With
    ' Restore form.
    DoCmd.Restore
End Sub

The next example enumerates the Forms collection and prints the name of each form in the Forms collection. It then enumerates the Controls collection of each form and prints the name of each control on the form.

Sub AllOpenForms()
    Dim frm As Form, ctl As Control

    ' Enumerate Forms collection.
    For Each frm In Forms
        ' Print name of form.
        Debug.Print frm.Name
        ' Enumerate Controls collection of each form.
        For Each ctl In frm.Controls
            ' Print name of each control.
            Debug.Print ">>>"; ctl.Name
        Next ctl
    Next frm
End Sub

The following example uses the Forms collection in an expression. If you have a form called Orders with a control named ShipCountry, you can use the IIf function to return a value based on the current value of the control. If the value of ShipCountry is Null, the expression returns a zero-length string; otherwise, it returns the field's contents. Enter the following expression in the ControlSource property of a text box on a form:

= IIf(IsNull(Forms!Orders! ShipCountry), "", _
     Forms!Orders!ShipCountry)