Controls: Message Boxes

There are multiple options available when considering message boxes, including the MsgBox function and the MessageBox class.  The simplest message box is demonstrated in the foillowing line:

MsgBox ("You're in big trouble now, mister!")

MsgBox Function

  • You can display a message box that includes a custom dialog intended to provide the user with information.
  • The MsgBox dialog can be customized to display a text message, a predefined icon, and predefined buttons. 

The figure below includes an example of a message box.  Note the different features that can be customized by the programmer.

  • A message box can be displayed by using the MsgBox statement or the MsgBox( ) function.
  • The MsgBox statement only displays a message box, while the MsgBox ( ) function displays a message box and returns a value.
  • The simplified syntax for the MsgBox statement is
    •       MsgBox strMessage [, TypeOfBox][, strTitle]
  • The simplified syntax for the MsgBox ( ) function is
    •       intReturn = MsgBox (strMessage [, TypeOfBox][, strTitle])


The TypeOfBox is determined by combining different groups of built-in integer constants.

TypeOfBox = ButtonType + IconType + ModalType + DefaultButton


The button type allows the programmer to specify the number and type of buttons that appear on the message box.   

The icon type allows the programmer to choose between four different types of icons to display in the message box.   

The modal type determines whether the user can can interact with other windows while the message box is displayed.  For application modal, the user cannot interact with other windows in the same application, but can still interact with other programs.  For system modal, the message box must be dismissed before any other action can take place in any application.


The default button is not shown in our text, but it allows the programmer to specify which button on the message box is the default.  


When the MsgBox function is used, it returns a value.  The table below lists the return values.     

Here is an example of the MsgBox statement: 

typeOfBox = vbOKOnly + vbInformation + vbApplicationModal
MsgBox "Enrollment recorded.", typeOfBox, "Status"

Here is an example of a MsgBox function call:

Dim msg As String 
Dim title As String 
Dim style As MsgBoxStyle 
Dim response As MsgBoxResult 
msg = "
OK to exit, Cancel to continue." ' Define message.
style = MsgBoxStyle.
ApplicationModal Or _
Exclamation Or _

title = "
Exit Application" ' Define title.
' Display message.
response = MsgBox(msg, style, title)

If response = MsgBoxResult.Yes then End

Message Box Function

MessageBox Class