Lesson VI.             MsgBox() and InputBox()

 

Objectives

ü       To be able to use the MsgBox Function

ü       To be able to use the InputBox Function

 

 

 


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

 
 

 

 


IN FOCUS: MESSAGE BOX

 

The format of the Message Box function is as follows:

 

<intVariable> = MsgBox(<str_message>, <int_type>, <str_title>)

 

<intVariable> is an Integer variable that receives the return value of the function.  If you do not need to determine which button the user clicked (for example, when there is only one button), you may use the function by executing:

 

MsgBox<str_message>, <int_type>, <str_title>

 

<str_message> is a String literal or a String variable that contains the message to be displayed in the Message Box.  <int_type> is an optional numeric value or expression the describes the options you want in the Message Box.  This value is the sum of the values of the options (see the tables below) you want to invoke.  For example, if you want to display the Yes and No buttons, vbQuestion icon, and set the No button as the default button, then the value of <int_type> is 292 (4 + 32 + 256).  You may also use their corresponding named literal as in vbYesNo + vbQuestion + vbDefaultButton2. <str_title> is a String literal or variable that contains the text that appears in the Title Bar of 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.

            The Buttons displayed in a Message Box


 

Named Literal

Value

Description

vbCritical

16

Displays the Critical Message Icon.

vbQuestion

32

Displays the Warning Query Icon.

VbExclamation

48

Displays Warning Message Icon.

vbInformation

64

Displays Information Message Icon.

            The Icons 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 Default Buttons displayed in a Message Box

 

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

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.

            MsgBox()’s Return Values

 

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")

 

 

 

 

 

 

 

 

 

 

MsgBox("This is another MessageBox", vbAbortRetryIgnore + vbDefaultButton2 + vbExclamation, "Sample 2")

 

 

 

 

 

 

 

 

 

 

InputBox("This is an Input Box", "Sample", "test")

 

 

 

 

 

 

 

 

 

 

 


 

 


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.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Write the following procedure:

 

Private Sub Form_Load()

    num = InputBox("Enter a number from 1 to 10.", "Input", "")

    If ((num < 1) Or (num > 10)) Then

        MsgBox "Please enter a number from 1 to 10 only!", vbCritical, "Invalid Entry"

    Else

        lblOutput.Caption = "You entered number " & num & "."

     End If

End Sub

 

 
Save your work as Lesson6.vbp.

 

 

 


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.

 

 

[ Home ]