MessageBox macro action

Applies to: Access 2013, Office 2013

You can use the MessageBox action to display a message box containing a warning or an informational message. For example, you can use the MessageBox action with validation macros. When a control or record fails a validation condition in the macro, a message box can display an error message and provide instructions about the kind of data that should be entered.

Setting

The MessageBox action has the following arguments.

Action argument

Description

Message

The text in the message box. Enter the message text in the Message box in the Action Arguments section of the Macro Builder pane. You can type up to 255 characters or enter an expression (preceded by an equal sign).

Beep

Specifies whether your computer's speaker sounds a beep tone when the message displays. Click Yes (sound the beep tone) or No (don't sound the beep tone). The default is Yes.

Type

The type of message box. Each type has a different icon. Click None, Critical, Warning?, Warning!, or Information. The default is None.

Title

The text displayed in the message box title bar. For example, you can have the title bar display "Customer ID Validation". If you leave this argument blank, "Microsoft Access" is displayed.

Remarks

You can use the MessageBox action to create a formatted error message similar to built-in error messages displayed by Microsoft Access. The MessageBox action permits you to supply a message in three sections for the Message argument. You separate the sections with the "@" character.

The following example displays a formatted message box with a sectioned message. The first section of text in the message is displayed as a bold heading. The second section is displayed as plain text beneath that heading. The third section is displayed as plain text beneath the second section, with a blank line between them.

Type the following string in the Message argument:

Wrong button!@This button doesn't work.@Try another.

You can't run the MessageBox action in a Visual Basic for Applications (VBA) module. Use the MsgBox function instead.

Examples

Synchronize forms by using a macro

The following macro opens a Product List form in the lower-right corner of the Suppliers form, displaying the current supplier's products. It shows the use of the Echo, MessageBox, GoToControl, StopMacro, OpenForm, and MoveAndSizeWindow actions. It also shows the use of a conditional expression with the MessageBox, GoToControl, and StopMacro actions. This macro should be attached to the Review Products button on the Suppliers form.

Condition

Action

Arguments: Setting

Comment

Echo

Echo On: No

Stop screen updating while the macro is running.

IsNull([SupplierID])

MessageBox

Message: Move to the supplier record whose products you want to see, then click the Review Products button again. Beep: YesType: NoneTitle: Select a Supplier

If there is no current supplier on the Suppliers form, display a message.

...

GoToControl

Control Name: CompanyName

Move focus to the CompanyName control.

...

StopMacro

Stop the macro.

OpenForm

Form Name: Product List View: DatasheetFilter Name: Where Condition: [SupplierID] = [Forms]![Suppliers]![SupplierID] Data Mode: Read OnlyWindow Mode: Normal

Open the Product List form and show the current supplier's products.

MoveAndSizeWindow

Right: 0.7799" Down: 1.8"

Position the Product List form in the lower right of the Suppliers form.

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 isn't 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 PostalCode 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, the postal code can't be validated.

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

MessageBox

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

If the postal code isn't 5 characters, display a message.

...

CancelEvent

Cancel the event.

GoToControl

Control Name: PostalCode

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

MessageBox

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

If the postal code isn't 4 characters, display a message.

...

CancelEvent

Cancel the event.

GoToControl

Control Name: PostalCode

([CountryRegion] = "Canada") And ([PostalCode] 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 isn't correct for Canada, display a message. (Example of Canadian code: H1J 1C3)

...

CancelEvent

Cancel the event.