1.            Interacting with the User

Learning Outcomes

On completion of this chapter you will know how to interact with the user by directly supplying information to the programme from the keyboard.

Download pdf version

Introduction

In the programme of Listing 21 there is no interaction between the user and the programme, other than actually running the programme in the first place. Although the programme calculates the values of the gross, tax and net correctly, it only does it when the values of hours and rate are 30 and 8 respectively.

Our programme needs to be more versatile than this.  By this we mean that it must be able to calculate the payroll regardless of the number of hours or how big or small the hourly rate is.  In order to be able to do this the user must be able to directly supply the programme with the values of the hours and rate.

The InputBox Function

Go to top

In Listing 31 below we have a new version of the programme we examined in Listing 21 above.  The structure and the size of the two programmes are the same.  The only difference is in lines 7 and 8.  Here the constants 30 and 8 are replaced by the InputBox function.

When a running programme meets a line with an InputBox function in it, it halts the top to bottom flow of the programme and puts a dialogue box on the screen like that shown in Figure 31 below.  The programme will  wait at that line until the user enters a value in the textbox and clicks on the OK button .

In our example below normal top to bottom flow occurs until the programme reaches line 7.  Here the InputBox function causes the programme to halt and the dialogue box in Figure 31 will appear on the screen.  The dialogue box will stay like this until the user enters the value of the hours into the textbox and clicks the OK button.  Once this occurs, the value entered by the user into the textbox is stored in the variable sngHours.  Thus if the user entered 45 into the textbox and pressed the OK button, this would be the same as if line 7, originally, was:

sngHours = 45

Returning back to examining line 7: once the user has entered value for the hours and clicked on OK, then, as we said, the value entered by the user is stored in the variable sngHours and the programme moves to the next line, i.e. line 8.  Here it encounters another InputBox function.

This version of the InputBox function will control the entry of the rate in the same way as its predecessor controlled the entry of the hours.

Apart from the use of the two InputBox functions there is no other difference between this programme and its predecessor.

Listing 31

1

Sub payroll2()

2

    Dim sngHours As Single

3

    Dim curRate As Currency

4

    Dim curGross As Currency

5

    Dim curTax As Currency

6

    Dim curNet As Currency

7

    sngHours = InputBox("Enter hours worked")

8

    curRate = InputBox("Enter hourly rate")

9

    curGross = sngHours * curRate

10

    curTax = curGross * 0.25

11

    curNet = curGross - curTax

12

    Debug.Print curGross

13

    Debug.Print curTax

14

    Debug.Print curNet

15

End Sub

 

Figure 31

Programme Divisions – Input, Process and Output

Go to top

Let us have another look at Listing 31. Lines 2 – 6 is where the variables are declared.  Apart from those lines the rest of the code i.e. lines 7 – 14 are divided into three logical parts.  Lines 7 and 8 look very similar and perform similar functions – they allow the user to put data into the programme.  This part of the code is called the Input section.

Line 9 uses the values that were input in lines 7 and 8 in order to calculate the value of the gross. Line 10 uses the result of line 9 in order to calculate the tax.  Line 11 uses the results of lines 9 and 10 in order to calculate the net.  Thus lines 9, 10 and 11 process the data that was inputted at lines 7 and 8.  For this reason lines 9, 10 and 11 are referred to as the Process section of the programme.

Lines 12, 13 and 14 print the result of the calculation, or in other words the result of the processing out on to the Immediate Window.  For this reason those 3 lines are referred to as the Output section of the programme.

Every programme, whether large or small is divided into the same three sections.  In large complex programmes the input section itself may be divided into its own Input, Process and Output sections.  The same applies to the Process and Output sections.

Practice

Copy the code of Listing 31into your programme area and run it a number of times – each time with a different set of values for hours and rate.  Each time check that the correct values for gross tax and net are printed out.

Exercise 3

Go to top

 

1.      What is the shortcoming of the programme shown in Listing 21.

2.      Explain the use of the InputBox function.

3.      All programmes are divided into three components.  Name the three components.

4.      Discuss what occurs in the three components mentioned above.

Asssignment Part 2

Go to top

Modify the programme that you have created in Assignment Part 1 so that it will use the InputBox function to ask the user directly to enter values for the unit price and the amount sold.  The calculation of the subtotal, GST and total are to be left as they were before.


5.