Visual Basic
Assignment #3
due April 21, 1999
This page has been visited
times since April 8, 1999.
What new topics are covered in this assignment?
Description of Assignment #3
The purpose of assignment #3 is to calculate an employee's net pay from the gross pay. The program will read the employee's name, gross pay, and whether or not the employee is taxable, i.e. if the employee must pay tax. This information is then used to calculate the net pay. The tax rate is 35% for taxable employees, i.e. the net pay will equal 65% of the gross pay. For non-taxable employees, the tax will be 0, thus the net pay will equal the gross pay.
The program will use the InputBox( ) function to input the employee's name and gross pay. It will use a MsgBox( ) function to read whether or not the employee is taxable.
The program uses an If/Else construct to calculate the netpay depending on whether the employee is taxable or not. It also uses an If/Else construct to cancel the calculation procedure if the user presses cancel in either InputBox or if the user enters a non-numeric data for gross pay.
The program will use the Chr(13) statement to create a multi-line string, which will be output to a single label to show the results.
Assignment #3 Notes
The assignment can basically be subdivided into 8 sections.
1. Declare the variables:
2. Read the name using the InputBox( ) function. This function is used as follows:
Dim instring As String
instring = InputBox("Enter a
string:","Title") where instring will store the
string typed by the user, the first string in the brackets is the
prompt in the InputBox, and the second string shows up as the
title of the InputBox. The following is a picture of the InputBox
the above line would create:

If the user clicks OK without typing any information, clicks Cancel, or clicks the X at the top right of the box, an empty string will be put in the instring. You should handle these types of situations in your programs by detecting this condition, and then executing special code to handle the error. In our case we will simply terminate the procedure if the user does not enter a name. The following is an example of how this can be done. You put this statement immediately after the call to InputBox. Then if the user does not input anything the procedure will stop.
if instring = "" then Exit Sub
3. Read the gross pay using an the InputBox( ) function:
This case is a little more complicated than reading the name, since gross pay will be a real number and not a string. Thus we first read the value into a temporary string, then if this string actually contains a number, we will convert it to a real number and store it in the gross pay variable. We still use the same two statements as when we input strings, i.e.
tempstring = InputBox("Enter gross
pay","Gross Pay")
if instring = "" then Exit Sub
However, we must now make sure that tempstring contains a number before converting it. If is does not we will print a message to the user using MsgBox and terminate the procedure. The If/Else statement is used to do this, i.e. if the temporary string contains a number, then convert it, otherwise, print a message and exit the procedure.
For example:
If IsNumeric(tempstring) Then
grosspay = CDbl(tempstring)
Else
ans = MsgBox("Grosspay must be numeric",
vbOKOnly + vbCritical,"Invalid Data")
Exit Sub
End If
Thus if tempstring is a numeric value, then we convert it to a real number and store it in the variable called grosspay. Otherwise we print a message stating that the value must be numeric using the MsgBox( ) function, and we then exit the procedure using Exit Sub.
In this case, 'ans' is an integer variable. Even though we will not be using the value in this case, it is required when we call the MsgBox( ) function. The above MsgBox( ) call produces the following message:

Notice the title and the message. The vbOKOnly produces the OK button. The vbCritical produces the large X. You are allowed one symbol and one combination of buttons in a MessageBox. These are explained on pages 101 - 104 in the text. The return value which will be stored in 'ans 'is also explained there.
4. Use a MessageBox to ask the user if the employee is taxable:
In this case we will be using a YesNo combination and a question mark in the MessageBox. Use pages 101-104 in the text to help you do this.
5. Calculate the tax:
The return value in 'ans' will be needed this time to determine whether the user clicked on Yes or No. If the user clicks Yes, then variable 'ans' will equal the constant vbYes, otherwise it will equal vbNo (see page 104 in the text for further explanation). If the employee is taxable, then the tax is calculated as 35% of the grosspay. Otherwise the tax is 0. This code will start like this:
If ans = vbYes then
...
Else
...
End If
The tax number should be rounded
to 2 decimal places.
Check out this link for information on rounding numbers in VB6 and VB5.
6. Calculate net pay as gross pay minus tax rounded to 2 decimal places.
7. Create the string for output in the label. This is done using the following logic.
outstring = "String 1: "
+ str1 + Chr(13)
outstring = outstring + "String 2: " + str2 + Chr(13)
The second line can be repeated to create as many lines as required in the string. Remember that you will need to convert numeric values to strings using the CStr function. i.e. str2 = CStr(num1)
8. Copy the output string to the label using: label1.caption = outstring
Other notes:
This information should be enough to enable you to do the assignment. You should also read the pertinent parts of the textbook for explanation of the options not covered here. If you have any questions please email me.
As with assignments 1 and 2, the executable of this assignment can be downloaded below.
Good luck.
Below are the assignment 3 downloads:
If you have Visual Basic 6 installed on your computer, download the Assignment #3 file only.
If you do not have Visual Basic 6 installed on your computer, you will need the Visual Basic 6 .dll files in addition to the assignment #3 file. The .dll download below is the same one used for assignments 1 and 2, so if you already downloaded it, you will not need to download it again. Simply use the same files. Otherwise download both the Assignment #3 file and the Assignment 3 required .dll files below. To run, place all files in the same directory and double-click on payroll.exe.
Assignment #3 file (3,000 bytes)
Assignment 3 required .dll files (1.3 million bytes)
Exercise #3 (4,000 bytes)
Get more explanation on the Combining Strings
To e-mail me click here
Page created and maintained by Ron Patterson