Professional Documents
Culture Documents
Objectives
Notes
You use Message and Input Boxes when you need to ask the user questions or display error
messages and advise the user.
Input Box
Message Box
Input Box
Valu
e
0
1
2
vbYesNoCancel
vbYesNo
vbRetryCancel
4
5
Description
Displays
Displays
Displays
buttons
Displays
buttons.
Displays
Displays
the OK button.
the OK and Cancel buttons.
the Abort, Retry, and Ignore
the Yes, No, and Cancel
the Yes and No buttons.
the Retry and Cancel buttons.
Named Literal
vbCritical
vbQuestion
VbExclamation
vbInformation
Valu
e
16
32
48
64
Description
Displays
Displays
Displays
Displays
Named Literal
vbDefaultButton1
vbDefaultButton2
Valu
e
0
256
Description
The first button is the default.
The second button is the default.
VbDefaultButton3
512
If your Message Box has more than 1 button, you determine which button is pressed by checking
the value of <intVariable>. The return values are enumerated in the following table.
Named Constant
vbOk
vbCancel
vbAbort
vbRetry
vbIgnore
vbYes
vbNo
Valu
e
1
2
3
4
5
6
7
Description
The
The
The
The
The
The
The
user
user
user
user
user
user
user
clicked
clicked
clicked
clicked
clicked
clicked
clicked
the
the
the
the
the
the
the
Ok button.
Cancel button.
Abort button.
Retry button.
Ignore button.
Yes button.
No button.
The sample Message Box on the previous page was created through:
MsgBox "You must enter A, B, or C.", vbCritical, "Error"
IN FOCUS: INPUT BOX
Input Box is very similar to a Message Box but instead of the 7 return values that indicate which
button was pressed, the Input Box returns a string data value entered by the user.
The following is the format of Input Box function:
<strVariable> = InputBox (<str_prompt>, <str_title>, <str_default>, <intXPos>, <intYPos>)
<str_prompt> works like <str_message> value in Message Box. It is the text that appears in the
Input Box. It is usually an instruction on what the user should enter in the TextBox provided for in
the Input Box. <str_title> is what appears in the Title Bar. <str_default> is the default String in the
TextBox. <intXPos> and <intYpos> determine the position of the Input Box relative to the Form.
They hold the number of twips from the top and left edge of the Form window to the top and left
edge of the Input Box, respectively. <str_title>, <str_default>, <intXPos>, <intYpos> are optional
parameters.
Input Boxes always containt Ok and Cancel buttons. If the user presses the Ok button, the function
returns the String entered in the TextBox. If the user presses the Cancel button, a null String () is
returned.
Examples:
MsgBox("This is a MessageBox", vbRetryCancel + vbInformation, "Sample")
Lesson in Action
The following application uses both Input and Message Boxes. When the application is executed
(therefore, place your code in the Form_Load procedure), an Input Box will ask for a number
from 1 to 10. A Message Box is displayed when the user enters a number not in the range.
When a valid entry is received, simply display that value in a Label on the Form.
The following are screen shots of the Input and Message Boxes.
On your Own
1.
Write an application that asks for your age through an InputBox() and then displays your
age 5 years ago through a MsgBox(). Display a default value of 10 in the InputBox. Note:
if the age entered is <=5, output You were not yet born 5 years ago. The InputBox
should automatically appear during program startup.
2.
Create a program with one CommandButton and one Label. When the user presses the
button, a MsgBox appears with 3 buttons: Yes, No, Cancel. Display which button was
clicked in the Label.