Access VBA MsgBox: How to Show Messages and Get Answers

VBA & macros · Updated November 12, 2026

In Access VBA, use the MsgBox function to show a message or ask a simple question. For example, MsgBox "Record saved.", vbInformation, "Save complete" displays an information icon and a title; if you show Yes and No buttons, the function also returns which button the user chose.

What MsgBox Does in Access

MsgBox is a built-in Visual Basic for Applications function available in Access. It opens a small dialog containing a prompt and a chosen set of buttons. The dialog pauses the running procedure until the user responds, so the code after the call does not run while the message is waiting on screen.

It is useful for brief, immediate feedback: confirming a completed action, warning that a required value is missing, or asking whether to continue with a potentially destructive step. It is not a replacement for an Access form. A message box is temporary, has limited layout and input options, and disappears when dismissed. Use a form when users need to enter several values, review detailed information, or keep a notice visible.

Access also has a MsgBox action in macros. That is a macro action with similar display options, but it is not the VBA function. The examples here use VBA, typically in an event procedure attached to a form control.

Syntax and the Arguments

The general VBA syntax is MsgBox(prompt, buttons, title, helpfile, context). Only prompt is required; the other arguments are optional. The function returns a value identifying the button clicked. In code, it is usually clearest to supply optional arguments by position or to use named constants for the button settings.

  • prompt is the text displayed in the dialog. It can be a string literal, a variable, or a string expression.
  • buttons controls which buttons appear, the icon, the default button, and some display options. Combine the relevant VBA constants.
  • title supplies the dialog’s title-bar text. If omitted, VBA uses the application name.
  • helpfile and context connect the dialog to a Help file and a context identifier. Most Access applications do not need these arguments.

A comma is still needed to reach a later optional argument. For example, to set a title but use the default buttons, provide an empty buttons argument: MsgBox "Ready", , "Status". This is valid VBA, although supplying an explicit button constant can make the intention easier to read.

To put text on separate lines, join strings with vbCrLf or use vbNewLine. For example, a prompt can show a short heading followed by a detail on the next line. Keep the message concise: a message box is intended for a quick decision, not a long report.

Buttons, Icons, and Default Choices

The buttons argument combines constants from the VbMsgBoxStyle enumeration. Add a button-group constant to an icon constant and, if useful, a default-button constant. Named constants are easier to understand and maintain than unexplained numbers.

  • Button groups include vbOKOnly, vbOKCancel, vbYesNo, vbYesNoCancel, vbRetryCancel, and vbAbortRetryIgnore.
  • Icons include vbInformation, vbExclamation, vbQuestion, and vbCritical. Choose one that matches the message; an icon should clarify the situation, not decorate routine notices.
  • Default-button constants include vbDefaultButton1, vbDefaultButton2, and vbDefaultButton3. The default is the button selected by Enter and often receives initial keyboard focus.

For instance, vbYesNo + vbQuestion + vbDefaultButton2 presents a Yes/No question with No as the default. This is useful when the less destructive choice should be selected if a user presses Enter without reading carefully. The default-button setting does not change what the buttons mean; it changes which one is initially selected.

Use button choices that fit the action. A confirmation that can be safely abandoned may use Yes and No, while a simple completion notice usually needs only OK. Do not show a Cancel button unless the procedure has a meaningful way to cancel or stop the operation.

How to Read the User’s Response

When the user dismisses the dialog, MsgBox returns a value from the VbMsgBoxResult enumeration. Common values are vbOK, vbCancel, vbYes, vbNo, vbRetry, vbIgnore, and vbAbort. Capture that result in a variable and compare it with the matching named constant.

A frequent mistake is to display a Yes/No question but ignore the returned value. The dialog may look interactive, yet the procedure will continue in the same way regardless of the choice unless the code branches on the response. A Select Case or If statement makes the choice explicit.

Another important distinction: MsgBox only displays information and receives a button response. It does not collect typed text. Use the VBA InputBox function for a simple text prompt, or create an Access form for input that needs validation, multiple fields, or a more controlled interface.

Examples in Access VBA

To add a basic message to a form button, open the form in Design View, select the button, and use the Property Sheet’s Event tab. Set its On Click property to [Event Procedure], then choose the build option to open the VBA editor. Put the code inside the button’s click procedure. This example shows a completion message:

Private Sub cmdSave_Click()
    ' Confirm that the save action has completed.
    MsgBox "Record saved.", vbInformation, "Save complete"
End Sub

