Archive for May, 2008

MS Access: Enhanced Message Box Replacement

Microsoft Access This project provides a custom and enhanced message box replacement for the default MsgBoxfound in Access. A Test database containing all the code for Access 2007/2010/2013 is available at the bottom of this post.
(UPDATED Saturday 21OCT2014 to VERSION 1.10.)

What’s wrong with the default MsgBox

The default message box in Access is sometimes useful to warn, inform or ask confirmation from the user.

Standard MsgBox

It has, however, a few drawbacks:

  • It is bland: the standard message box does not even follow the currently selected Office colour scheme.
  • The amount of text it can display is limited: if you try to display too much text it will be truncated.
  • You can’t copy or save the content of the message.
  • Because popup boxes are viewed as intrusive, people tend not to read them and end-up closing message boxes before they realize they may have contained useful information.
  • They only displays plain text: you cannot format the message to draw attention to the key points.
  • They are blocking, meaning that nothing can happen in the main application while the box is displayed (it can’t even shut down).
  • It will only appear on the monitor that has the main Access application window, even though the message box may have been opened from a form on another monitor.

Sometimes you need to display an important message or require users to make take a decision.
Message boxes are not to be abused but they serve a useful purpose.

An enhanced message box

Rather than using the bland standard message box you can now have something a bit more customized.

Plain Text version of the enhanced custom message box under the Office Blue Colour Scheme:

Plaintex Enhanced Message Box

RichText version of the enhanced custom message box under the Office Black Colour Scheme:

RichText Enhanced Message Box

Here are the features of the enhanced message box:

  • It is entirely compatible with the standard one: just change MsgBox to Box using find and replace should be enough (see tip below to avoid getting strange errors).
  • It allows the user to simply click on a button to copy the content of the message to the clipboard or save it to a text file to a configurable default location.
  • It looks and feels like it belongs to the main application, following its colour scheme.
  • It attempts to prevent users from blindly closing the modal box reading the message: buttons will first be inactive for a configurable amount of time. It’s not a perfect solution, but it is quite effective.
  • There is a RichBox version that can display rich HTML content, not just plain text, so important parts of the message can be formatted in a useful way.
  • It is able to display large amount of data. While it’s not something you usually want, it may be useful for the message box to display more text in some situations (log or tracing information, legal documentation, etc).
  • Rather than sprinkling your code with “& vbCrLf & _” uglies, you can embed newlines in the text itself by using C-style “\n” escape sequences that will automatically be transformed into the appropriate newlines. Makes for clearer code and less typing.
  • Because you get the source, you can easily customise the message box with new icons and colours to better match your overall application’s personality.
  • It is non-blocking: if your application forces users to log-off after a certain amount of inactivity, the enhanced message box will just close rather than prevent Access from shutting down like the standard MsgBox does. Of course, it’s up to you to decide how to handle that gracefully, if at all.
  • It properly displays the expected button captions based on the language of the operating system, so it behaves very much like the default MsgBox (for instance, it will properly display “Cancel” on English systems and “Annuler” on French ones).
  • It also properly plays the system sounds associated with the type of message. You can also enable or disable the sound effect as needed.
  • From of version 1.4, it will display on the correct monitor in a multi-monitor environment.
  • Version 1.7 adds support for Unicode escape sequences within strings to display Unicode characters in the dialog box. This was added following the publication of this article about .Net Strings in VBA.
  • Version 1.10 adds a feature that allows users to dismiss a particular message so it doesn’t appear again.

How to use it

Download the demo database below and copy (drag & drop) the following into your application:

  • the FormDialog form,
  • the Dialog module.

If you rename the FormDialog, make sure you replace any occurrence to it in the code, in particular in the Dialog module.

