MsgBox Function |
Yes
MsgBox(prompt[, buttons][, title][, helpfile, context])
prompt
Use: Required
Data Type: String
The text of the message to display in the message box dialog.
buttons
Use: Optional
Data Type: Numeric
The sum of the Button, Icon, Default Button, and Modality constant values.
title
Use: Optional
Data Type: String
The title displayed in the titlebar of the message box dialog.
helpfile
Use: Optional
Data Type: String
An expression specifying the help file to provide help functionality for the dialog.
context
Use: Optional
Data Type: Numeric
An expression specifying a context ID within helpfile.
An Integer value indicating the button clicked by the user.
Displays a dialog box containing a message, buttons, and optional icon to the user. The action taken by the user is returned by the function in the form of an integer value.
prompt can contain approximately 1,000 characters, including carriage return characters such as the built-in vbCrLf constant.
If the title parameter is omitted, the name of the current application or project is displayed in the titlebar.
If the helpfile parameter is provided, the context parameter must also be provided, and vice versa.
In VB5 and VB6 applications, when both helpfile and context are passed to the MsgBox function, a Help button is automatically placed on the MsgBox dialog, allowing the user to click and obtain context-sensitive help. However, in VB4, the user was not presented with a Help button and could access help only by pressing the F1 key. VBA applications such as Excel automatically show a Help button.
If you omit the buttons argument, the default value is 0; that is, VB opens an application modal dialog containing only an OK button.
The following intrinsic constants can be added together to form a complete buttons argument:
ButtonDisplayConstant + IconDisplayConstant + _ DefaultButtonConstant + ModalityConstant
Only one constant from each group can make up the overall buttons value.
Button Display Constants
Constant | Value | Buttons to Display |
---|---|---|
vbOKOnly | 0 | OK only |
vbOKCancel | 1 | OK and Cancel |
vbAbortRetryIgnore | 2 | Abort, Retry, and Ignore |
vbYesNoCancel | 3 | Yes, No, and Cancel |
vbYesNo | 4 | Yes and No |
vbRetryCancel | 5 | Retry and Cancel |
Icon Display Constants
Constant | Value | Icon To Display |
---|---|---|
vbCritical | 16 | Critical Message |
vbQuestion | 32 | Warning Query |
vbExclamation | 48 | Warning Message |
vbInformation | 64 | Information Message |
Default Button Constants
Constant | Value | Default Button |
---|---|---|
vbDefaultButton1 | 0 | First button |
vbDefaultButton2 | 256 | Second button |
vbDefaultButton3 | 512 | Third button |
vbDefaultButton4 | 768 | Fourth button |
Modality Constants
Constant | Value | Modality |
---|---|---|
vbApplicationModal | 0 | Application |
vbSystemModal | 4096 | System |
The following intrinsic constants determine the action taken by the user and represent the value returned by the MsgBox function:
If the MsgBox contains a Cancel button, the user can press the Esc key, and the function's return value is that of the Cancel button.
The Help button doesn't itself return a value, because it doesn't close the MsgBox dialog. If the user clicks the Help button, a Help window is opened. Once the Help window is closed, the user clicks one of the other buttons on the message box to close the dialog; this then returns a value.
Application modality means that the user can't access other parts of the application until a response to the message box has been given. In other words, the appearance of the message box prevents the application from performing other tasks or from interacting with the user other than through the message box.
System modality used to mean that all applications were suspended until a response to the message box was given. However, with multitasking operating systems such as Windows 95 and Windows NT, this isn't the case. Basically the message box is defined to be a "Topmost" window that is set to "Stay on Top," which means that the user can switch to another application and use it without responding to the message box, but because the message box is the topmost window, it's positioned on top of all other running applications.
Unlike its InputBox counterpart, MsgBox can't be positioned on the screen; it's always displayed in the center of the screen.
If your application is to run out-of-process on a remote machine, you should remove all MsgBox functions, since they won't be displayed to the user, but will instead appear on the monitor of the remote server! For in-process server components, VB now has an option to compile an ActiveX DLL for Unattended Execution, in which case all user interface references are instead written to an event log when the program executes. However, you must decide how your program would execute if this were the case; if you're looking for a particular return value from a Msgbox function for your program to continue, your program may not function correctly (if at all) if these references are automatically removed.