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.

note

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 test box. An Input box displays a prompt to the user and obtains text input from the user.

caution

You might hear about a Visual Basic statement called the MsgBox statement (as opposed to the MsgBox() function). Although Visual Basic still supports the MsgBox statement, Microsoft recommends that you use only the MsgBox() function because of its inspection ability for a return value.

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.

Note that there is more than one way for the user to respond to an input box. 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.

Top Home