Sams Teach Yourself Visual Basic 6 in 24 Hours

Sams Teach Yourself Visual Basic 6 in 24 Hours

By Greg Perry

A MsgBox() and InputBox() Overview

You use input boxes and message boxes when you need to ask the user questions or display error messages and advice to the user. As stated earlier, the form's controls don't often work well for such user dialog boxes. For example, suppose the user is to enter a sales code of A, B, or C to indicate a discount to be used in a total calculation. Users don't always know what's expected of them, so a message box can pop up when the user enters a bad value, and the message box can explain that the user needs to enter only A, B, or C. If the user enters an invalid code, your program could display an error message such as the one shown in Figure 6.2.

06fig02.gif

Figure 6.2 A message box can tell the user what to do.

A message box is a dialog box you display to give the user information. An input box is a dialog box you display to ask the user questions. A message box is typically used to display a short single sentence message to the user. Program execution continues after the user acknowledges the message box. An input box, on the other hand, can be thought of as a message box with a text box. An input box displays a prompt to the user and obtains text input from the user.

The Text Box controls that you've seen are great for getting values from the user. Other controls that you'll learn as you progress through this book also accept the user's input from the keyboard or mouse. Nevertheless, Visual Basic's controls just aren't enough to handle all the input that your program will need. Input boxes are great to use when the user must respond to certain kinds of questions. Text boxes and other controls are fine for getting fixed input from the user, such as data values with which the program will compute. Input boxes are great for asking the user questions that arise only under certain conditions. Input boxes always give the user a place to respond with an answer. In Figure 6.3, the input box is asking the user for the name of a server.

06fig03.gif

Figure 6.3 Input boxes get user information.

Note that there is more than one way for the user to respond to the input box in Figure 6.3. The user can answer the question by typing the name of the server and pressing Enter or clicking OK. The user also can click Cancel whether or not the user entered the server name. Therefore, the program must be capable of reading the user's entered answer as well as responding to a Cancel command button press. Responding to message box and input box command buttons is part of the processing that you'll learn about in the remaining sections of this lesson.

Examining MsgBox()

Always assign a MsgBox() function to an integer variable. The variable will hold the return value, and that value will indicate the button the user clicked (message boxes can display multiple buttons such as OK and Cancel).

Here is the format of the MsgBox() function:


   anIntVariable = MsgBox( strMsg [, [intType] [, strTitle]])

strMsg is a string (either a variable or a string constant enclosed in quotation marks) and forms the text of the message displayed in the message box. intType is an optional numeric value or expression that describes the options you want in the message box. Table 6.1, Table 6.2, and Table 6.3 contain all the possible values you can use for the type of message box you want displayed. (Visual Basic displays no icon if you don't specify an intType value.) If you want to use a value from two or more of the tables, you'll add the values together. Although you can use the integer value, if you use the built-in Visual Basic named literal, you'll more easily understand the message box's style if you ever have to change the message box in the future. strTitle is an optional string that represents the text in the message box's title bar. If you omit strTitle , Visual Basic uses the project's name for the message box's title bar text.

Table 6.1. Select the buttons displayed in the message box.

Named Literal Value Description
vbOKOnly 0 Displays the OK button.
vbOKCancel 1 Displays the OK and Cancel buttons.
vbAbortRetryIgnore 2 Displays the Abort, Retry, and Ignore buttons.
vbYesNoCancel 3 Displays the Yes, No, and Cancel buttons.
vbYesNo 4 Displays the Yes and No buttons.
vbRetryCancel 5 Displays the Retry and Cancel buttons.

Table 6.2. Select the icon displayed in the message box.

Named Literal Value Description
vbCritical 16 Displays Critical Message icon.
vbQuestion 32 Displays Warning Query icon.
vbExclamation 48 Displays Warning Message icon.
vbInformation 64 Displays Information Message icon.
VbSystemModal 4096 Displays a SystemModal dialog box. The user must acknowledge a SystemModal dialog box before doing anything else.

Table 6.3. Select the default button in the message box.

Named Literal Value Description
vbDefaultButton1 0 The first button is the default.
vbDefaultButton2 256 The second button is the default.
vbDefaultButton3 512 The third button is the default.

The options that you select using the intType value in the MsgBox() function determine whether the message box displays an icon and controls the modality of the message box. The modality determines whether a message box is application-specific or system-specific. If it's application-specific, the user must respond to the message box before doing anything else in the application. If the message box is system-specific, the user must respond to the message box before doing anything else on the system.

Modality determines how the system handles a dialog box.

The modality often causes confusion. If you don't specify a system-modal intType value of 4096 (or if you don't use the named literal vbSystemModal to specify the system's modal mode), the user's application will not continue until the user closes the message box. However, the user can switch to another Windows program by pressing Alt+Tab or using the application's control menu. If, however, you do specify that the message box is system modal, the user will not be able to switch to another Windows program until the user responds to the message box because the message box will have full control of the system. Reserve the system-modal message boxes for serious error messages that you want the user to read and respond to before continuing the program. You may make a message box a System Modal message box by adding 4096 to the intType value of the message box.

The following MsgBox() function produces the message box shown in Figure 6.4:

06fig04.gif

Figure 6.4 Message boxes support several command buttons.

intPress = MsgBox("Are you ready for the report?", vbQuestion + _
vbYesNoCancel, "Report Request")

Remember that the MsgBox() values such as vbQuestion and vbYesNoCancel are not variables but named literals that Visual Basic has defined to correspond with matching integer values. The named literals vbQuestion and vbYesNoCancel produced both a question mark icon and the three buttons. A title also appeared due to the third value inside the MsgBox() function.

MsgBox()'s Return Value

The reason that you assign MsgBox() functions to variables is so you can tell which button the user presses. Suppose that the user clicked the Yes button in Figure 6.4. The program could then print the report. If, however, the user clicked the No button, the program could describe what the user needed to do to get ready for the report (load paper, turn on the printer, and so on). If the user pressed the Cancel button, the program would know that the user didn't want the report at all.

Table 6.4 lists the seven possible MsgBox() return values. You can test either for the integer or the named literal return value.

Table 6.4. MsgBox() return values.

Named Constant Value Description
vbOK 1 The user clicked the OK button.
vbCancel 2 The user clicked the Cancel button.
vbAbort 3 The user clicked the Abort button.
vbRetry 4 The user clicked the Retry button.
vbIgnore 5 The user clicked the Ignore button.
vbYes 6 The user clicked the Yes button.
vbNo 7 The user clicked the No button.

Share ThisShare This

Informit Network