ACC2000: How to Use VBA to Set Default Control Properties

This article was previously published under Q200593
This article has been archived. It is offered "as is" and will no longer be updated.
Advanced: Requires expert coding, interoperability, and multiuser skills.

This article shows you how to use Visual Basic for Applications to setproperties for default controls on forms and reports.
More information
Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements. In order to set properties for default controls, you must use the form'sDefaultControl method. Note that, although you can use this method to set properties for default controls, not all of a control's properties are available as default properties.

The following properties apply only to default controls and cannot be setfor a control that's already on the form or report:

  • AddColon
  • AutoLabel
  • LabelAlign
  • LabelX
  • LabelY
  • TextAlign
To programmatically set the properties for text box controls, follow thesesteps:
  1. Open the sample database Northwind.mdb.
  2. Create a module and type the following line in the Declarations section if it is not already there:
    Option Explicit					
  3. Type the following procedure in the module:
    Sub SetDefaultProperties()   Dim frm As Form   Dim ctl As Control   Set frm = CreateForm()   Set ctl = frm.DefaultControl(acTextBox)   With ctl       ' Add a label for new textboxes.       .AutoLabel = True       ' Do not include a colon in labels for new textboxes.       .AddColon = False       ' Align label text to the right for new textboxes.       .LabelAlign = 3       ' Place labels for new textboxes 1/4 inch above       ' and aligned to the left.       .LabelX = 1440 * .33       .LabelY = -1440 * .25       ' Center text in new textboxes.       .TextAlign = 2   End With   DoCmd.SelectObject acForm, frm.Name, False   DoCmd.RestoreEnd Sub					
  4. To test this procedure, type the following line in the Immediate window, and then press ENTER:
    Note that a new, blank form opens in Design view.
  5. Add a new text box to the form.Note that the label for the new text box does not include a colon, has right aligned text, and appears 1/4 inch above and is left aligned with thetext box. Also, note that the text in the new text box is now centered bydefault.
For more information about using Visual Basic for Applications to set the properties of default controls, click Microsoft Access Help on the Help menu, type DefaultControl method in the Office Assistant or the Answer Wizard, and then click Search to view the topics returned.

Article ID: 200593 - Last Review: 10/25/2013 23:14:00 - Revision: 4.0

Microsoft Access 2000 Standard Edition

  • kbnosurvey kbarchive kbhowto kbprogramming KB200593