Earlier, you used a MsgBox to pop up a message on the screen.
You can use a similar box to get input from your students.
The only difference is that the new dialogue box will have
a space for your students to type something. We'll start
with something simple: asking for the student's name.
Sub YourName()
userName = InputBox(Prompt:="Type your name",
_
Title:="Input
Name")
End Sub
There are a few important things about this simple procedure.
First, pay attention to the space and underscore at the end
of the line. The last three characters on the second line
are comma, space, and underscore. Without the space, the
computer won't recognize the underscore that follows. The
underscore is a special VBA character that tells VBA that
what is on the next line is part of this line. Therefore
that entire line could have been written on one line without
the underscore:
userName = InputBox(Prompt:="Type your name",
Title:="Input Name")
The underscore simply allows you to divide long lines so
you don't have to scroll to the right to see what is on each
line. Feel free to write long lines on one line or divide
them up among several lines as you see fit.
The next thing that is important about this small piece
of code is that it uses a variable: userName. Since we don't
do anything with the variable at this point, it is not terribly
interesting, but we should note a few things about variables.
Variables are places to store information. You can think
of them as boxes in the computer's memory. Unlike algebraic
variables, which represent one (or more than one) specific,
unchanging value in an equation or series of equations, computer
variables change values. That is, you can take something
out of a box and put something else into the box. In algebra,
the equation
x = x + 1
would not make any sense. In the computer, it makes perfect
sense for two reasons:
- While the variable x can only hold one value at a time,
that value can change. At one time x might hold the value
7, and a moment later, x might hold the value 8.
- The equal sign (=) is not a statement of equality. It
is an assignment operator. It says, take the value on the
right side and store it in the variable named on the left
side. Therefore, the above equation is not a statement
of algebraic fact; it is an action. The part on the right
(x+1) says, find what the value of x is and add one to
it; the rest (x =) says, store that value in x. That is,
if x was 7, it will now be 8. Using the box analogy, it
says, look in the box we call "x," add one to
what you find there, and put the result back in the box.
In the YourName procedure, we have used the variable userName.
What we have said is: Take whatever the user types in the InputBox and
put it into a variable called userName. Later, we
will want to use the name (to say, for example, "Good
job, Ella") so we will get it out of the userName box
when we are ready.
Now, we are ready to put it all together with a Dim statement
and two procedures:
Dim userName As String
Sub YourName()
userName = InputBox(prompt:="Type your name",
_
Title:="Input
Name")
End Sub
Sub DoingWell()
MsgBox("You are doing well, " & userName)
End Sub
The first procedure could be associated with a button on
the first slide, and the second procedure could be associated
with a button on a later slide. The result would be that
when the first button was pressed, the student would be asked
to "Type your name." If the student types "Ada," when
the second button is pressed, a message would pop up on the
screen saying, "You are doing well, Ada." The & (ampersand)
character used in the MsgBox procedure is for concatenation
of strings; i.e., the two strings "You are doing well," and
whatever is stored in the variable userName (in this case "Ada")
are joined together to make one string, "You are doing
well, Ada," which is displayed in the box on the screen.