How-to: Set 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 sub form controls need not be the same as the name of the form objects.

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

Naming Subforms

When referring to subforms, the subform object (embedded on the parent 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.

It is possible and very common to just name the subform object the same as the subform itself, but a naming convention which makes them distinct can make it easier to refactor things at a later date.

Relative vs Absolute references

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.

It is best to keep all code references as close as possible to the original context so that each form is self-contained - perhaps using a relative subform/parent form reference but not relying or expecting any other forms to be open.

Using the Forms collection with an absolute reference to another form is often a sign that you could simplify things by moving the code into a different form/event instead.

VBA Variables

An alternate syntax, valid 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 © 1999-2024 SS64.com
Some rights reserved