Datasheet
Chapter 1: A Quick Introduction to Programming
10
Dim YourName
Dim Greeting
YourName = InputBox(“Hello! What is your name?”)
If YourName = “” Then
Greeting = “OK. You don’t want to tell me your name.”
Else
Greeting = “Hello, “& YourName & “, great to meet you.”
End If
MsgBox Greeting
Walking through the code, you do the following:
1. You declare the two variables that you are going to use:
Dim YourName
Dim Greeting
YourName = InputBox(“Hello! What is your name?”)
You ask the user for some input, again using the InputBox function. This function expects one
required parameter, the prompt text (the text that appears on the input box). It can also accept
several optional parameters. Here, you only use the one required parameter.
Note that the parameter text that you passed
“Hello! What is your name?” is displayed as a
prompt for the dialog box. The
InputBox function returns the value that the user types, if any. If the
user does not type anything or clicks the
Cancel button (both do the same thing), then InputBox
returns a zero-length string, which is a strange kind of programming concept that basically means that
it returns text that doesn’t actually contain any text. Your script stores the result of the
InputBox
function in the
YourName variable.
2. You come to the actual loop you’re going to use:
If YourName = “” Then
Greeting = “OK. You don’t want to tell me your name.”
Else
Greeting = “Hello, “& YourName & “, great to meet you.”
End If
This code presents the VBScript engine with an option that is based on what the user typed (or
didn’t type) into the input box. The first line tests the input from the user. It tests to see if the
input that is stored in the variable
YourName is a zero-length string. If it is, the next line of code
is run and the variable
Greeting is assigned a string. Figure 1-6 shows the message displayed if
the user doesn’t type his or her name into the
InputBox .
c01.indd 10c01.indd 10 8/27/07 7:45:20 PM8/27/07 7:45:20 PM