Access VBA MsgBox: Syntax, Buttons, and Examples

VBA & macros · Updated November 17, 2026

In Microsoft Access VBA, use MsgBox to display a message or ask a user a simple question; its optional button settings control the choices and icons, and its return value tells your code which button was clicked. For example, If MsgBox("Continue?", vbYesNo Or vbQuestion, "Confirm") = vbYes Then runs a branch when the user chooses Yes.

What MsgBox does in Access

MsgBox is a built-in VBA function that opens a small dialog box. Access applications commonly use it in form event procedures, such as a button’s On Click event, to explain an action, flag a problem, or ask for confirmation before changing data. It is suitable for a short prompt with a small number of standard responses—not for collecting detailed information or presenting a complex workflow.

A message box pauses the code that called it until the user responds. The dialog is modal by default, so the user must dismiss it before interacting with the calling Access window. Afterward, VBA can continue immediately or use the selected button’s return value to decide what happens next.

For a simple notification, the one-argument form is enough:

Option Compare Database
Option Explicit

Private Sub cmdHelp_Click()
    ' Show a brief instruction from a form button.
    MsgBox "Choose a customer before opening an order."
End Sub

Before running any macro or VBA that changes records, save a copy of the database. VBA actions cannot be undone with Ctrl+Z, and a message box does not itself protect data from later code.

Syntax and the optional arguments

The general syntax is MsgBox(prompt, buttons, title, helpfile, context). The prompt is required; the remaining arguments are optional. The same function can be used as a statement when the response does not matter, or as an expression when the code needs to examine the response.

  • prompt: The text to display. It can be a string variable or an expression that produces text.
  • buttons: A number made from button, icon, default-button, and modality constants. If omitted, the dialog has an OK button.
  • title: Text in the title bar. If omitted, VBA uses the application name.
  • helpfile and context: Optional help-file details for a context-sensitive Help button. Most Access forms do not need these arguments.

Use named constants rather than unexplained numbers. The button argument combines options with the VBA Or operator, which makes the intent easier to read. A title is especially useful when multiple Access databases or forms are open, because it gives the prompt useful context.

Choosing buttons, icons, and a default

Choose buttons that correspond to actions the code can actually handle. Common button groups include vbOKOnly, vbOKCancel, vbYesNo, and vbYesNoCancel. Icon constants include vbInformation, vbQuestion, vbExclamation, and vbCritical. A default-button constant can identify which choice is initially selected: vbDefaultButton1, vbDefaultButton2, or, where the button set has one, vbDefaultButton3.

For a consequential action, consider making the safer response the default. In a Yes/No prompt, vbDefaultButton2 selects No initially. This does not prevent a user from choosing Yes; it simply avoids making the potentially destructive choice the first response to an accidental Enter key press.

Here is a form button example that asks before deleting a record. The code assumes it is in the form’s command button Click event; replace the deletion action with the appropriate operation for the form.

Option Compare Database
Option Explicit

Private Sub cmdDelete_Click()
    Dim answer As VbMsgBoxResult

    ' Make No the default choice for this confirmation.
    answer = MsgBox("Delete this record?", _
                    vbYesNo Or vbQuestion Or vbDefaultButton2, _
                    "Confirm deletion")

    If answer = vbYes Then
        ' Put the record-deletion action here.
        MsgBox "The confirmation was accepted.", vbInformation, "Status"
    End If
End Sub

The final message in this example only reports the choice; it does not delete anything. In a real form, use the correct Access operation and account for validation, related records, and error handling. A confirmation prompt is a user-interface safeguard, not a replacement for database rules or a backup.

Reading the response in VBA

When the result matters, store it in a variable declared as VbMsgBoxResult, or compare the function result directly. The return constants include vbOK, vbCancel, vbYes, vbNo, vbAbort, vbRetry, and vbIgnore. The available responses depend on the buttons displayed. For example, a Yes/No prompt returns Yes or No, not OK or Cancel.

A three-choice prompt needs code for all three outcomes. Handling Cancel explicitly is important: otherwise, later code may accidentally continue as if the user had approved an action.

Option Compare Database
Option Explicit

Private Sub cmdSave_Click()
    Dim answer As VbMsgBoxResult

    ' Ask whether to save, discard, or return to editing.
    answer = MsgBox("Save your changes?", _
                    vbYesNoCancel Or vbQuestion, _
                    "Unsaved changes")

    Select Case answer
        Case vbYes
            ' Save the form's changes.
            Me.Dirty = False
        Case vbNo
            ' Discard changes by undoing the form edits.
            Me.Undo
        Case vbCancel
            ' Leave the form unchanged and let the user decide later.
            Exit Sub
    End Select
