Name Property

Applies To

CheckBox control, ComboBox control, CommandButton control, Font object, Image control, Label control, ListBox control, MultiPage control, OptionButton control, Page object, ScrollBar control, SpinButton control, Tab object, TabStrip control, TextBox control, ToggleButton control, UserForm object.

Description

Specifies the name of a control or an object, or the name of a font to associate with a Font object.

Syntax

For Font:

Font.Name [= String]

For all other controls and objects:

object.Name [= String]

The Name property syntax has these parts:

Part

Description

object

Required. A valid object.

String

Optional. The name you want to assign to the font or control.


Settings

Guidelines for assigning a string to Name, such as the maximum length of the name, vary from one application to another.

Remarks

For objects, the default value of Name consists of the object's class name followed by an integer. For example, the default name for the first TextBox you place on a form is TextBox1. The default name for the second TextBox is TextBox2.

You can set the Name property for a control from the control's property sheet or, for controls added at run time, by using program statements. If you add a control at design time, you cannot modify its Name property at run time.

Each control added to a form at design time must have a unique name.

For Font objects, Name identifies a particular typeface to use in the text portion of a control, object, or form. The font's appearance on screen and in print may differ, depending on your computer and printer. If you select a font that your system can't display or that isn't installed, Windows substitutes a similar font.

See Also

Bold, Italic, Size, StrikeThrough, Underline, Weight properties.

Example

The following example displays the Name property of each control on a form. This example uses the Controls collection to cycle through all the controls placed directly on the Userform.

To use this example, copy this sample code to the Declarations portion of a form. Make sure that the form contains a CommandButton named CommandButton1 and several other controls.

Private Sub CommandButton1_Click()
    Dim MyControl As Control

    For Each MyControl In Controls
        MsgBox "MyControl.Name = " & MyControl.Name
    Next
End Sub