Home

Message Boxes

  

Introduction

A message box is a special dialog box used to display a piece of information to the user. As opposed to a regular form, the user cannot type anything on 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:

MsgBox([Message] [Buttons] [Title] [HelpFile] [Context])

The MsgBox function takes five arguments and only the first one is required: the Message.

The Message of a Message Box

The Message 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 "That's All Folks". Here is an example:

Private Sub cmdMessageBox_Click()
    MsgBox "Your credentials have been checked."
End Sub

This would produce:

Message Box

You can also create it from other pieces of strings. The Message argument can be made of up to 1024 characters. To display the Message 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:

Private Sub cmdMessageBox_Click()
    MsgBox "Your logon credentials have been checked." & _
            vbCrLf & "To complete your application, please " & _
            "fill out the following survey"
End Sub

This would produce:

Message Box

The Buttons on a Message Box

The Buttons argument specifies what button(s) should display on the message box. There are different kinds of buttons available and Visual Basic recognizes them by a numeric value assigned to each. The Buttons argument is a value of the VbMsgBoxStyle enumeration. It can be one of the following constants:

VbMsgBoxStyle Member Constant Value Button(s) Displayed
vbOKOnly 0 OK
vbOKCancel 1 OK Cancel
vbAbortRetryIgnore 2 Abort Retry Message Box Button: Ignore
vbYesNoCancel 3 Yes Message Box Button: No Cancel
vbYesNo 4 Yes Message Box Button: No
vbRetryCancel 5 Retry Cancel

Here is an example that displays the Yes and the No buttons on the message box:

Private Sub cmdMessageBox_Click()
    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?", _
           VbMsgBoxStyle.vbYesNo
End Sub

This would produce:

Message Box

You can use the name of the member of the VbMsgBoxStyle enumeration directly, that is, without qualifying it. Here is an example:

Private Sub cmdMessageBox_Click()
    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
End Sub

Or you can use the constant value of the member of the enumeration if you know it. Here is an example:

Private Sub cmdMessageBox_Click()
    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?", 4
End Sub

These three formats would produce the same result.

The Icons 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 a member of the VbMsgBoxStyle. The available members for the icons are:

VbMsgBoxStyle Member Integer Value Description
vbCritical 16
vbQuestion 32 Question
vbExclamation 48 Exclamation
vbInformation  64 Information

To use one of these icons, you have two options. You can combine its VbMsgBoxStyle button with the VbMsgBoxStyle icon member. To perform this combination, you use the Or operator. Here is an example:

Private Sub cmdMessageBox_Click()
    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?", _
           VbMsgBoxStyle.vbYesNo Or VbMsgBoxStyle.vbQuestion
End Sub

This would produce:

Message Box

Once again, you can use the name of the member of the VbMsgBoxStyle enumeration without qualifying it. Here is an example:

Private Sub cmdMessageBox_Click()
    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

The second alternative it to use the integral constants instead of the members of the  enumeration. Here is an example:

Private Sub cmdMessageBox_Click()
    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?", _
           4 Or 32
End Sub

Alternatively, you can add (using the arithmetic addition) the integral value of the button to the integral value of the icon. For example, the integral value of the Yes/No button is 4 and the integral value of the question icon is 32. If you add both, you get 4 + 32 = 36. Therefore, if you use 36 for the second argument, you would get the question icon, the Yes, and the No button. This would be done as follows:

Private Sub cmdMessageBox_Click()
    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?", 36
End Sub

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 one more member of the a member of the VbMsgBoxStyle enumeration. The available members are:

VbMsgBoxStyle Member Integral Constant If the message box contains more than one button, the default would be
vbDefaultButton1  0 The first button
vbDefaultButton2  256 The second button
vbDefaultButton3  512 The third button
vbDefaultButton4  768 The fourth button

Once again, to specify a default value, use the Or operator to combine a VbMsgBoxStyle Member with any other combination. Here is an example:

Private Sub cmdMessageBox_Click()
    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?", _
           VbMsgBoxStyle.vbYesNoCancel Or VbMsgBoxStyle.vbQuestion _
           Or VbMsgBoxStyle.vbDefaultButton2
End Sub

This would produce:

Message Box

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

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

Once again, you can use the name of the member of the VbMsgBoxStyle enumeration directly without qualifying it. Here is an example:

Private Sub cmdMessageBox_Click()
    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?", _
           vbYesNoCancel Or vbQuestion Or vbDefaultButton2
End Sub

Also, remember that you can use the constant integer of a member. Here is an example:

Private Sub cmdMessageBox_Click()
    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?", _
           3 Or 32 Or 256
End Sub

You can also arithmetically add the constant values of the desired members.

The Title or Caption 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 is equipped with the "Microsoft Office Access" string as its default value. 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:

Private Sub cmdMessageBox_Click()
    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?", _
           vbYesNoCancel Or vbQuestion Or vbDefaultButton2, _
           "Crofton Circle of Friends - Membership Application"
End Sub

This would produce:

Message Box

Notice that the caption is now customized instead of the routine "Microsoft Office Access". The caption can also be a string created from an expression or emanating from a variable or value.

Help for a Message Box

If your application is using a help file, you can specify this and let the message box use it. The HelpFile argument is a string that specifies the name of the help file, and the Context argument provides the number that corresponds to the appropriate help topic for the message box.

The Return Value of a Message Box

The MsgBox() function can be used to return a value. This value corresponds to the button the user clicks on the message box. Depending on the buttons the message box is displaying, after the user has clicked, the MsgBox() function can return one of the following values:

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

Home Copyright © 2009 FunctionX