To ask before deleting, store the response and proceed only after Yes. The example shows the decision point; the deletion code would go where the comment indicates. Save a copy of the database before running a macro or procedure that changes or deletes data: VBA actions cannot be undone with Ctrl+Z.

Private Sub cmdDelete_Click()
    Dim answer As VbMsgBoxResult

    ' Make No the default to reduce accidental deletion.
    answer = MsgBox("Delete this record?", _
                    vbYesNo + vbQuestion + vbDefaultButton2, _
                    "Confirm deletion")

    If answer = vbYes Then
        ' Place the intended deletion operation here.
        MsgBox "Deletion confirmed.", vbInformation, "Confirmed"
    End If
End Sub

A response can also control different branches. For example, a Retry/Cancel question should have code for both outcomes; otherwise Retry may simply repeat a prompt without addressing the problem. Keep the response variable typed as VbMsgBoxResult to show clearly what it stores.

Private Sub cmdCheck_Click()
    Dim answer As VbMsgBoxResult

    ' Explain the available choices in the prompt.
    answer = MsgBox("The check did not finish. Try again?", _
                    vbRetryCancel + vbExclamation, _
                    "Check incomplete")

    Select Case answer
        Case vbRetry
            ' Call the appropriate retry procedure here.
        Case vbCancel
            ' Leave the current operation without retrying.
    End Select
End Sub

These procedures are examples of where to put the logic, not complete record-management routines. The actual save or delete operation should use suitable Access methods and error handling for the database. A message saying that an action succeeded should appear only after the action really succeeds.

Common Problems and Practical Fixes

  • The message does not appear: Check that the code is in the event procedure that actually runs, and that the control’s On Click property points to [Event Procedure]. Also look for an earlier error or exit that stops execution before the call.
  • The wrong button seems to be selected: Check how the style constants are combined and whether a vbDefaultButton constant is included. The first button is the default when no alternate default is specified.
  • Choosing No still continues: The procedure may be ignoring the return value. Assign the function result to a variable, then branch on vbNo or vbYes as appropriate.
  • The prompt includes a blank or unexpected value: Inspect the variables used to build the string. In Access, Nz can provide a substitute for a Null value, for example when a field may be empty. Handle Nulls deliberately rather than assuming every value is text.
  • The text is hard to read: Shorten it, add a line break, or use a form for detailed instructions. A MsgBox cannot be resized or formatted like a custom Access form.

For debugging, place a breakpoint on the MsgBox line and step through the procedure in the VBA editor. This helps distinguish a dialog-style issue from an event that never fired or a condition that bypassed the call.

Dialog Behavior, Safety, and Alternatives

A standard MsgBox is modal: the user must respond before continuing in the calling procedure. The usual application-modal behavior is appropriate for most Access prompts. The vbSystemModal option can place a dialog above other applications and is rarely suitable for ordinary database interfaces because it interrupts work outside Access. Avoid using it merely to make a message more noticeable.

Consider whether a message is necessary at all. Repeated pop-ups slow down routine work and train users to dismiss prompts without reading them. For ordinary success feedback, a status message on a form may be less disruptive. For persistent warnings, multi-step decisions, or detailed explanations, an Access form offers more space and control. For diagnostics that need to survive after a dialog closes, write details to a log table or another appropriate logging destination rather than relying on MsgBox.

Access may also restrict VBA or macros based on the file’s security settings and whether the database is trusted. If a procedure does not run, investigate the application’s security notice and trusted-location policy rather than trying to bypass safeguards. Only enable code from a source you trust. Before running code that edits records, make a backup; a message box itself does not provide transaction protection or an undo facility.

Frequently Asked Questions

How do I show a message box in Access VBA?
Call MsgBox with a prompt, and optionally specify a style and title, such as MsgBox "Done", vbInformation, "Status". In a form, put the call in an event procedure that runs at the right time.

How do I make an Access MsgBox show Yes and No?
Use vbYesNo in the buttons argument, commonly with an icon such as vbQuestion. Save the returned value and test it against vbYes or vbNo so the choice affects what the procedure does.

How can I get the button the user clicked?
Assign the result of MsgBox to a variable declared as VbMsgBoxResult. Compare it with constants such as vbOK, vbCancel, vbYes, or vbNo.

Can MsgBox accept typed input?
No. MsgBox accepts a button response only. Use InputBox for a simple text entry, or an Access form when you need validation or a richer input interface.

Why does MsgBox stop my Access code?
It waits for the user to respond before the calling procedure continues. That pause is normal. If the dialog appears at an inconvenient point, consider moving it, reducing unnecessary prompts, or using a form-based status display instead.

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.