Datasheet

Chapter 1: A Quick Introduction to Programming
13
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
If YourName = “Fred” Then
Greeting = Greeting & “ Nice to see you Fred.”
End If
MsgBox Greeting
The If...Else...End If block can be extended through the use of the ElseIf clause, and
through nesting. Nesting is the technique of placing a block of code inside of another block of
code of the same type. The following variation on your script illustrates both concepts:
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.”
ElseIf YourName = “abc” Then
Greeting = “That’s not a real name.”
ElseIf YourName = “xxx” Then
Greeting = “That’s not a real name.”
Else
Greeting = “Hello, “& YourName & “, great to meet you.”
If YourName = “Fred” Then
Greeting = Greeting & “ Nice to see you Fred.”
End If
End If
MsgBox Greeting
Once again, seeing how the code has been indented helps you to identify which lines of code are
subordinate to the lines above them. As code gets more and more complex, proper indenting of
the code becomes vital as it will become harder to follow.
Even though the branching logic you are adding to the code tells the script to execute certain
lines of code while not executing others, all the code must still be interpreted by the script
engine (including the code that’s not executed). If any of the code that’s not executed contains
any syntax errors, the script engine will still produce an error message to let you know.
c01.indd 13c01.indd 13 8/27/07 7:45:22 PM8/27/07 7:45:22 PM