Visual Basic Free Tutorial

Web based School

Chapter 6

Improving Code: Message and Input Boxes

In this and subsequent lessons, your application will need to display messages and ask questions of the user. The application needs to receive the user's response from the questions. Although the Label and Text Box controls work well for giving and receiving user information, such controls don't lend themselves to messages and questions that the program displays during execution such as error messages and warning boxes.

For example, suppose you want to know if the user has prepared the printer for printing. To prepare a printer, the user has to turn on the printer, make sure paper is there, and ensure that the online light is on. Your program should not attempt to print a report until the user has performed these actions or an error will occur. Therefore, when the user initiates a report for printing, your application can gather the data and then ask the user if the printer is ready. If the user responds affirmatively, you can start the report's output. The form's controls simply do not provide such interaction. In this hour's lesson you will learn how to display message boxes and input boxes that provide runtime I/O.

The highlights of this hour include

  • How message boxes differ from text boxes

  • Why functions benefit programmers

  • When to test message box return values

  • Why to add remarks

  • How to receive input box answers

A Function Preview

The programming language you've learned so far--the variable declarations, assignments, and operator usage--has focused on programming statements. This lesson begins to discuss a new kind of programming language concept called a function.

Visual Basic comes supplied with several built-in functions (often called intrinsic functions) that do work for you. Many functions perform common mathematical tasks such as computing a square root. Other functions manipulate sting data such as converting text inside a string to uppercase or lowercase letters. Other functions, such as the functions taught in this lesson, perform input and output.

A function is a routine that accepts zero, one, or more arguments and returns a single result based on the argument list.

An intrinsic function is a function supplied with Visual Basic.

NOTE: Chapters 13, "Modular Programming," and 14, "Built-in Functions Save Time," describe how you can write your own functions.

A function takes zero, one, or more arguments and converts those arguments to a single return value. Figure 6.1 shows an overview of a function's job. The most important thing to remember is that a function always returns a single value.

New Term: An argument is a value you pass to a function so the function has data to work with.

Figure 6.1. A function accepts arguments and returns a single value.

A function's job is to save you time. For example, if you need to compute the square root of a user's entered value, you could write the assignments and expressions to compute the square root. The square root, however, is such a common routine that Microsoft wrote the code once and stored the square root routine in an intrinsic function. Now, if you want the square root of a value, you'll pass the value as a single argument to the square root function, and after performing the necessary math, the square root function will return the root.

This lesson focuses on two intrinsic functions that don't do math. Instead, they display messages or receive user input. Don't worry too much about what a function is as long as you have the general idea. You'll become much more familiar with them before you're through with this tutorial.

This lesson spends the rest of the hour teaching you these functions:

  • MsgBox()

  • InputBox()

NOTE: Function names, unlike variable names, usually appear with parentheses at the end. The parentheses hold the function arguments that you send to the function. Even if a function receives no arguments, the parentheses are still required when you use the functions.

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 dialogs. 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.

New Term: A message box is a dialog box you display to give the user information.

New Term: An input box is a dialog box you display to ask the user questions.

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

WARNING: You may hear about a Visual Basic statement called the MsgBox statement (as opposed to the MsgBox() function). Although Visual Basic does still support the MsgBox statement, Microsoft recommends that you use only the MsgBox() function due to its inspection ability for a return value. (The MsgBox statement does not even appear in Visual Basic 5's online help.)

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 guide 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 a title that will go at the top of a printed report listing.

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 title at the bottom of the input box and pressing Enter or clicking the OK command button. The user also can click the Cancel command button whether or not the user entered a title. 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]])

NOTE: The MsgBox() function's format shown here accepts one required (strMsg) and two optional (intType and strTitle) arguments. MsgBox() can accept more arguments, but these three are the only ones needed in most applications.

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. The buttons displayed in a 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. The icons displayed in a 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.

Table 6.3. The default buttons displayed in a 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 is application specific, the user must respond to the message box before the user can do 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.

New Term: 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, but the user can switch to another Windows program by pressing Alt+Tab or by switching to another program 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.

NOTE: If you don't specify an icon, Visual Basic doesn't display an icon. If you don't specify the system modality, Visual Basic assumes that you want an application-modal message box.

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

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

  vbYesNoCancel, "Report Request")

TIP: If you need to type long VB program statements, such as this MsgBox() function, you can break the line into multiple, more manageable lines by terminating the first line with an underscore character (_).

Figure 6.4. Message boxes support several command buttons.

Remember that the MsgBox() values such as vbQuestion and vbYesNoCancel are not variables but are 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 pressed the Yes button in Figure 6.4. The program could then print the report. If, however, the user pressed 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.

NOTE: You'll learn how to test for specific values in Hour 7, "Making Decisions."

Visual Basics Code Window Help

Can you remember the named literals in this lesson's tables? How can you remember that the named literal value to display three buttons--Yes, No, and Cancel--is the vbYesNoCancel named literal?

Fortunately, with version 5, Visual Basic now supplies you with all the help you need. As soon as VB's Code window editor recognizes that you're entering a function, the editor immediately displays pop-up help that displays the function's format, as shown in Figure 6.5.

