Access pattern · built-in MsgBox

Advanced Message Box

Before you build a whole form just to talk to the user, squeeze more out of Access's built-in MsgBox. Clearer choices, a real title, and Yes / No / Cancel that actually drives what happens next.

Download MsgBox.pdf

Videos that used to sit on this page may return later. The written explanation, the PDF, and the VBA are here now. Gumroad links are gone on purpose.

Why bother with an "advanced" MsgBox?

Message boxes can be modified quite extensively to give your user a much better experience. So before you set about creating a Form to use as a Message Box, consider using the more advanced features of a built-in Message Box.

Colin on Access World Forums has a nicely done example database: An Attention Seeking Database. That demo is a different flavour -- formatted boxes, HTML, countdown, flashing text, dimmed background, warning sounds. Useful if you want to get attention.

This page is the practical uncle-gizmo take: walk through the built-in MsgBox options most people skip, then drop a real-world Yes / No / Cancel pattern into a switchboard-style workflow.

Walk-through

Message boxes have advanced features you may or may not be aware of. One old programming technique that shows up when you combine button constants is bit-wise comparison -- the same idea behind adding vbYesNoCancel + vbQuestion (and friends) so one call carries buttons and an icon.

Useful extras that sit with the walk-through:

Working in the Nifty Switchboard Builder

The same pattern is shown controlling events inside the Nifty Switchboard Builder. That heritage product page is still a 404 at /nifty-switchboard-builder/ -- a natural follow-on restore, not this job. The point here is not pretty MsgBox art. It is the code deciding what happens next when the user picks Yes, No, or Cancel.

The working example on this page is the btnGo_Click listing below. The PDF walk-through is a download from this site, not from Drive. Gumroad is gone.

There is a separate browser switchboard demo at /demo/switchboard/ (menu items as rows). That is not the old Builder product page.

Real-world case: delete or rename a switchboard page

A concrete user case from the Switchboard Builder:

  • You want to delete a menu page, but the user must understand the implications.
  • You must not let them delete the Main Menu / Master Menu Page -- that would cause all sorts of complications.
  • Two common Access scenarios: calling the prompt from a button on a form, and calling it from a pop-up form.

The Advanced Message Box pattern drops in so you can create that prompt quickly and help the user make the correct decision. The thinking lived in the old YouTube demos; the free text example stayed on the web page -- that is the paste below.

Another switchboard case: creating a new Switchboard Page. The page gets a default name. The message box offers the user the option to change that default to something meaningful, leave the default and continue, or escape without making changes.

Code example (btnGo_Click)

Pasted from the Cyotek backup, section "Advanced" Message Box -- Code Example. This is the real-world sample updated on the old site (not identical to the first video demo). It assumes a form with txtNewPageName, a constant conAppName, and a routine fAddNewPage (Switchboard Builder context).

The listing is coloured like Visual Studio on a white sheet. Switch the site to Light (top right) if names look faint -- keywords stay blue either way.

VBA · btnGo_Click
Private Sub btnGo_Click()
If txtNewPageName = "" Or IsNull(Me.txtNewPageName) Then
    Me.txtNewPageName = "Default New Page Name"
End If

    If txtNewPageName = "Default New Page Name" Then

            Dim strMsg1 As String
            Dim strYes As String
            Dim strNo As String
            Dim strCancel As String
            Dim strTitle As String

            strMsg1 = "Do You Want Change Default Page Name, to Something Meaningful?"
            strYes = "  Yes:          Return to the Textbox. I will Create my own Name"
            strNo = "  No:          Just Use the Default Please"
            strCancel = "  Cancel:    It's Bloody Confusing, I'll Just Play it Safe for Now"
            strTitle = conAppName & " --- Change Default Page Name?"

                Select Case MsgBox(strMsg1 & vbCrLf & vbLf & strYes & vbCrLf & strNo & vbCrLf & strCancel & vbCrLf, vbYesNoCancel + vbQuestion, strTitle)
                    Case vbYes:
                        Me.txtNewPageName = ""
                        Me.txtNewPageName.SetFocus
                    Case vbNo:
                        Call fAddNewPage
                        DoCmd.Close acForm, Me.Name
                    Case vbCancel:
                        DoCmd.Close acForm, Me.Name
                    Case Else: 'Default case to trap any errors
                        'Do nothing
                        MsgBox "Error From --- Select Case in btnGo_Click"
                End Select
    Else
        Call fAddNewPage
        DoCmd.Close acForm, Me.Name

    End If
End Sub      'btnGo_Click

What the pattern is doing

Empty name
Seed the box with "Default New Page Name".
Still the default
Show a Yes / No / Cancel message whose body explains each button in plain English (strYes / strNo / strCancel).
Yes
Clear the text box and put focus back so they type a real name.
No
Accept the default, call fAddNewPage, close the form.
Cancel
Close without adding.
Already typed a name
Skip the prompt and add the page.

That is the "advanced" bit people miss: the message text carries the decision labels, and Select Case on the MsgBox return value drives the workflow. Combining vbYesNoCancel + vbQuestion is the bit-wise part -- buttons and icon in one argument.

Related

CallCalled class module -- another Access VBA product page with a listing.

Nifty Switchboard Builder (heritage, 404 for now): /nifty-switchboard-builder/. Browser menu demo: Switchboard.

Old WordPress categories: OTHER, Technics. Tags: Message box, msgbox.