[Previous] [Table of Contents] [Next]

Invoking an Input Dialog Box in VBScript

VBScript supports the InputBox function to invoke an input dialog box. The function has the following syntax:

result = InputBox(prompt[, [title], [default], [xpos], [ypos]])

The dialog box has a message, title bar text, and a simple text box, as shown in Figure 8-1. The user can enter text in the text box and click either of the command buttons.

Figure 8-1 An input dialog box created using the InputBox function

The InputBox function has the following parameters:

InputBox supports a sixth (helpfile) and seventh (context) parameter. Both parameters are optional and can be used to connect a help file (in .hlp format) to an input dialog box. Because creating custom help files is beyond the scope of this book, I omitted those parameters.

If an optional parameter is omitted, VBScript uses a default value. To omit an optional parameter between other parameters, you must use a pair of empty commas, as shown here:

InputBox("Hello", "Test", , 100, 200)

The InputBox function has no buttons parameter (as MsgBox does). When the user closes the dialog box, InputBox returns a result value that depends on the button the user clicked to close the dialog box. The OK button returns the content of the text box (the user input). The Cancel button aborts the dialog box and returns an empty string. You can use the result value to check whether the user input is valid.

The following code sequence checks the result value and uses an If statement to show the result:

If result = "" Then    ' Test for Cancel.
    WScript.Echo "Canceled"
    WScript.Echo "You entered: " & result
End If

The VBScript program shown in Listing 8-1 displays an input dialog box containing a text box with a default value, which the user can change. After the user closes the dialog box, the script displays a second dialog box showing the result (Figure 8-2, left) or a cancellation message (Figure 8-2, right).

Figure 8-2 User input retrieved using InputBox

The text shown in the dialog box is declared in the script's header using global variables. Storing the text in variables keeps the InputBox function call as simple as possible. Input.vbs also uses the VBScript constant vbCrLf to split the output string onto two lines. (Like the other samples shown in this chapter, Input.vbs is in the \WSHDevGuide\Chapter08 folder on the book's companion CD.)

Listing 8-1 Input.vbs

' File:    Input.vbs (WSH sample in VBScript) 
' Author:  (c) G. Born
' Retrieving user input in VBScript
Option Explicit

Dim Message, result
Dim Title, Text1, Text2

' Define dialog box variables.
Message = "Please enter a path"           
Title = "WSH sample user input - by G. Born"
Text1 = "User input canceled"
Text2 = "You entered:" & vbCrLf

' Ready to use the InputBox function
' InputBox(prompt, title, default, xpos, ypos)
' prompt:    The text shown in the dialog box
' title:     The title of the dialog box
' default:   Default value shown in the text box
' xpos/ypos: Upper left position of the dialog box 
' If a parameter is omitted, VBScript uses a default value.

result = InputBox(Message, Title, "C:\Windows", 100, 100)

' Evaluate the user input.
If result = "" Then    ' Canceled by the user
    WScript.Echo Text1
    WScript.Echo Text2 & result
End If

'*** End