Since the enhanced message box is just a replacement for the standard one, you just use it like you would use the MsgBox.

    '-----------------------------------------------------------------------------
    ' Simple use of the Plaintext box
    ' Note the use of n that will be converted into a newline
    Dialog.Box "This is a plaintext message.\nClick OK to dismiss",
               vbOKOnly + vbinformation, _
               "Message Title"
    '-----------------------------------------------------------------------------
    ' Getting the result back
    Dim dr As vbMsgBoxresult
    dr = Dialog.Box("Are you sure you want to delete?", _
                    vbYesNoCancel + vbQuestion, "Confirm action")
    If (dr = vbYes) Then DeleteRecords
    '-----------------------------------------------------------------------------
    ' Using named parameters
    Dialog.Box Prompt:="All your bases are belong to us", _
               Buttons:=(vbOkOnly + vbCritical), _
               Title:="Bad error"
    '-----------------------------------------------------------------------------
    ' Using the RichBox to display simple HTML
    ' The first line will be bold, then the word 'button' will be printed in red
    ' Here the \n will be escaped to '<br/>' tags to simulate newlines.
    Dialog.RichBox "<strong>This is a bold message</strong>.\n" & _
                   "Click the <font color=""#FF0000"">button</font> to dismiss.",
                   vbOKOnly + vbInformation, _
                   "RichText Message Title"

Options

There are a few additional settings that can be used to change the behaviour of the enhanced message boxes.

Custom buttons

You can customise the button labels instead of using the default ones (thanks to Kristjan for the suggestion):

Screenshot of dialog box with custom button labels

    '-----------------------------------------------------------------------------
    ' Use custom labels. Buttons that are not labelled will not be displayed
    ' The returned value is either vbBt1, vbBt2 or vbBt3
    Dim dr As vbMsgBoxresultEx
    dr = Dialog.Box (Prompt:="This is a custom button label test.",
                     Buttons:=vbCustom + vbInformation, _
                     Title:="A custom message", _
                     LabelButton1:="Hit Button 1!", _
                     LabelButton2:="No!, Me! Me!", _
                     LabelButton3:="Forget it!")
    If (dr = vbBt1) Then Debug.Print "Button 1 pressed!"
    ElseIf (dr = vbBt2) Then Debug.Print "Button 2 pressed!"
    ElseIf (dr = vbBt3) Then Debug.Print "Button 3 pressed!"
Button delay

One is that you can adjust the delay before the buttons become activated.

    '-----------------------------------------------------------------------------
    ' Use the ButtonDelay to specify the time in seconds before the buttons become activated
    ' The default is 2s. Use 0 to activate the buttons immediately.
    Dialog.Box Prompt:="All your bases are belong to us", _
               Buttons:=(vbOkOnly + vbCritical), _
               Title:="Bad error", _
               ButtonDelay:=1
    '-----------------------------------------------------------------------------
    ' Change the default delay value.
    ' To disable the activation delay
    Dialog.DefaultButtonDelay = 0
    ' To make the user wait 3 seconds before they can press any button
    Dialog.DefaultButtonDelay = 3
Beep

Another one is that you can enable or disable whether beeps should be played or not.

    '-----------------------------------------------------------------------------
    ' Use AllowBeep to specify whether beeps should be played when the message box opens
    ' By default, they are.
    Dialog.Box Prompt:="All your bases are belong to us", _
               Buttons:=(vbOkOnly + vbCritical), _
               Title:="Bad error", _
               AllowBeep:=False
    '-----------------------------------------------------------------------------
    ' Change the default behaviour. This is True by default.
    Dialog.DefaultAllowBeep = False
Hide Buttons

You can also hide the Copy to clipboard and save to File buttons which are normally visible by default.

    '-----------------------------------------------------------------------------
    ' Use AllowCopyToClipboard and AllowSaveToFile to specify whether to display
    ' the copy to clipboard and save to file buttons.
    ' By default, they are visible, but here we hide them.
    Dialog.Box Prompt:="All your bases are belong to us", _
               Buttons:=(vbOkOnly + vbCritical), _
               Title:="Bad error", _
               AllowCopyToClipboard:=False, _
               AllowSaveToFile:=False
    '-----------------------------------------------------------------------------
    ' Change the default behaviour. This is True by default.
    Dialog.DefaultCopyToClipboardAllowed = False
    Dialog.DefaultSaveToFileAllowed = False
Save Folder

