Home

Visual Basic Built-In Functions: The Message Box

  

Introduction

A message box is a special dialog box used to display a piece of information to the user. The user cannot type anything in the message box. There are usually two kinds of message boxes you will create: one that simply displays information and one that expects the user to make a decision.

A message box is created using the MsgBox function. Its syntax is:

Public Function MsgBox( _
   ByVal Prompt As Object, _
   Optional ByVal Buttons As MsgBoxStyle = MsgBoxStyle.OKOnly, _
   Optional ByVal Title As Object = Nothing _
) As MsgBoxResult

The MsgBox() function takes three arguments and only the first one is required.

The Message of a Message Box

The Prompt argument is the string that the user will see displaying on the message box. As a string, you can display it in double quotes, like this "Your credentials have been checked.". Here is an example:

Public Module Exercise

    Public Function Main() As Integer
        MsgBox("Your credentials have been checked.")
        Return 0
    End Function

End Module

This would produce:

Message Box

You can also create the message from other pieces of strings. The Prompt argument can be made of up to 1024 characters. To display the Prompt on multiple lines, you can use either the constant vbCrLf or the combination Chr(10) & Chr(13) between any two strings. Here is an example:

Public Function Main() As Integer
    MsgBox("Your logon credentials have been checked." & _
           vbCrLf & "To complete your application, please " & _
           "fill out the following survey")
    Return 0
End Function

This would produce:

Message Box

The Buttons of a Message Box

The Buttons argument specifies what button(s) should display on the message box. There are different kinds of buttons available and the Visual Basic language. The Buttons options are primary members of an enumerations named MsgBoxStyle. The members of the MsgBoxStyle enumeration that produce buttons are:

Button Value Alternative Numeric Value Display
MsgBoxStyle.OKOnly vbOKOnly 0 OK
MsgBoxStyle.OKCancel vbOKCancel 1 OK Cancel
MsgBoxStyle.AbortRetryIgnore vbAbortRetryIgnore 2 Abort Retry Message Box Button: Ignore
MsgBoxStyle.YesNoCancel vbYesNoCancel 3 Yes Message Box Button: No Cancel
MsgBoxStyle.YesNo vbYesNo 4 Yes Message Box Button: No
MsgBoxStyle.RetryCancel vbRetryCancel 5 Retry Cancel

When calling the MsgBox() function and specifying the button, you can use either a member of the MsgBoxStyle enumeration or one of the above constant numeric values. Here is an example that displays the Yes and the No buttons on the message box:

Public Function Main() As Integer
    MsgBox("Your logon credentials have been checked " & _
           "and your application has been approved: Congratulations!" & _
           vbCrLf & "Before leaving, would you like " & _
           "to take our survey survey now?", MsgBoxStyle.YesNo)
    Return 0
End Function

This would produce:

The Buttons of a Message Box

The Icon on a Message Box

Besides the buttons, to enhance your message box, you can display an icon in the left section of the message box. To display an icon, you can use or add a member of the MsgBoxStyle enumeration. The members that are meant to display an icon are:

Icon Constant Alternative Numeric Value Description
MsgBoxStyle.Critical vbCritical 16 Icon Stop
MsgBoxStyle.Question vbQuestion 32 Question Question
MsgBoxStyle.Exclamation vbExclamation 48 Exclamation Exclamation
MsgBoxStyle.Information  vbInformation  64 Information Information

To use one of these icons, you must combine the value of the button to the desired value of the icon. To perform this combination, you use the OR operator. Here is an example:

Private Sub cmdMessageBox_Click()
    Dim iAnswer As Integer
    
    iAnswer = MsgBox("Your logon credentials have been checked " & _
                    "and your application has been approved: Congratulations!" & _
                     vbCrLf & "Before leaving, would you like " & _
                     "to take our survey survey now?", vbYesNo Or vbQuestion)
End Sub

This would produce:

The Icon of a Message Box

When calling the MsgBox() function, if you want to show one or more buttons and to show an icon, you can use either two members of the MsgBoxStyle enumeration using the OR operator, or you can add one of the constant values of the buttons to another contant values for an icon. For example,  3 + 48 = 51 would result in displaying the buttons Yes, Ne, and Cancel, and the exclamation icon.

 
 
 

The Default Button of a Message Box

If you create a message box with more than one button, the most left button usually has a thick border, indicating that it is the default. If the user presses Enter after viewing the button, the effect would be the same as if he had clicked the default button. If you want, you can designate another button as the default. To do this, you can use or add another member of the MsgBoxStyle enumeration. The members used to specify the default button are:

Default Button Constant Alternative Numeric Value If the message box contains more than one button, the default would be
MsgBoxStyle.DefaultButton1  vbDefaultButton1  0 The first button
MsgBoxStyle.DefaultButton2  vbDefaultButton2  256 The second button
MsgBoxStyle.DefaultButton3  vbDefaultButton3  512 The third button

Once again, to specify a default value, use the OR operator to combine a Default Button Constant with any other combination. Here is an example:

Public Function Main() As Integer
    MsgBox("Your logon credentials have been checked " & _
           "and your application has been approved: Congratulations!" & _
           vbCrLf & "Before leaving, would you like " & _
           "to take our survey survey now?", _
           MsgBoxStyle.YesNo Or _
    	       MsgBoxStyle.Question Or MsgBoxStyle.DefaultButton2)
    Return 0
End Function

This would produce:

The Default Button of a Message Box

These additional buttons can be used to further control what the user can do:

Constant  Value Effect
MsgBoxStyle.ApplicationModal 0 The user must dismiss the message box before proceeding with the current database
MsgBoxStyle.SystemModal 4096 The user must dismiss this message before using any other open application of the computer
 

The Title of a Message Box

The Title argument is the caption that would display on the title bar of the message box. It is a string whose word or words you can enclose between parentheses or that you can get from a created string. The Title argument is optional. As you have seen so far, if you omit, the message box would display the name of the application on the title bar. Otherwise, if you want a custom title, you can provide it as the third argument to the MsgBox() function. The caption can be a simple string. Here is an example:

Public Function Main() As Integer
    MsgBox("Your logon credentials have been checked " & _
           "and your application has been approved: Congratulations!" & _
           vbCrLf & "Before leaving, would you like " & _
           "to take our survey survey now?", _
           MsgBoxStyle.YesNo Or MsgBoxStyle.Question, _
           "Crofton Circle of Friends - Membership Application")
    Return 0
End Function

This would produce:

The Title of a Message Box

Notice that the caption is now customized instead of the name of the application. The caption can also be a string created from an expression or emanating from a variable or value.

The Returned Value of a Message Box

The MsgBox() function can be used to return a value. This value corresponds to the button the user clicked on the message box. Depending on the buttons the message box is displaying, after the user has clicked, the MsgBox() function can return a value. The value can be a member of the MsgBoxResult enumeration or a constant numeric value recognized by the Visual Basic language. The value returned can be one of the following values:

If the user click The function returns or Numeric Value
OK MsgBoxResult.OK vbOK 1
Cancel MsgBoxResult.Cancel vbCancel 2
Abort MsgBoxResult.Abort vbAbort 3
Retry MsgBoxResult.Retry vbRetry 4
Ignore MsgBoxResult.Ignore vbIgnore 5
Yes MsgBoxResult.Yes vbYes 6
No MsgBoxResult.No vbNo 7
 
 
   
 

Home Copyright © 2008-2016, FunctionX, Inc.