Export (0) Print
Expand All

GoToControl Action [Access 2003 VBA Language Reference]

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

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

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.

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.


Show:
© 2015 Microsoft