Export (0) Print
Expand All
Expand Minimize

MsgBox Macro Action

Office 2007

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 argumentDescription
MessageThe 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).
BeepSpecifies 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.
TypeThe type of message box. Each type has a different icon. Click None, Critical, Warning?, Warning!, or Information. The default is None.
TitleThe 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.

ConditionActionArguments: SettingComment
EchoEcho On: NoStop screen updating while the macro is running.
IsNull([SupplierID])MsgBoxMessage: 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.
... GoToControlControl Name: CompanyNameMove focus to the CompanyName control.
...StopMacro Stop the macro.
OpenFormForm 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.
MoveSizeRight: 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.

ConditionActionArguments: SettingComment
IsNull([CountryRegion])StopMacro If CountryRegion is Null, the postal code can't be validated.
[CountryRegion] In ("France","Italy","Spain") And Len([PostalCode]) <> 5MsgBoxMessage: 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.
GoToControlControl Name: PostalCode
[CountryRegion] In ("Australia","Singapore") And Len([PostalCode]) <> 4MsgBoxMessage: 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.
GoToControlControl Name: PostalCode
([CountryRegion] = "Canada") And ([PostalCode] Not Like"[A-Z][0-9][A-Z] [0-9][A-Z][0-9]")MsgBoxMessage: 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:
© 2014 Microsoft