It is recommended to set the the folder where we should save the content of the message when the user clicks the Save button on the message box.

    '-----------------------------------------------------------------------------
    ' Change the save folder.
    ' By default, the text messages will be saved in the same directory as the database.
    ' Here we want them to be saved to a temp directory
    Dialog.DefaultSavedTextFileFolder = "C\:temp"

These few settings make the enhanced message box more customizable.

Raw text and paths

By default, the enhanced dialog box will escape certain sequences in the message to convert them to their printable version:

  • Escape sequences like \n and \t are converted to newlines and tabs spaces
  • Unicode sequences are converted to their symbol: \u20ac is converted to the euro symbol .

If you do not want this behaviour (for instance you need to display data that contains lots of \ characters), use the NoStrEsc option:

    '-----------------------------------------------------------------------------
    ' By default, all messages are unescaped.
    ' Here however, we want to disable that so we can display
    Dialog.Box Prompt:="A path c:\my\doc\file.doc", _
               NoStrEsc:=True
    '-----------------------------------------------------------------------------
    ' Change the default behaviour. This is False by default.
    Dialog.DefaultNoStrEsc = True

Alternatively, you can use the helper function dialog.EscBackslash():

    '-----------------------------------------------------------------------------
    ' Use EscBackslash() when you only want some portion of text
    ' to display '\' correctly, like paths.
    ' Here however, we want to disable that so we can display
    Dialog.Box Prompt:="A path " & EscBackslash("c:\my\doc\file.doc")
Don’t display this message again

Based on suggestions (and on a feature I wanted to implement for a while), I added a way to allow the user to choose not to display a particular message again.

Note that this feature will only work for dialog boxes displaying a single vbOKOnly button. It makes some sense since if you ask the user to choose between multiple actions, you can’t really expect their choice to be the same every time the message is displayed.

To make the dialog box dismissable, you only need to provide it with a unique ID for the message, using the DismissID option:

    '-----------------------------------------------------------------------------
    ' Use DismissID to allow the user to never show the message again.
    Dialog.Box Prompt:="An annoying warning message", _
               Buttons:= vbOKOnly + vbExclamation
               DismissID:="1234ABC"

sshot-309

The user can then tick the box and this particular message will never be shown again (unless we reset the setting for it).

To ensure that the user’s choice is remembered even if the Access application is updated, the message’s DismissID is stored in the registry under:
HKCU\Software\VB and VBA Program Settings\<AppFileName>\DialogBox, where <AppFileName> is simply the name of your Access file (without the path).

You can easily re-enable a particular message or all messages from your code:

    '-----------------------------------------------------------------------------
    ' Re-enable the display of a previously dismissed message:
    Dialog.ResetDismissID "1234ABC"
    '-----------------------------------------------------------------------------
    ' Re-enable the display of all messages:
    Dialog.ResetAllDismissID

Large text

The standard MsgBox cannot display much text. On the other hand, there is no real limitation to the amount of text the Box and RichBox can display.
When the amount of information is too much to fit the maximum allowed size for the message box the text will overflow and can be scrolled up/down as necessary.

Limitations of the RichBox

The RichBox version relies on the normal TextBox control’s ability under Access 2007 to display RichText wich is nothing more than lightweight HTML.
Because font size may be varying a lot in the message, it becomes very difficult to accurately predict the size of the box needed to display the whole message.
Short of implementing a complete HTML engine, we have to rely on some assumptions to display HTML.
The risk is that sometimes the content may not properly fit the TextBox control in some circumstances.
If you use the RichBox, thoroughly try displaying your messages and tweak the HTML as necessary to include additional lines or non-breaking spaces to ensure that the result looks good.
If you don’t overuse font size and don’t display in multiple fonts the RichBox should do the right thing most of the time.
Don’t overuse the RichBox to display colourful messages. There is a fine line between being informative and tasteless. Keep colours and formatting where it is useful.
I think that in most cases, the plain text version Box is more than enough.

How it works

The code makes extensive use of Win32 API calls.
Most of the hard work is done in the FomDialog class form. There is too much there to really go into the details but you are welcome to have a look at the commented code.
The code relies also on a utility function from Stephen Lebans used to calculate the size of of text. I have made some minor modification to that code so I would refer you to his original implementation if you are interested in calculating TextBox sizes for forms or reports.

