Thursday, May 10, 2018

GoToControl Macro Action

GoToControl Macro Action

You can use the GoToControl macro action in Access desktop databases and Access web apps 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.

Setting

The GoToControl action has the following argument.

Action argument

Description

Control Name

Type the name of the field or control where you want the focus. 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] if you are using an Access desktop database.

Remarks

You can't use the GoToControl macro action to move the focus to a control on a hidden form.

Tip:  You can use the GoToControl macro 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.

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.

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

If IsNull([CountryRegion])

StopMacro

If CountryRegion is Null, postal code can't be validated.

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

MessageBox

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

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

MessageBox

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

If ([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: 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.

No comments:

Post a Comment