Assignment 3 Guide
Implementation
Phase - Coding Step 2: Getting User Input
These
notes guide you through getting user input for your assignment 3 program.
Introduction
Declare your Input Variables
Get the Inputs and Store Them in Variables
Check your Code
Objectives
This guide will help
you to: -
· declare
your input variables in VB
·
get user input
·
check your code
If you need to you should print out the
following two assignment documents.
Assignment Scenario
Assignment 3: Implementation
When a user runs your program and types information such
as lawn width, height etc. you will need to keep the values typed in so you
can use them in the calculations later on. You can keep the values by
storing them in variables.
So your next task is to create a little bit of programming code
by declaring all your input variables.
In Assignment 2 part a
you were asked to decide on formats and names for your variables.
Here is a reminder...
Rectangular
Lawn Length (m) |
floating point number |
single |
rectLawnLength
|
The number must
be larger than 0. Max number of characters will be 4. |
A lawn length
measurement may have a decimal part. E.g. 4.3m. |
Rectangular
Lawn Width (m) |
floating point number |
single |
rectLawnWidth
|
The number must
be larger than 0. Max number of characters will be 4. |
A lawn width
measurement may have a decimal part. E.g. 3.3m. |
etc... |
|
|
|
|
|
I started a table listing all of the inputs I expected from
the user, such as Lawn Length. Then I specified the data
type as a single and a variable name of rectLawnLength .
If you completed this task, you will be able to use the variable names in the
VB coding form to declare your variables.
Your Task
Here are step by step instructions on how to declare your
variables. You are going to declare all your variables inside a separate
coding sheet called a module.
Module Coding Windows
-
If you have not added a module to your project already,
then on the VB menu, click Project , then Add
Module . A new white coding window should appear. You need to save
the separate module just like you save a form. Here is a picture
of my VB explorer window showing two forms (input and output) and
my module.

-
If you double-click the module you should get a white
coding window. In the coding window, declare your variables like so...

You need to carry on declaring your other input variables for
your two fields. You should end up with about 7 input variables for both the
lawns and the fields.
When you have declared your variables, click the save
button
to save your work.
Your next task is to get the inputs entered by the user in
your input form and assign those values to variables.
Now, thinking about what happens when a user runs your
program, they will -
-
run your program and see your input form - we cannot get
any input values yet because the user has not entered anything
-
the user should then enter the input values
-
then they will click a command button indicating they want
your program to display the outputs.
So, it is when the user clicks the command button
that your program needs to get the input values so it can calculate the
outputs.
So, in your program, go to your form1 and double-click your
command button. You should see these lines of code similar to:-
Option Explicit
Private Sub
cmdSubmit_Click()
End
Sub
Note : The part that says cmd Submit
is the name of my command button. Your command button will probably
have a different name. Perhaps you called it cmd Go or cmd Calculate .
To get the user inputs from the textboxes we have to write
code similar to this:-
Option Explicit
Private Sub
cmdSubmit_Click()
rectLawnLength = txtRectLawnLength.Text
rectLawnWidth = txtRectLawnWidth.Text
End Sub
The second line puts the value from the textbox called
txtRectLawnLength into the variable
rectLawnLength .
The third line puts the value from the textbox called
txtRectLawnWidth into the variable
rectLawnWidth .
In other words, both variables will now have whatever values
the user typed into the text boxes.
 |
Example: When
the user clicks my submit button, the variables will be get their
values from the textboxes.
rectLawnLength will be 3
rectLawnWidth will be 4.4
|
Now carry on assigning values to the rest of your input
variables. Just keep adding lines of code like this..
Option Explicit
Private Sub
cmdSubmit_Click()
rectLawnLength = txtRectLawnLength.Text
rectLawnWidth = txtRectLawnWidth.Text
circLawnRadius = txtCircLawnRadius.Text
etc....
End Sub
Remember, there should be about seven inputs. Also,
you must make sure your variable names exactly match the names you
used when you declared them.
When you have finished, click the save
button
to save your work.
Now click the run button
just to see if VB accepts your code. It will complain if you have made a syntax
error, (such as a spelling error, or have misnamed a variable.)
~~Activity~~
Activity A |
At the top of all your forms you should make sure you have the
word..
Option Explicit
This is an instruction to VB to check your code to see if you
have declared all your variables. VB looks through your
code and if you have say spelt one of your variables wrong it
will tell you.
To see
what I mean, take one of your variables like rectLawnLength
and spell it wrong, i.e. rectLawnLen
Now run
your program.
VB will
probably give an error message when you click the submit button because
that's when it finds the mispelt variable.
 |
Now you are getting user input, it would be useful to make
sure your input variables are getting the correct values. There are a
couple of ways of checking this. The first way is to use a temporary
message box. The second way is to step through your code.
Suppose I want to check my rectLawnLength
variable is getting the correct value from the input box. I could use
a message box to check temporarily. When I am satisfied that all is
well I can then delete message box line of code.
At the end of the cmdSubmit sub routine I am going to
type in a line to bring up a message box.
Private
Sub
cmdSubmit_Click()
rectLawnLength = txtRectLawnLength.Text
rectLawnWidth = txtRectLawnWidth.Text
circLawnRadius = txtCircLawnRadius.Text
etc....
MsgBox "The value of rectLawnLength is " &
rectLawnLength
End
Sub
Now when I click the Submit button, a
message box should appear...

I could check other variables if I wish. All I have
to do is change the msgbox line of code a little bit. If I want to
check the value of the rectLawnWidth variable I would change it
to...
MsgBox "The value of rectLawnWidth is " &
rectLawnWidth
I could use a msgbox to check the values for each one of my
variables in turn.
~~Activity~~
Activity B |
Use a message box to check the values of each one of your variables in
turn.
-
Check your variables that store that values from the rectangular
lawn textboxes, i.e.

-
Check your variables that store that values from the circular lawn
textbox, i.e.

-
Check your variables that store that values from the L-shape field
textboxes.
-
Check your variables that store that values from the field with pond
textboxes.
|
There is another way of checking your code. You can
step through it line by line using a breakpoint. You can add a breakpoint to
any line of code by clicking in the margin of your code...

When a line of code has a breakpoint on it, the line
appears red and a red circle appears in the margin. It is easy to
remove the breakpoint, you just click on the red circle and it goes away.
When a line has a breakpoint, this means that when VB is
running your program and it reaches the line it will pause.
This is useful because then you can step slowly through your code one line
at a time and see what values controls and variables have. Here is what
happens when VB reaches my breakpoint when I run the program...

The breakpoint line goes yellow and VB waits for me to do
something. I want to go to the next line and see what value rectLawnLength
has; to do this I press the F8 key.

VB goes to the next line - it highlights the line yellow.
If I hover my mouse over the txtRectLawnLength
part of the code, VB shows me what value is in that textbox. Since I
had typed a number 3 into that textbox this is what it shows me in the
little tooltip...

Now if I hover my mouse over the
rectLawnLength
part of the code, VB shows me what value the variable has...

The variable has a value of 0. This is because VB
has not executed the line yet. As soon as it does execute the line,
rectLawnLength
will have the same value as txtRectLawnLength .
I can execute the line by pressing F8. VB executes the line,
then goes to the next line and pauses. Now if I hover my mouse over the
rectLawnLength , we can see it's value has
changed to 3.

~~Activity~~
Activity C |
Place a breakpoint on your submit buttons sub routine. Then run
you program and press F8 to step through your code. Remember to
hover your mouse over variables to see what values they have.
You can stop running your program at any time by clicking the
button |
Have you finished all the tasks and
handed them in?
If so - well done.
|