Class: MsgBox in Category General VBA/VB6 : Application Techniques from Total Visual SourceBook

Wrapper class to simplify using Message Boxes in VBA and VB6.

Coding MsgBox statement throughout your application can be a drudge. You have to set all the parameters for your message box and add a bunch of bits together to specify attributes. This class solves that problem by providing a very simple wrapper over the MsgBox command that exposes the message box as a set of properties and actions.

Procedure Name Type Description
(Declarations) Declarations Declarations and private variables for the CMsgBox class.
Buttons Property Get the buttons shown on the message box.

The following values are available:
  • btnOKOnly: Show the OK button only.
  • btnOKCancel: Show OK and Cancel buttons.
  • btnOKAbortRetryIgnore: Show OK, Abort, Retry and Ignore buttons.
  • btnYesNoCancel: Show Yes, No and Cancel buttons.
  • btnYesNo: Show Yes and No buttons.
  • btnRetryCancel: Show Retry and Cancel buttons.
If you specify a value for the HelpFile property, the message box displays a Help button in addition to the buttons you have defined with the Buttons property.
DefaultButton Property Get the current Default button setting for the message box.

The following values are available:
  • dfButton1: First button is default.
  • dfButton2: Second button is default.
  • dfButton3: Third button is default.
  • dfButton4: Fourth button is default.
Foreground Property Determine whether the message box is set to appear in the foreground (top-most window).
HelpContextID Property Get the current help context ID. If you specify a value for this property, you must also specify the path and name of a help file using the HelpFile property.
HelpFile Property Get the help file currently used by the message box. If you specify a value for this property, the message box displays a Help button in addition to the buttons you have defined with the Buttons property.
Icon Property Get the current icon used for the message box.

The following values are available:
  • icoCritcal - Critical stop icon.
  • icoQuestion - Question icon.
  • icoExclamation - Exclamation icon.
  • icoInformation - Information icon.
Message Property Get the message text for the message box. As with the VBA/VB6 MsgBox command, the string passed to the Message property can contain carriage-return/line-feed characters so the text contains blanks lines for readability. Use the VBCRLF constant to get these.
Modal Property Get the current modal mode for the message box.

Use the Modal property to specify the modality of the message box:
  • mbNotModal: Message box is not modal.
  • mbApplicationModal: Message box is modal to the application that called it.
  • mbSystemModal: Message box is modal to all windows. The user must respond to the message box before any other windows can get focus. Note that this option is not applicable in Windows NT and Windows 95 and later. It is included for backwards compatibility with Windows 3.x.
Response Property Get the user's response to the message box. When the user responds to the message box by pressing one of the buttons, this property is set to a value that represents the buttons pressed. The return values are defined by the EnumMBResponse enumerated type, and corresponds exactly to the VBA/VB6 MsgBox return value constants. Because of this, you can use either the class enumerated type, or the VBA/VB6 MsgBox return value constants.
RightAligned Property Get the current RightAligned property for the message box. This property corresponds to the VBA/VB6 vbMsgBoxRight property which appears to have no effect on Windows 95/98 systems. This property works only under Windows NT.
Title Property Get the current title for the message box. The title bar of a message box usually contains your application name, or text summarizing the reason for the message. VBA/VB6 expands the message box window to accommodate the length of your title text, so you should keep the value for this property short. Otherwise, you can end up with a message box window as wide as the screen.
Show Method Show the message box with the properties and settings that you have already specified.
' Example of CMsgBox
' To use this example, create a new module and paste this code into it.
' Then run the procedure by putting the cursor in the procedure and pressing:
'    F5 to run it, or
'    F8 to step through it line-by-line (see the Debug menu for more options)

Private Sub Example_CMsgBox()
  ' Comments: Examples of using the CMsgBox class to simplify using the MsgBox command in VBA and VB6

  Const cstrHelp As String = "TVSB.chm"

  Dim clsMsgBox As CMsgBox
  Dim strRet As String

  ' Instantiate the class
  Set clsMsgBox = New CMsgBox

  ' Set the message box properties
  With clsMsgBox
    .Title = "My Message Box"
    .Icon = icoQuestion
    .Buttons = btnYesNoCancel
    .DefaultButton = dfButton1
    .Modal = mbApplicationModal
    .HelpFile = cstrHelp
    .HelpContextID = 1000
    .Message = "This is a test. Click on a button."
  End With

  ' Show the message box

  ' Determine the response
  Select Case clsMsgBox.Response
    Case mbReturnOK
      strRet = "OK"
    Case mbReturnCancel
      strRet = "Cancel"
    Case mbReturnAbort
      strRet = "Abort"
    Case mbReturnRetry
      strRet = "Retry"
    Case mbReturnIgnore
      strRet = "Ignore"
    Case mbReturnYes
      strRet = "Yes"
    Case mbReturnNo
      strRet = "No"
  End Select

  ' Modify the message box to show the results
  clsMsgBox.Message = "The example returned: " & strRet
  clsMsgBox.Icon = icoInformation
  clsMsgBox.Buttons = btnOKOnly
  ' Without a help file, the help button is not visible
  clsMsgBox.HelpFile = ""

  ' Show the results

  Set clsMsgBox = Nothing

End Sub

Total Visual SourceBook The source code in Total Visual Sourcebook includes modules and classes for Microsoft Access, Visual Basic 6 (VB6), and Visual Basic for Applications (VBA) developers. Easily add this professionally written, tested, and documented royalty-free code into your applications to simplify your application development efforts.

Total Visual SourceBook is written for the needs of a developer using a source code library covering the many challenges you face. Countless developers over the years have told us they learned some or much of their development skills and tricks from our code. You can too!

Additional Resources

Total Visual SourceBook CD and Printed Manual

Microsoft Access/ Office 2016, 2013, 2010, and 2007 Version
is Shipping!

New features in Total Visual SourceBook for Access, Office and VB6

Supports Access/Office 2016, 2013, 2010 and 2007, and Visual Basic 6.0!

View all FMS products for Microsoft Access All Our Microsoft Access Products


Reader Choice Award for MS Access Source Code Library
Reader Choice

"The code is exactly how I would like to write code and the algorithms used are very efficient and well-documented."

Van T. Dinh, Microsoft MVP

SourceBook Info

Additional Info




Free Product Catalog from FMS