In the code for the FormDialog, I re-implement some of the expected functionalities of the MsgBox: proper arrangement of the buttons, displaying of the appropriate icon, etc.
Once this is done, we calculate the size of the textbox needed to display the whole of the message.
In the case of RichText, we first use Application.PlainText() to convert the HTML into properly formatted plain text. We then calculate the Textbox size using a slightly larger font than needed as a way to ensure that the content of the RichText message will fit the box in most cases.
Once we know the size of the TextBox, we can easily resize the form to properly display the TextBox.
If there is too much text, we resize the form to its maximum permissible (70% of screen width and 90% of screen height) and change some of the visual cues to let the user know the text is overflowing.

One thing of note is the way the form is kept modal.
Rather than using DoCmd.OpenForm and DoCmd.Close I use the form as a class and create an instance manually (see the code in Dialog.Box and Dialog.Richbox). I keep this instance alive until I got the form’s result back.
If you are interested in knowing how the form is made modal, this is the code in FormDialog.ShowModal() what keeps the form open until the user clicks a button:

    Public Function ShowModal() As VbMsgBoxResult
       ...
        ' Here we reset the result for the clicked button such as vbOK, vbYes, etc
        ' This is set in each Button's Click event
        m_Result = -1
        ' Wait for the user to click a button
        Do While (m_Result = -1)
            DoEvents
            Sleep 50
        Loop
        ShowModal = m_Result
    End Function

The Sleep() function is a Win32 API that stops the current process for the given number of milliseconds. This in effects hands back the control to the Operating System for a short time. That way the system is still responsive and does not consume resources when it’s just waiting for user input.

Replacing MsgBox in existing code

As I said above, replacing the standard MsgBox is easy but you need to make sure your search and replace parameters are configured correctly:

Search and replace options

If you’re getting strange compile errors, it may be because you forgot to tick the Find Whole Word Only and some of the strings containing the letter sequence “msgbox” were replaced in the process.

If that’s the case, you can revert the damage by simply doing a search and replace across the whole project on:
VbboxStyle or VbDialog.BoxStyle to be replaced with VbMsgBoxStyle
VbboxResult or VbDialog.BoxResultto be replaced with VbMsgBoxResult

Upgrading from an older version

If you are already using the enhanced DialogBox, upgrading to the newest version is simple.

In your Access application:

  • delete the FormDialog form,
  • delete the Dialog module.
  • delete the API_GetTextMetrics module if you have it (used in versions before 1.5)

