Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

GoToControl Macro Action

Office 2007

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 Name control and move to the next control.

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

Setting

The GoToControl action has the following argument.

Action argumentDescription
Control NameThe 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 can't use the GoToControl action to move the focus to a control on a hidden form.

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, Close, 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.

ActionArguments: SettingComment
EchoEcho On: NoStop screen updating while the macro is running.
CloseObject Type: Form

Object Name: Product List

Save: No

Close Product List form.
OpenFormForm Name: Products

View: Form

Data Mode: Add

Window Mode: Normal

Open the Products form.
SetValueItem: [Forms]![Products]![SupplierID]

Expression: SupplierID

Set the Supplier ID control to the current supplier on the Suppliers form.
GoToControlControl Name: CategoryIDGo 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, MsgBox, 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.

ConditionActionArguments: SettingComment
IsNull([CountryRegion])StopMacro If CountryRegion is Null, postal code can't be validated.
[CountryRegion] In ("France","Italy","Spain") And Len([Postal Code]) <> 5MsgBoxMessage: The postal code must be 5 characters.

Beep: Yes

Type: Information

Title: Postal Code Error

If the postal code isn't 5 characters, display a message.
...CancelEvent Cancel the event.
GoToControlControl Name: PostalCode
[CountryRegion] In ("Australia","Singapore") And Len([Postal Code]) <> 4MsgBoxMessage: The postal code must be 4 characters.

Beep: Yes

Type: Information

Title: Postal Code Error

If the postal code isn't 4 characters, display a message.
...CancelEvent Cancel the event.
GoToControlControl Name: PostalCode
([CountryRegion] = "Canada") And ([Postal Code] Not Like"[A-Z][0-9][A-Z] [0-9][A-Z][0-9]")MsgBoxMessage: The postal code is not valid. Example of Canadian code: H1J 1C3

Beep: Yes

Type: Information

Title: Postal Code Error

If the postal code isn't correct for Canada, display a message. (Example of Canadian code: H1J 1C3)
...CancelEvent Cancel the event.


Show:
© 2014 Microsoft