Microsoft Access Form References

Absolute references

Controls on a form:

Forms!frmMain!txtControlName
Forms!frmMain!txtControlName.enabled = True

Controls on a Sub form:

Forms!frmMain!Subform1.Form!txtControlName
Forms!frmMain!Subform1.Form!txtControlName.enabled = True

If the name is unique, shortcut naming will also work, so the above can be simplified to:

Forms![frmMain]![Subform1]![txtControlName]

To refer to other control properties replace .enabled with the property name you need.

The default property is always .value (you can include .value if you want to make this absolutely clear.)

Properties of the whole FORM:

Forms!frmMain.RecordSource

e.g. Forms!frmMain.RecordSource = “select * from my_table;”

Properties of a whole Sub form:

Forms!frmMain!Subform1.Form.RecordSource

To refer to other form properties replace .RecordSource with the property name you need.

Relative references

Controls on a form:

Me!txtControlName

Controls on a Sub form:

Me!Subform1.Form!txtControlName
Me!Subform1.Form!txtControlName.enabled = True

Controls on a Parent form:

Me.Parent!txtControlName

In these examples txtControlName is whatever name you have given the control e.g. txtSurname

To refer to other control properties replace .enabled with the property name you need.

e.g. txtControlName.Visible or txtControlName.BackColor

Properties of the whole FORM:

Me.RecordSource

Properties of a Sub form:

Me!Subform1.Form.RecordSource

Refer to properties of a Parent form:

Me.Parent.RecordSource

To refer to other form properties replace .RecordSource with the relevant property name.

Sub-Sub forms

Nesting a subform inside another subform is rare, but for the sake of completion..

For these examples Subform1 is the name of a subform control on frmMain, Subform2 is the name of another subform control placed on Subform1. The name of form controls need not be the same as the name of the forms.

Controls on a nested Sub-Sub form:

Forms!frmMain!Subform1.Form!Subform2.Form!txtControlName
Me!Subform1.Form!Subform2.Form!txtControlName

Properties of a nested Sub-Sub form:

Forms!frmMain!Subform1.Form!Subform2.Form.RecordSource
Me!Subform1.Form!Subform2.Form.RecordSource

The parent of a Parent:

Me.Parent.Parent!txtControlName
Me.Parent.Parent.RecordSource

Using a relative reference on a form ensures that nothing will break if the form is renamed.

If you are writing an expression as part of an SQL statement, then use an absolute reference.

When referring to subforms, the subform object (on the main form) can have a different name to the form itself. For example when frmOrderLines is added to frmOrders as a subform, the new subform object could be called sfrOrderLines.
In the examples above we refer to the subform object name.

VBA Variables

An alternate syntax for VBA only, is written like: Forms("frmMain").ControlName
This syntax allows you to use a variable as part of the reference: Forms(strMyVariable).ControlName

Related

SetFocus - Move focus to a specified field or control
Q209207 - Command-line switches for MS Access
Naming conventions - A copy of the famously detailed Reddick VBA Naming convention.
Microsoft Office online - Referring to an object or its properties in expressions


© Copyright SS64.com 1999-2014
Some rights reserved