|
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:
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:
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 |
|
vbOKCancel |
1 |
|
vbAbortRetryIgnore |
2 |
|
vbYesNoCancel |
3 |
|
vbYesNo |
4 |
|
vbRetryCancel |
5 |
|
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:
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 |
|
|
vbExclamation |
48 |
|
|
vbInformation |
64 |
|
|
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:
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:
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.