MsgBox Macro Action

Some of the content in this topic may not be applicable to some languages.

You can use the MsgBox action to display a message box containing a warning or an informational message. For example, you can use the MsgBox 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 MsgBox 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 MsgBox action to create a formatted error message similar to built-in error messages displayed by Microsoft Office Access 2007. The MsgBox 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 MsgBox 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, MsgBox, GoToControl, StopMacro, OpenForm, and MoveSize actions. It also shows the use of a conditional expression with the MsgBox, GoToControl, and StopMacro actions. This macro should be attached to the Review Products button on the Suppliers form.

Condition

A ction

Arguments: Setting

Comment

Echo

Echo On: No

Stop screen updating while the macro is running.

IsNull([SupplierID])

MsgBox

Message: Move to the supplier record whose products you want to see, then click the Review Products button again.

Beep: Yes

Type: None

Title: 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: Datasheet

Filter Name:

Where Condition: [SupplierID] = [Forms]![Suppliers]![SupplierID]

Data Mode: Read Only

Window Mode: Normal

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

MoveSize

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

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

[CountryRegion] In ("Australia","Singapore") And Len([PostalCode]) <> 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

([CountryRegion] = "Canada") And ([PostalCode] 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.

Applies To: Access 2007, Access 2010 Developer, Access 2013 Developer, Access 2007 Developer, Access 2013, Access 2010, Access 2016 Preview



Was this information helpful?

Yes No

How can we improve it?

255 characters remaining

To protect your privacy, please do not include contact information in your feedback. Review our privacy policy.

Thank you for your feedback!

Support resources

Change language