Set Form, Report, and Control Properties in Visual Basic

Set Form, Report, and Control Properties in Visual Basic

See Also

Form, Report, and Control objects are Microsoft Access objects. You can set properties for these objects from within a Sub, Function, or event procedure. You can also set properties for form and report sections.

To set a property of a form or report

Refer to the individual form or report within the Forms or Reports collection, followed by the name of the property and its value. For example, to set the Visible property of the Customers form to True (–1), use the following line of code:

Forms!Customers.Visible = True

You can also set a property of a form or report from within the object's module by using the object's Me property. Code that uses the Me property executes faster than code that uses a fully qualified object name. For example, to set the RecordSource property of the Customers form to an SQL statement that returns all records with a CompanyName field entry beginning with "A" from within the Customers form module, use the following line of code:

Me!RecordSource = "SELECT * FROM Customers " _
    & "WHERE CompanyName Like 'A*'"

To set a property of a control

Refer to the control in the Controls collection of the Form or Report object on which it resides. You can refer to the Controls collection either implicitly or explicitly, but the code executes faster if you use an implicit reference. The following examples set the Visible property of a text box called CustomerID on the Customers form:

' Faster method.
Me!CustomerID.Visible = True
' Slower method.
Forms!Customers.Controls!CustomerID.Visible = True

The fastest way to set a property of a control is from within an object's module by using the object's Me property. For example, you can use the following code to toggle the Visible property of a text box called CustomerID on the Customers form:

With Me!CustomerID
    .Visible = Not .Visible
End With

To set a property of a form or report section

Refer to the form or report within the Forms or Reports collection, followed by the Section property and the integer or constant that identifies the section. The following examples set the Visible property of the page header section of the Customers form to False (0):

Forms!Customers.Section(3).Visible = False
Me!Section(acPageHeader).Visible = False

Notes