End Sub

This example assumes the code is running from a bound form where Me.Dirty and Me.Undo apply. A form’s record-saving behavior depends on its data source and state, so adapt the actions to the particular form. The key pattern is to map each possible result to an intentional action.

Writing clear prompts and multiline messages

A useful prompt says what is happening and what the user’s choices mean. “Continue?” is vague if the next step is irreversible. Prefer wording such as “Remove this saved filter?” and give the dialog a short title such as “Remove filter.” Keep the text concise, avoid technical error details that do not help the user, and do not imply that an action succeeded before the code has confirmed it.

To put text on separate lines, join strings with vbCrLf. For example, with a message stored in a variable, you can put a summary and an instruction on separate lines:

Option Compare Database
Option Explicit

Private Sub cmdNotice_Click()
    Dim promptText As String

    ' Use a line break to separate the summary from the next step.
    promptText = "The report is ready." & vbCrLf & _
                 "Select OK to return to the report screen."
    MsgBox promptText, vbInformation, "Report status"
End Sub

In HTML, the ampersand in the code sample is written as & so the page displays the VBA concatenation operator correctly. In the VBA editor, type the ordinary & character.

MsgBox compared with InputBox and Access forms

MsgBox is for displaying text and offering standard buttons. It does not provide a field where the user can enter a name, date, or other value. VBA’s InputBox can collect a simple text response, but it offers limited validation and presentation. For a date, numeric amount, or value that must meet rules, an Access form with a suitable control usually gives you better control over validation and feedback.

Use a custom Access form instead of a message box when users need to review several fields, choose from a controlled list, see related records, or complete a multi-step decision. A form can provide labels, validation, and clearer navigation. MsgBox remains useful alongside that form for a brief confirmation or status notice; it is not a substitute for a well-designed data-entry screen.

Common problems and practical fixes

  • The code ignores the answer: A call such as MsgBox "Proceed?", vbYesNo displays buttons but does not make the program respond to them. Assign the result or compare it in an If or Select Case statement.
  • The wrong button is returned: Compare against the result constants supported by the buttons you displayed. A Yes/No box should be checked for vbYes or vbNo.
  • Text appears on one line: Join the parts of the prompt with vbCrLf. Check that the concatenation operator and line-continuation underscore are placed correctly.
  • The prompt appears repeatedly: A message box inside a loop or a frequently triggered form event may run more than once. Check the event and loop conditions, and avoid showing a dialog for every row in a bulk operation.
  • Code stops at the dialog: That is normal: MsgBox waits for a response. If the message is appearing at the wrong time, inspect which event calls it and whether the form is triggering that event more often than intended.
  • The macro or VBA does not run: Access may block active content in an untrusted database. Only enable content for a database you trust, using Access’s security prompts or a properly managed trusted location; do not treat a warning as something to bypass indiscriminately.

When debugging, check the exact event procedure and the value being tested. A breakpoint or the VBA editor’s Immediate window can help determine whether the code reached the MsgBox call and what response was returned. Avoid using message boxes as the only diagnostic method in a long-running process, because each one requires a person to dismiss it.

Frequently Asked Questions

How do I show a message in Access VBA?
Call MsgBox with the text to display, such as MsgBox "Record saved.", vbInformation, "Status". Add button and icon constants only when they help the user understand the message.

How do I make an Access VBA MsgBox with Yes and No?
Use vbYesNo in the buttons argument, then test the returned value. For example, compare the result with vbYes before running the action that needs approval.

How do I get the button the user clicked?
Assign the function result to a variable declared as VbMsgBoxResult, then compare it with constants such as vbYes, vbNo, or vbCancel. Handle every choice offered by the dialog.

Can MsgBox display multiple lines?
Yes. Join text with vbCrLf to insert a line break. Keep the message short enough to read comfortably in the dialog.

Can MsgBox collect text from the user?
No. MsgBox displays a prompt and buttons; it does not accept typed input. Use InputBox for a simple text response or, for better validation and a richer interface, create an Access form.

Related

Try it: the Column Letter to Number: aA is 27, XFD is 16,384. Converts both ways.

More in VBA & macros

Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources

Spotted an error in this guide? Let us know.