Control Object

Microsoft Access Visual Basic

Control Object

         
Multiple objects Control
Multiple objects

The Control object represents a control on a form, report, or section, within another control, or attached to another control.

Using the Control Object

All controls on a form or report belong to the Controls collection for that Form or Report object. Controls within a particular section belong to the Controls collection for that section. Controls within a tab control or option group control belong to the Controls collection for that control. A label control that is attached to another control belongs to the Controls collection for that control.

When you refer to an individual Control object in the Controls collection, you can refer to the Controls collection either implicitly or explicitly.

' Implicitly refer to NewData control in Controls
' collection.
Me!NewData
' Use if control name contains space.
Me![New Data]
' Performance slightly slower.
Me("NewData")
' Refer to a control by its index in the controls
' collection.
Me(0)
' Refer to a NewData control by using the subform
' Controls collection.
Me.ctlSubForm.Controls!NewData
' Explicitly refer to the NewData control in the
' Controls collection.
Me.Controls!NewData
Me.Controls("NewData")
Me.Controls(0)

Note   You can use the Me keyword to represent a Form or Report object within code only if you're referring to the form or report from code within the class module. If you're referring to a form or report from a standard module or a different form's or report's module, you must use the full reference to the form or report.

Each Control object is denoted by a particular intrinsic constant. For example, the intrinsic constant acTextBox is associated with a text box control, and acCommandButton is associated with a command button. The constants for the various Microsoft Access controls are set forth in the control's ControlType property.

To determine the type of an existing control, you can use the ControlType property. However, you don't need to know the specific type of a control in order to use it in code. You can simply represent it with a variable of data type Control.

If you do know the data type of the control to which you are referring, and the control is a built-in Microsoft Access control, you should represent it with a variable of a specific type. For example, if you know that a particular control is a text box, declare a variable of type TextBox to represent it, as shown in the following code.

Dim txt As TextBox
Set txt = Forms!Employees!LastName

Note   If a control is an ActiveX control, then you must declare a variable of type Control to represent it; you cannot use a specific type. If you're not certain what type of control a variable will point to, declare the variable as type Control.

The option group control can contain other controls within its Controls collection, including option button, check box, toggle button, and label controls.

The tab control contains a Pages collection, which is a special type of Controls collection. The Pages collection contains Page objects, which are controls. Each Page object in turn contains a Controls collection, which contains all of the controls on that page.

Other Control objects have a Controls collection that can contain an attached label. These controls include the text box, option group, option button, toggle button, check box, combo box, list box, command button, bound object frame, and unbound object frame controls.