The InputBox Function

 

Displays a prompt in a dialog box, waits for the user to input text or click a button, and returns a string containing the contents of the text box.

 

Usage:

stringvariable = InputBox(prompt[, title] [, default] [, xpos] [, ypos] [, helpfile, context])

 

The arguments to the InputBox function are described below:

 

Argument

Description

prompt

Required. String expression displayed as the message in the dialog box. The maximum length of prompt is approximately 1024 characters, depending on the width of the characters used.

title

Optional. String expression displayed in the title bar of the dialog box. If you omit title, the application name is placed in the title bar.

default

Optional. String expression displayed in the text box as the default response if no other input is provided. If you omit default, the text box is displayed empty.

xpos and ypos

Both optional. Numeric expressions that specify custom positioning of the box on screen (by default, the box is displayed in the center of the screen, which is usually desired).

helpfile and context

Both optional. Can be used if a help file has been set up for the application. If either one of these arguments are used, they both must be used.

 

Remarks

If the user clicks OK or presses ENTER , the InputBox function returns whatever is in the text box. If the user clicks Cancel, the function returns a zero-length string ("").

 

 

In the example given in the previous topic, an InputBox was used to obtain the user's name. Recall that a string variable (strName) was declared and used to store the result of the InputBox function:

 

    Dim strName As String

   

    strName = InputBox("Enter your name:", "Input Test")

 

 

Note that the InputBox function in this example used "Enter your name:" as the required prompt argument, and "Input Test" as the optional title argument (which caused that text to be displayed in the title bar of the dialog box). When the program was first run, the InputBox looked like this:

 

 

 

The user then entered his name into the text area of the InputBox:

 

 

When the user clicked OK, the string "Harry" was stored in the variable strName. Had the user clicked Cancel, the zero-length string ("") would have been stored in strName.

 

 

The next example shows how the default argument is used.

 

Let's say you need the user to enter an account number, but the majority of the time the program will be processing account "ABC-123". You could provide the string "ABC-123" as the third argument to InputBox:

   

    strAcctID = InputBox("Enter account ID:", "Input Test", "ABC-123")

 

When the program is run, the text entered as the default argument shows up pre-filled and highlighted in the text entry area of the InputBox:

 

 

The user can either accept that and click OK, or they can type the "ABC-123" to enter whatever is required and then click OK.

 

If you wish, set up another "Try It" project, copy folder where you saved the original "Try It" project over to a new folder, open the copied project, and replace the cmdTryIt_Click event with the following code:

 

Private Sub cmdTryIt_Click()

 

    Dim strAcctID As String

   

    strAcctID = InputBox("Enter account ID:", "Input Test", "ABC-123")

   

    Print "Data for account ID "; strAcctID; " will be processed."

   

End Sub

 

When you run the project and click the "Try It" button, the InputBox with the default entry as shown ablove will be displayed. If you accept it and click OK, the output should look like this:

 

 

 

Download the VB project files for the example above here.