GoToControl macro action

Applies to: Access 2013, Office 2013

You can use the GoToControl action to move the focus to the specified field or control in the current record of the open form, form datasheet, table datasheet, or query datasheet. You can use this action when you want a particular field or control to have the focus. This field or control can then be used for comparisons or FindRecord actions. You can also use this action to navigate in a form according to certain conditions. For example, if the user enters No in a Married control on a health insurance form, the focus can automatically skip the Spouse/partner Name control and move to the next control.

Setting

Note

This action is not available for use with data access pages.

The GoToControl action has the following argument.

Action argument

Description

Control Name

The name of the field or control where you want the focus. Enter the field or control name in the Control Name box in the Action Arguments section of the Macro Builder pane. This is a required argument.

NOTE: Enter only the name of the field or control in the Control Name argument, not the fully qualified identifier, such as Forms!Products![Product ID].

Remarks

You cannot use the GoToControl action to move the focus to a control on a hidden form.

Tip

You can use the GoToControl action to move to a subform, which is a type of control. You can then use the GoToRecord action to move to a particular record in the subform. You can also move to a control on a subform by using the GoToControl action to move first to the subform and then to the control on the subform.

To run the GoToControl action in a Visual Basic for Applications (VBA) module, use the GoToControl method of the DoCmd object. You can also use the SetFocus method to move the focus to a control on a form or any of its subforms, or to a field in an open table, query, or form datasheet.

Examples

Set the value of a control by using a macro

The following macro opens the Add Products form from a button on the Suppliers form. It shows the use of the Echo, CloseWindow, OpenForm, SetValue, and GoToControl actions. The SetValue action sets the Supplier ID control on the Products form to the current supplier on the Suppliers form. The GoToControl action then moves the focus to the Category ID field, where you can begin to enter data for the new product. This macro should be attached to the Add Products button on the Suppliers form.

Action

Arguments: Setting

Comment

Echo

Echo On: No

Stop screen updating while the macro is running.

CloseWindow

Object Type: FormObject Name: Product List Save: No

Close Product List form.

OpenForm

Form Name: Products View: FormData Mode: AddWindow Mode: Normal

Open the Products form.

SetValue

Item: [Forms]![Products]![SupplierID] Expression: SupplierID

Set the Supplier ID control to the current supplier on the Suppliers form.

GoToControl

Control Name: CategoryID

Go to the Category ID control.

Validate data by using a macro

The following validation macro checks the postal codes entered in a Suppliers form. It shows the use of the StopMacro, MessageBox, CancelEvent, and GoToControl actions. A conditional expression checks the country/region and postal code entered in a record on the form. If the postal code is not in the right format for the country/region, the macro displays a message box and cancels saving the record. The macro then returns you to the Postal Code control, where you can correct the error. This macro should be attached to the BeforeUpdate property of the Suppliers form.

Condition

Action

Arguments: Setting

Comment

IsNull([CountryRegion])

StopMacro

If CountryRegion is Null, postal code cannot be validated.

[CountryRegion] In ("France","Italy","Spain") And Len([Postal Code]) <> 5

MessageBox

Message: The postal code must be 5 characters. Beep: YesType: InformationTitle: Postal Code Error

If the postal code is not 5 characters, display a message.

...

CancelEvent

Cancel the event.

GoToControl

Control Name: PostalCode

[CountryRegion] In ("Australia","Singapore") And Len([Postal Code]) <> 4

MessageBox

Message: The postal code must be 4 characters. Beep: YesType: InformationTitle: Postal Code Error

If the postal code is not 4 characters, display a message.

...

CancelEvent

Cancel the event.

GoToControl

Control Name: PostalCode

([CountryRegion] = "Canada") And ([Postal Code] Not Like"[A-Z][0-9][A-Z] [0-9][A-Z][0-9]")

MessageBox

Message: The postal code is not valid. Example of Canadian code: H1J 1C3 Beep: YesType: InformationTitle: Postal Code Error

If the postal code is not correct for Canada, display a message. (Example of Canadian code: H1J 1C3)

...

CancelEvent

Cancel the event.