GoToControl Action

Microsoft Access Visual Basic

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 window. 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.

ShowTip

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 Visual Basic, 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

Show 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. Then the GoToControl action 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.
Close Object Type: Form

Object Name: Product List

Save: No

Close Product List form.
OpenForm Form Name: Products

View: Form

Data Mode: Add

Window 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.

Show 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. It 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([Country]) StopMacro If Country is Null, postal code can't be validated.
[Country] In ("France","Italy","Spain") And Len([Postal Code]) <> 5 MsgBox Message: 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.
GoToControl Control Name: PostalCode
[Country] In ("Australia","Singapore") And Len([Postal Code]) <> 4 MsgBox Message: 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.
GoToControl Control Name: PostalCode
([Country] = "Canada") And ([Postal Code] Not Like"[A-Z][0-9][A-Z] [0-9][A-Z][0-9]") MsgBox Message: 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.