Message Boxes
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 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 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:
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.
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:
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
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:
Once again, to specify a default value, use the Or operator to combine a VbMsgBoxStyleMember 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:
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.
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Comments
Post a Comment