ACC97: Setting Default Control Properties in Visual Basic

Last reviewed: August 29, 1997
Article ID: Q167065
The information in this article applies to:
  • Microsoft Access 97

SUMMARY

Advanced: Requires expert coding, interoperability, and multiuser skills.

This article demonstrates how to use Visual Basic for Applications to set properties for default controls on forms and reports.

This article assumes that you are familiar with Visual Basic for Applications and with creating Microsoft Access applications using the programming tools provided with Microsoft Access. For more information about Visual Basic for Applications, please refer to your version of the "Building Applications with Microsoft Access" manual.

MORE INFORMATION

In order to set properties for default controls, you must use the form's DefaultControl 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 only apply to default controls and cannot be set for a control that has already been placed on the form or report:

  • AddColon
  • AutoLabel
  • LabelAlign
  • LabelX
  • LabelY
  • TextAlign

To programmatically set the properties for textbox controls, follow these steps:

  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.Restore
          End Sub
    
    

  4. To test this procedure, type the following line in the Debug window, and then press ENTER:

          SetDefaultProperties
    

    Note that a new, blank form opens in Design view.

  5. Add a new textbox to the form.

    Note the label for the new textbox does not include a colon, has right aligned text, and appears 1/4 inch above and is left aligned with the textbox. Also, note that the text in the new textbox is now centered by default.

REFERENCES

For more information about using Visual Basic for Applications to set the properties for default controls, search the Help Index for "DefaultControl method".

Keywords          : kbprg PgmHowTo FmrHowTo
Version           : 97
Platform          : WINDOWS
Hardware          : x86
Issue type        : kbhowto


================================================================================


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: August 29, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.