ACC: CreateControl() and CreateReportControl() Functions

Last reviewed: August 29, 1997
Article ID: Q93095
The information in this article applies to:
  • Microsoft Access versions 1.0, 1.1, 2.0, 7.0, 97

SUMMARY

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

This article discusses the Microsoft Access functions CreateControl() and CreateReportControl(). If you intend to write your own Form Wizards or Report Wizards, you can use these functions to create controls on a form or report that is open in Design view.

MORE INFORMATION

You can use the CreateControl() and CreateReportControl() functions to create new controls on a form or report open in Design view. These functions require two arguments: the name of the form or report as a string value and a numeric code that represents the control type.

CreateControl() and CreateReportControl() both return a control object value. Therefore, you define a control variable first, and then you set the control variable equal to the function name.

For example, the following procedure creates a form and then adds a command button to the form:

   Dim MyForm As Form, MyControl As Control
   Set MyForm = CreateForm()
   Set MyControl = CreateControl(MyForm.Name, 104)

NOTE: In Microsoft Access 1.x, use MyForm.FormName instead of MyForm.Name).

When the procedure is finished, you can modify the properties of the new control by using the control variable you defined. For example, you can change the control's Width and Caption properties with these statements:

   MyControl.Width = 2000
   MyControl.Caption = "&Sum All Records"

For controls that are frequently associated with a field in a table or query, you can modify the ControlSource property to bind the control to the field.

By default, some controls are created with their Height and Width properties set to zero to make them invisible. Also by default, controls appear in the upper-left corner of the form. You can adjust the size and position of a control immediately after you create it by changing the control's properties. For example, the following code creates, sizes, and moves a text box by changing the properties:

In Microsoft Access 7.0 and 97:

   Set MyControl = CreateControl(MyForm.FormName, 109)
   With MyControl
      .Width = 1500
      .Height = 200
      .Top = 440
      .Left = 200
   End With

In Microsoft Access 1.x and 2.0:

   Set MyControl = CreateControl(MyForm.FormName, 109)
   MyControl.Width = 1500
   MyControl.Height = 200
   MyControl.Top = 440
   MyControl.Left = 200

In addition to the form name and the code for the type of control, you can also specify the form or report section where you want Microsoft Access to place the control.

REFERENCES

For more information about creating a control in code, search the Help Index for "CreateControl" or "CreateReportControl," or ask the Microsoft Access 97 Office Assistant.

Keywords          : kbprg PgmHowTo SynFnc PgmObj FmrProp
Version           : 1.0 1.1 2.0 7.0 97
Platform          : WINDOWS
Hardware          : x86
Issue type        : kbinfo


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


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.