Figure 6.5. Visual Basic displays the function's format for you.

Visual Basic give you help not only with a function's format, but also with the function's named literals. When you get to any function argument that requires one of the named literals, Visual Basic displays a drop-down list box such as the one in Figure 6.6, from which you can select a named literal. To accept the selected named literal, press Enter, type a comma, or press the Spacebar to continue with the program.

NOTE: The format and argument list box pop-up help appears all throughout Visual Basic. As you learn additional Visual Basic statements, you'll see the pop-up Code window help more often.

Figure 6.6. Visual Basic displays the function's named literals.

A Short Detour: Remarks

Figures 6.5 and 6.6 show two new program statements you've not seen to this point. Two remark statements appear in each figure. Remarks help both you and other programmers who might modify and update your Visual Basic applications in the future. Remarks offer descriptive messages that explain in English (or whatever language you prefer) what's going on in the program's event procedures.

It's said that a program is written once and read many times. That saying is true because of the nature of applications. Often, you'll write a program that helps you or your business compute required calculations and keep track of daily transactions. Over time, requirements change. Businesses buy and sell other businesses, the government changes its reporting and taxing requirements, and people's needs change. You should realize that, after you write and implement a program, you will make modifications to that program later. If you use the program in a business, you'll almost certainly make many modifications to the program to reflect changing conditions.

TIP: If you program for someone else or as part of a team, the chances are high that others will modify the programs that you write and that you'll modify programs that other programmers write. Therefore, as you write programs, think about the future maintenance that you and others will make. Write your programs clearly, using ample spacing and indentation, and add remarks that explain difficult sections of code.

New Term: A remark is a message that you put inside a program's code. Programmers concerned about maintenance know that ample remarks help clarify code and aid future maintenance. Visual Basic completely ignores any and all remarks because those remarks are for people looking at your program code. Users don't see remarks because users don't see the program's code; rather, users see a program's output.

