DefaultControl Property

Microsoft Access Visual Basic

object with which you can set the default properties for a particular type of control on a particular form or report. For example, before you create a text box on a form, you might want to set the default properties for the text box. Then you can create a number of text boxes that have the same base property settings, rather than having to set properties for each text box individually once it has been created.

expression.DefaultControl(controltype)

The DefaultControl property has the following arguments.

Argument Description
expression An expression that evaluates to a Form or Report object on which controls are to be created. The default property settings defined for a type of control apply only to controls of the same type created on this form or report.
controltype An intrinsic constant indicating the type of control for which default property settings are to be set.

Remarks

The DefaultControl property enables you to set a control's default properties from code. Once you have set the default properties for a particular type of control, each subsequently created control of that type will have the same default values.

For example, if you set the FontSize property of the default command button to 12, each new command button will have a font size of 12 points.

Not all of a control's properties are available as default properties. The default properties available for a control depend on the type of control.

The DefaultControl property returns a Control object of the type specified by the controltype argument. This Control object doesn't represent an actual control on a form, but rather a default control that is a template for all subsequently created controls of that type. You set the default control properties for the Control object returned by the DefaultControl property in the same manner that you would set properties for an individual control on a form.

For a list of intrinsic constants that can be passed as the controltype argument, see the CreateControl function.

The DefaultControl property can be used only in form Design view or report Design view. If you try to apply this property to a form or report that is not in Design view, a run-time error will result.

If you try to set a property that can't be set as a default property with the DefaultControl property, a run-time error will result. To determine which properties can be default properties, list the Properties collection of the Control object returned by the DefaultControl property.

Example

The following example creates a new form and uses the DefaultControl property to return a Control object representing the default command button. The procedure sets some of the default properties for the command button, then creates a new command button on the form.

Sub SetDefaultProperties()
    Dim frm As Form, ctlDefault As Control, ctlNew As Control

    ' Create new form.
    Set frm = CreateForm
    ' Return Control object representing default command button.
    Set ctlDefault = frm.DefaultControl(acCommandButton)
    ' Set some default properties.
    With ctlDefault
        .FontWeight = 700
        .FontSize = 12
        .Width = 3000
        .Height = 1000
    End With
    ' Create new command button.
    Set ctlNew = CreateControl(frm.Name, acCommandButton, , , , 500, 500)
    ' Set control's caption.
    ctlNew.caption = "New Command Button"
    ' Restore form.
    DoCmd.Restore
End Sub