Download the new version of the demo database below and open it.

  • drag and drop the `FormDialog to your application
  • drag and drop the Dialog module to your application

That’s all you need to do.

Code and demo database

You can download a database containing all the necessary code as well as a number of tests.
This version contains the database in Microsoft Access accdb format (the code relies on features that don’t exist in pre-2007 versions of Access).

sshot-310

Download Download the EnhancedMsgBoxv1.10.zip (177KB), version 1.10 – 21OCT2014 containing the ACCDB database.

Code Updates

v1.10: 21OCT2014
Corrected minor bugs and added new features:

  • Added dialog.EscPath() to escape paths in your message and display them correctly, as suggested by Mark Singer in comment 115.
  • Added option NoStrEsc to display raw text when you don’t want escape and unicode sequences like ‘\n’ and ‘\u20ac’ to be converted at all in your whole message.
  • Modified the code for FileExists() to avoid the issue raised by Matthias Kläy in comment 116
  • Added option DismissID to allow the user to choose to prevent a message from displaying again (suggested by David Dewick in comment 110).

v1.9: 03FEB2014
Corrected some bugs and added some options:

  • Corrected bugs that would throw exceptions when a message would contain some file path whose ‘\’ would be wrongly interpreted as an escape sequence.
  • Added options to show the buttons for copying the message to the clipboard or saving it to file.

v1.8: 28SEP2013
Resolved some Unicode-related bugs:

  • Corrected bugs that would truncate the strings in the dialog box when they contain some Unicode characters.
  • Corrected bug with copy-to-clipboard that was not copying Unicode text.
  • Corrected bug with copy-to-file that was not saving Unicode text properly.

v1.7: 13SEP2013
Added support for character literals in strings and Unicode escape sequences as supported in .Net strings.
See using .Net strings in VBA for fun an profit for details.

v1.6: 29JUN2013
Corrected issues pointed out by Joseph Strickland (thanks) when running under Office 2010 x64.
Code updated and tested under a Virtual Machine running Win8 x64 and Office 2010 x64.

v1.5: 23JUN2013
Many thanks to contributors Steve Spiller, Jiriki and Kytu for improving and pointing out issues. See details below.

  • Moved the code from the API_GetTextMetrics module into the FormDialog class to reduce the number of necessary files (now only FormDialog and Dialog objects are necessary).
  • Corrected bugs on the test form (button delay and beep options on the form were not wired up correctly in the test form)
  • RichBox was not initialising its buttonDelay correctly, resulting in the first call to use a 0 delay instead of the DefaultButtonDelay value.
  • Corrected bug reported by Jiriki on 06JUN2013 (when the ButtonDelay was set to 0, the dialog would just close the first time the dialog was opened).
  • Focus issues should be solved: the buttons are now properly focused and will behave as the standard dialog box (you can hit ENTER or ESC on the keyboard once the buttons are visible to confirm the default dialog action or cancel it).
  • Addressed parent form focus issue mentioned by KyTu on 19JUN2013: when closing the dialog, the parent form will be properly focused instead of the Navigation Panel.
  • Now supports both x86 and x64 Office systems (32 and 64 bits versions of MSAccess). Many thanks to Steve Spiller for sending me the updated database.

v1.4: 01APR2013
It’s been a while, but at last some improvements and bug fixes!

  • As per Julie B’s comment, updated code to properly display the dialog on the proper monitor in multi-monitor environments.
    The dialog box will open in front of the Access window that currently has focus (assumed to be the one that opened the dialog), so if your application has forms on different monitors, the dialog should open on the right one. If we can’t determine the active window, the dialog box will open in the middle of the monitor containing the main Access application window.
  • Implemented Kristjan’s suggestion regarding the use of custom button labels. See updated description above.
  • Corrected background colours for the dialog box so they correctly match the MS Office theme.
  • Corrected a bug in the code that decided of the correct sound to play.

v1.3: 17MAR2009
Thanks to Henry of Access-Pro.de for proposing a correction to the default buttons behaviour.

  • Updated behaviour for the default buttons. They are now focused in a way that matches that of the standard msgbox.
  • Reversed the naming of the buttons on the form to make it a bit more consistent with the standard box.

v1.2: 07SEP2008
Thanks to Andy Colonna (http://www.pcesoft.com) for uncovering the following bugs (check out his free Spell Checker with source code!):

  • Corrected bug in Form_FormDialog.FilenameSanitize() function that would fail to remove all invalid characters for a file name.
  • File name for the saved text message will be truncated to first 32 characters of message box title in Form_FormDialog.MakeFriendlyFileName().
  • Changed the use of FollowHyperlink to ShellExecute to avoid security warning in some instances in Form_FormDialog.btCopyToFile_Click()
  • Corrected twips to pixel conversion bug in API_GetTextMetrics.fTextWidthOrHeight() that would result in an improperly sized dialog box when the text message was too wide.

v1.1: 08AUG2008

  • Corrected code for DefaultButtonDelay (thanks to Geoffrey) (was referencing wrong variable, causing self-referencing code).
  • Corrected code for Box and RichBox to take the DefaultSavedTextFileFolder into account (the path was previously not passed onto the dialog boxes and the text file would always be created in the application folder instead of the one specified by DefaultSavedTextFileFolder)
  • Added license notice at top of source code.

v1.0: 20MAY2008

  • Original version

Resources

Creative Commons License
This work is licensed under a Creative Commons Attribution 3.0 Unported License.
Free for re-use in any application or tutorial providing clear credit is made about the origin of the code and a link to this site is prominently displayed where end-users can easily access it.

150 comments May 20th, 2008

Previous Posts


Most Recent Posts

Categories

Links

Posts by Month