Creating Forms and Controls with VBA

Access has built-in functions for creating new form and control objects programmatically. These are the same functions that the wizards use to create new objects and work the same regardless of whether you are using an MDB or ADP file.

The CreateForm method creates a form and returns a Form object. CreateForm([database[, formtemplate]])

The CreateForm method has the following arguments.

Argument

Description

database

A string expression identifying the name of the database that contains the form template you want to use to create a form. If you want the current database, omit this argument.

formtemplate

A string expression identifying the name of the form you want to use as a template to create a new form. If you omit this argument, Microsoft Access bases the new form on the template specified by the Forms/Reports tab of the Options dialog box, available by clicking Options on the Tools menu.

303

Add code similar to the Click event of a command button:

Private Sub cmdCreateNewForm_Click() Dim frmNewForm As Access.Form Set frmNewForm = CreateForm()

'Form is created in a minimized state. DoCmd.Restore

1 Set caption, resize and reposition. frmNewForm.Caption = "My New Form" DoCmd.MoveSize 500, 500, 8000, 4000

' Save new form

DoCmd.Save acForm, frmNewForm.Name End Sub

The following is the CreateControl function:

CreateControl(formname,

controltype[,

section[,

. parent[,

, columnname[,

left[,

top[, width[, height]]]]]

]] )

The CreateControl function to create controls has a few more arguments.

Argument

Description

Formname

A string expression identifying the name of the open form or report on which you want to create the control.

controltype

One of the following intrinsic constants identifying the type of control you want to create.

Constant

Control type

acBoundObjectFrame

Bound object frame

acCheckBox

Check box

acComboBox

Combo box

acCommandButton

Command button

acCustomControl

ActiveX control

acImage

Image

acLabel

Label

AcLine

Line

acListBox

List box

acObjectFrame

Unbound object frame

Continues

Argument

Description

acOptionButton

Option button

acOptionGroup

Option group

AcPage

Page

acPageBreak

Page break

acRectangle

Rectangle

acSubform

Subform

AcTabCtl

Tab control

acTextBox

Text box

acToggleButton

Toggle button

Section

One of the following intrinsic constants identifying the section that will contain the new control

Constant

Section

AcDetail

(Default) Detail section

AcHeader

Form or report header

AcFooter

Form or report footer

acPageHeader

Page header

acPageFooter

Page footer

acGroupLeveliHeader

Group-level 1 header (reports only)

acGroupLeveliFooter

Group-level 1 footer (reports only)

acGroupLevel2Header

Group-level 2 header (reports only)

acGroupLevel2Footer

Group-level 2 footer (reports only)

Parent

A string expression identifying the name of the parent control of an attached control. For controls that have no parent control, use a zero-length string for this argument, or omit it.

columnname

The name of the field to which the control will be bound, if it is to be a data-bound control

If you are creating a control that won't be bound to a field, use a zero-length string for this argument.

left, top

Numeric expressions indicating the coordinates for the upper-left corner of the control in twips.

width, height

Numeric expressions indicating the width and height of the control in twips.

The following code can add a control to the above form:

Private Sub cmdCreateControl_Click() Dim frmNewForm As Access.Form Dim ctlNewControl As Access.Control

1 Forml is the default name of the form create earlier Set frmNewForm = Application.Forms("form1")

1 Create the new control

Set ctlNewControl = CreateControl(frmNewForm.Name, acTextBox, acDetail, , , frmNewForm.WindowLeft + 250, _ frmNewForm.WindowTop + 250, 1400, 500)

1 Name it & save the form ctlNewControl.Name = "txtNewTextbox" DoCmd.Save acForm, frmNewForm.Name End Sub

0 0

Responses

  • Ester
    How to create a command click event in access 2003?
    7 years ago
  • Robert
    How to option group with vba in access 2003?
    7 years ago

Post a comment