MS Access: Enhanced Message Box Replacement
This project provides a custom and enhanced message box replacement for the default MsgBoxfound in Access. A Test database for Access 2007 is available at the bottom of this post. (Updated 12AUG2008.)
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.

It has, however, a few drawbacks:
- It is bland; the standard message box does not follow the currently selected Office 2007 scheme.
- The amount of text it can display is limited. If you try to display too much the text will be truncated.
- You can’t copy or save the content of the message being displayed.
- Because message boxes are viewed as intrusive, people tend not to read them and end-up closing them before they realize it may have contained useful information.
- It only displays plain text. You cannot format the message to draw attention to the key points.
Sometimes you need to display an important message or require users to take an action. 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:

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

Here are the features of the enhanced message box:
- It is entirely compatible with the standard one: just change
MsgBoxtoBoxusing find and replace should be enough. - It allows the user to simply click on a button to copy the content of the message to the clipboard or to save it to a text file to a configurable default location.
- It looks and feel like it belongs to the main application, following its colour scheme.
- It attempts to prevent users from blindly closing the modal box without them making an effort to read 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
RichBoxversion 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 closing like the standard
MsgBoxdoes. 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.
How to use it
Download the demo below and copy (drag & drop) the following into your application:
- the
FormDialogform, - the
API_GetTextMetricsmodule, - the
Dialogmodule.
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.
There are a few additional settings that can be used to change the behaviour of the enhanced message boxes.
One is that you can adjust the delay before the buttons become activated.
Another one is that you can enable or disable whether beeps should be played or not.
The last settings is the folder where we should save the content of the message when the user clicks the Save button on the message box.
These few settings make the enhanced message box more customizable.
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% or 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:
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.
Sample database
You can download a sample database containing all the necessary code as well as a number of tests.
This version only contains the database in Microsoft Access 2007 accdb format.

Download the EnhancedMsgBox01.zip (107KB), v1.1 containing the ACCDB database.
Code Updates
v1.1: 08AUG2008
- Corrected code for
DefaultButtonDelay(thanks to Geoffrey) (was referencing wrong variable, causing self-referencing code). - Corrected code for
BoxandRichBoxto take theDefaultSavedTextFileFolderinto 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 byDefaultSavedTextFileFolder) - Added license notice at top of source code.
v1.0: 20MAY2008
- Original version
Resources
- Dissecting the MessageBox article on CodeProject
- TextWidth-Height code demo from Stephen Lebans (great resource, check it out!).
- Pixel to Twips conversion from MSDN.
- Copy Text to Clipboard from the excellent site The Access Web.
- Getting Resource Strings and more from DLLs.

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.
27 comments May 20th, 2008



