When you want to display a brief message, such as a warning, you can use a predefined dialog box called a message box. You can display a message box by adding the MsgBox action to a macro (macro: An action or set of actions that you can use to automate tasks.) or by adding the MsgBox function to a Microsoft Visual Basic (Microsoft Visual Basic: A high-level, visual-programming version of Basic. Visual Basic was developed by Microsoft for building Windows-based applications.) procedure (procedure: A sequence of declarations and statements in a module that are executed as a unit. Procedures in a Visual Basic for Applications (VBA) module include both Sub and Function procedures.).
Use a macro
Use Visual Basic code
- Create a Visual Basic procedure.
- In the Database window, click Modules under Objects, and then click New on the Database window toolbar.
- Declare the procedure by typing the Sub statement.
- Type a procedure name, immediately followed by any arguments (argument: A value that provides information to an action, an event, a method, a property, a function, or a procedure.) in parentheses. For example, the following declaration for the ShowEvent Sub procedure (Sub procedure: A Visual Basic for Applications (VBA) procedure that carries out an operation. Unlike a Function procedure, a Sub procedure does not return a value.) specifies EventName as an argument:
Sub ShowEvent(EventName As String)
- Add an assignment statement to the procedure that assigns the value returned by the MsgBox function to a variable (variable: A named storage location capable of containing data that can be modified during program execution. Each variable has a name that uniquely identifies it within its level of scope. A data type can be specified or not.). The function returns different values depending on the button a user clicks in the message box. Your procedure can then perform different operations based on the value returned.
For example, the following assignment statement runs the MsgBox function, displaying a message, and assigns the value returned by the function to the variable RetValue:
RetValue = MsgBox("Continue?", vbOKCancel)
- To test, run the procedure by clicking Run Sub/UserForm on the toolbar, select the procedure in the Macros dialog box, and then click Run.