Show All

Create a message box programmatically

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 or by adding the MsgBox function to a Microsoft Visual Basic procedure.

Use a macro

  1. Create a macro.

    How?

    1. In the Database window, click Macros under Objects.
    2. Click the New button on the Database window toolbar.
  2. In a blank action row, click MsgBox in the action list, and then set the action arguments.
  3. Click Save to save the macro.
  4. To test the macro, click Run on the toolbar.

Use Visual Basic code

  1. Create a Visual Basic procedure.

    How?

    1. In the Database window, click Modules under Objects, and then click New on the Database window toolbar.

    2. Declare the procedure by typing the Sub statement.
    3. Type a procedure name, immediately followed by any arguments in parentheses. For example, the following declaration for the ShowEvent Sub procedure specifies EventName as an argument:
      Sub ShowEvent(EventName As String)
  2. Add an assignment statement to the procedure that assigns the value returned by the MsgBox function to a variable. 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)
  3. 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.