Form Object, Report Object.
The DefaultControl method returns a Control object with which you can set the default properties for a particular type of control on a particular form. 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.
Set control = object.DefaultControl(controltype)
The DefaultControl method uses the following arguments.
Argument |
Description |
control |
A Control object for which the default properties are to be set. |
object |
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. |
The DefaultControl method 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 method 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 method 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. These constants are also listed in the Constants module of the Microsoft Access library in the Object Browser.
The DefaultControl method applies only to form Design view or report Design view. If you try to apply this method 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 method, a run-time error will result. To determine which properties can be default properties, enumerate the Properties collection of the Control object returned by the DefaultControl method.
Control Object; ControlType Property; CreateControl, CreateReportControl Functions; FontName, FontSize Properties.
The following example creates a new form and uses the DefaultControl method 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.RestoreSub