Programmers often add remarks to their programs for the following purposes:

  • To state the programmer's name and the date that the program was written

  • To describe in the general section the overall goal of the program (the general section appears before all of a procedure's procedures and is the location Hour 5, "Putting Code into Visual Basic," described when it talked about declaring global variables)

  • To describe at the top of every procedure the overall goal of that procedure

  • To explain tricky or difficult statements so that others who modify the program later can understand the lines of code without having to decipher cryptic code

Even if you write programs for yourself, and if you are the only one who will modify your programs, you should still add remarks to your programs! Weeks or months after you write a program, you'll have forgotten the exact details of the program, and remarks that you interspersed throughout the code will simplify your maintenance and will help you find the code that you need to change.

TIP: Add remarks as you write your programs. Often, programmers say to themselves, "I'll finish the program and add remarks later." Trust me--the remarks don't get added. It's only later, when programmers need to modify the program, that they notice the lack of remarks--and regret it.

Add remarks to your program so that you and others can more quickly grasp the nature of the program and can make modifications to it more easily when needed. Visual Basic supports several remark formats. Unlike in some other programming languages, Visual Basic remarks are easy to add to your code, and their free-form nature enables you to add remarks whenever and wherever needed.

Visual Basic supports two kinds of remarks:

  • Remarks that begin with the Rem statement

  • Remarks that begin with the apostrophe (`)

The Rem statement is more limiting than the apostrophe and isn't as easy to use. Nevertheless, you'll run across programs that use Rem statements, so you should learn how Rem works. Here is the format of the Rem statement:

Rem The remark's text

You can put anything you want in place of The remark's text. The following are examples of remarks:

Rem Programmer: Grant Holdorf, Date: Mar-27-1999


Rem This program supports the check-in and check-out

Rem  process for the dry-cleaning business.


Rem This event procedure executes when the user

Rem  clicks on the Exit command button. When pressed,

Rem  this event procedure closes the program's data

Rem  files, prints an exception report, and terminates

Rem  the application

The first of these remark sections consists of a one-line remark that tells the programmer's name and the date that the program was last modified. If someone else must modify the program later, that person can find the original programmer if needed to ask questions about the program's code. The second remark describes the overall program's goal by starting with a high-level description of the program's purpose. The third remark might appear at the top of a command button's Click event procedure.

As you can see, you can add one or more lines of remarks depending on the amount of description needed at that point in the program. Visual Basic ignores all lines that begin with Rem. When someone looks at the program code later, that person will know who the programmer is, the date that the program was written, the overall purpose of the program, and the overall description of each procedure that includes a remark section.

Say that you used apostrophes in place of the Rem statement in the previous remarks. The following rewritten remarks demonstrate that the remarks are even more effective because Rem doesn't get in the way of each remark's text:

` Programmer: Grant Holdorf, Date: Mar-27-1999


` This program supports the check-in and check-out

`  process for the dry-cleaning business.


` This event procedure executes when the user

`  clicks on the Exit command button. When pressed,

`  this event procedure closes the program's data

`  files, prints an exception report, and terminates

`  the application

The remarks don't have to go at the beginning of event procedures. You can place remarks between lines of code, as done here:

Dim intRec As Integer

Rem Step through each customer record

For intRec = 1 To intNumCusts

  ` Test for a high balance

  If custBal(intRec) > 5000 Then

    Call PayReq

  End If

Next intRec

NOTE: Don't try to understand the details of this code yet. Concentrate now on the remarks. The code contains some advanced features (Visual Basic arrays and subroutine procedures) that you'll learn about in the last half of this guide.

You can place apostrophe remarks at the end of Visual Basic statements. By placing a remark to the right of certain lines of code, you can clarify the purpose of the code. Consider how the following code section uses a remark to explain a specific line of code:

a = 3.14159 * r ^ r    ` Calculate a circle's area

Perhaps only a mathematician could interpret the formula without the remark. The remark helps even non-mathematicians understand the purpose of the statement. There is no reason that you should have to re-examine code every time you look at it. By reading remarks, you can glean the code's purpose without taking the time to interpret the Visual Basic code.

The wrong kind of remarks won't help clarify code, though, so don't overdo remarks. As a matter of fact, lots of lines of code need no remarks to explain their purpose. The following remark is redundant and wastes both your programming time and the time of anyone who may maintain the program later:

Dim Sales As Single   ` Define a variable named Sales

Examining InputBox()

You'll find that the InputBox() function is easy because it acts a lot like the MsgBox() function. The InputBox() function receives answers that are more complete than the MsgBox() function can get. Whereas MsgBox() returns one of seven values that indicate the user's command button press, the InputBox() function returns a string data value that holds the answer typed by the user.

Here is the format of the InputBox() function:

strVariable = InputBox( strprompt [, [strTitle] [, strDefault]

         Â[, intXpos, intYpos]]])

strPrompt works a lot like the strmsg value in a MsgBox() function. The user sees strPrompt inside the input box displayed on the screen. strTitle is the title inside the input box's title bar. strDefault is a default string value that Visual Basic displays for a default answer, and the user can accept the default answer or change the default answer.

The intXpos and intYpos positions indicate the exact location where you want the input box to appear on the form. The intXpos value holds the number of twips from the left edge of the Form window to the left edge of the input box. The intYpos value holds the number of twips from the top edge of the Form window to the top edge of the input box. If you omit the intXpos and intYpos values, Visual Basic centers the message box on the form.

NOTE: Input boxes always contain OK and Cancel command buttons. If the user clicks OK (or presses Enter, which selects OK by default), the answer in the input box is sent to the variable being assigned the returned value. If the user clicks Cancel, a null string ("") returns from the InputBox() function.

The following statement displays an input box that asks the user for a company name. The user either enters a response to the prompt or clicks the Cancel command button to indicate that no answer is coming.

strCompName = InputBox("What is the name of the company?",

          Â "Company Request", "XYZ, Inc.")

TIP: You can offer a default answer that the user can accept or change in the strDefault argument. The input box function returns the answer to the string variable to which you assign the function.

Figure 6.7 contains the message box displayed from this InputBox() function.

Figure 6.7. Asking the user a question and getting the answer with InputBox().


This hour introduces functions so you can prepare for message boxes and input boxes. Message boxes display output, and input boxes get input. The message and input boxes offer ways for your programs to request information that regular controls can't handle. Use controls to display and get data values that are always needed. Use message and input boxes to display messages and get answers that the program needs in special cases, such as for error conditions and exception handling.

The next hour explains how to test the return values from this hour's functions as well as shows you additional operators with which your applications can make decisions.


Q When do I use controls and when do I use message and input boxes?

You use Form controls when the user is to interact with a form and enter values the form module will process. The Toolbox controls are extremely useful for guiding the user through a list of choices. The message box is a program feature you can use to display one-time notes and warnings to your users. The input box is a great one-time dialog box you can display to ask the user for questions when needed during the execution of the program.

Q Why should I add remarks to my code?

You'll modify your programs over time. The more you modify a program, the faster that modification (called maintenance) will go if you add ample remarks at the time you create the program. The remarks help you remember what a particular section of code is for. In addition to remarks, use named literals when available for options such as the message box button type because the named literal mnemonics are easier to remember than their numeric equivalents.


The quiz questions and exercises are provided for your further understanding. See Appendix C, "Answers," for answers.


1. What is the difference between a message box and a text box?

2. Which stays on the user's screen during the majority of a program's execution: a text box or an input box?

3. Why do the named literals provide for better program maintenance?

4. What are the two kinds of remark statements?

5. Who are remarks for?

6. What does modal mean?

7. How many icons can you display with message boxes?

8. True or false: You can pass multiple arguments and receive multiple return values from functions.

9. What role do default values play in input boxes?

10. True or false: The MsgBox() function can return one of seven values.


1. Write three remarks for the top of a program that calculates sales tax. The first remark should hold your name, the second should hold the date that you write the remark, and the third should span at least two lines and should describe the purpose of the program.

2. Write an input box function that asks users for their ages. Display a default value of 25.