Visual Basic Programming

Page 22

This tutorial is probably my favorite, so I hope you enjoy it to. It also includes the most amount of tedious coding in the entire tutorial. We are going to finish the string manipulation techniques here. This includes a slightly different way of manipulating strings, and a straight forward way of doing so. We are going to create an Employee Pay calculator for Joe Shmoe's Carpentry. The string manipulation involves writing the words on a check. You will see. Now let us begin.

Be sure to set their AutoSize property to True and to set their captions as seen above.

Now we can finally code this application.

Lines 1 - 7 clear the text boxes.
Lines 8 - 15 clear the labels that will display the actual figures.
Line 16: Disable the command button cmdView.
Line 17: Set the focus to (send the cursor to) the text box txtName.

This line tells the program to set the focus to the text box txtZip (the next text box) when the length of the text in the text box txtState is 2. This does not let the user put more than two letters into the text box for the state.

Line 1: Set the selection start in the text box txtState to 0 (the position before the first character.
Line 2: Set the length of the selection in the text box txtState to the length of the text in it, thus highlighting all the text in it.

This line converts the text in the text box txtState to all uppercase characters using the UCase function.

This line tells the program to set the focus to the text box txtHours if there are five characters in the text box txtZip when the user changes the text.

Line 1: Declare the variable bytResponse as a byte to store the users selection in a message box.
Line 2: Test to see if the form frmPayView is visible; If it is, unload it. We will add this form shortly.
Line 3: Display a message box to store the users selection in the variable bytResponse. We will declare conBtns shortly.
Line 4: When the user selects Yes, end the program.

Now we are going to code the sub-procedure for the cmdCalc option button. But first, we need to declare the variables that are going to be used in that procedure. They are going to be global so that the form that displays the check knows the values.

Line 1: Declare global variable strInfo as a string array (the four in the parenthesis makes it an array; gives the variable five dimensions because it includes 0), which will store the residence information on the pay form so that we may display it on the check.
Line 2: Declare global variable strEmployeePay as a string, which will store the net pay for the employee so that the check form may display it.
Line 3: Declare global constant conBtns as a byte in the form of Yes and No and a question.
Line 4: Declare global variable strCity as a string to store the employee's city in order to display it on the check. Declare global variable strState as a string to store the state the employee lives in. Declare global variable strZip to store the zip code of the employee.
Line 5: Declare global variable sngOvertime to store the extra hours the employee works. This is printed on a printout later. Declare global variable sngPayRate to store the hourly pay amount. Declare global variable sngGrossPay to store the full amount of pay before taxes.
Lines 6 and 7 store the amount of money withheld by taxes --> sngWithholdings stores the total of the other five variables.

Line 1: Declare local variable strLength as a string.
Line 2: Test to see if the leftmost one character in the text box txtRate is a dollar sign.
Line 3: When the leftmost character in the text box txtRate is a dollar sign, set the variable sngPayRate equal to the value of the rightmost characters in the string (the text in the text box) txtRate, and look at the right most Length of the string minus one. The Len function returns a number value of how many characters are in the text box txtRate. Suppose the length is 6. When the first character on the left is a dollar sign (tested in line 2), sngPayRate will be equal to the right most six characters because the length is six, then subtract one from the length of the string txtRate, thus equaling five.
Line 4: Otherwise (the leftmost one character in the text box txtRate is NOT a dollar sign), set sngPayRate equal to the value of the text in txtRate.
Line 6: Test to see if the value of the text in txtHours is greater than forty.
Line 7: When the hours are greater than forty, set the variable sngOvertime equal to the value of the text in the text box txtHours minus 40.
Line 8: Set the value of the variable sngGrossPay equal to 40 plus the product of 1.5 times sngOvertime (time and a half), then multiply the whole thing by sngPayRate.
Line 9: Test the value of the variable sngOvertime.
Line 10: When the value of the variable sngOvertime is not equal to one, set the caption of the label lblOvertime(1) to the value of the variable sngOvertime and " hours".
Line 11: Otherwise (the value of sngOvertime is one), set the caption of the label lblOvertime(1) to the value of the variable sngOvertime and      " hour."
Line 13: Otherwise (value of txtHours.Text is not greater than 40)
Line 14: Set the variable sngGrossPay to the value of the text in the text box times sngPayRate.
Line 15: Clear the text in the caption of the label lblOvertime(1) in case there was some text in the label previously.

Line 1: Determine Medicare tax withholding; multiply the gross pay (sngGrossPay variable) by one and a half percent (0.015).
Line 2: Determine Social Security tax withholding; multiply the gross pay by six percent.
Line 3: Determine state tax withholding; multiply gross pay by four percent.
Line 4: Determine federal tax withholding; multiply gross pay by nine percent.
Line 5: Add the totals of all the withholdings calculated above and store them in the variable sngWithholdings.
Line 6: Determine the net pay (the employee's actual pay) --> subtract the withholdings from the actual money they earned and so gracefully gave to the government (don't you just love seeing all this money go away?)
Line 7: Store the value of the variable sngNetPay in the variable strEmployeePay, which will be used to manipulate to display the check.
Line 8: Format the text in the text box txtRate as dollars and cents (currency).
Lines 9 through 15 format the variables above as Currency (dollars and cents).
Line 16: Store the text in all the text boxes in the variable strLength.
Line 17: Test the length of strLength.
Line 18: When the length is greater than 25, enable the command button cmdView, then set the focus to that command button on Line 19.
Line 20: When the length of the variable strLength is less than 25, disable the command button cmdView.

Now we need to declare a whole bunch of variables.

Line 1: Declare variable strCents as a String, which will store the cents from the total amount.
Line 2: Declare variable strDollars as a String, which will store the dollars from the total amount. Declare variable strTens as a String, which will store the tens place in the total amount.
Line 3: Declare variable strTeens as a String, which will store the teens amount from the total amount. Declare variable strTeensAmount as a String, which will store the teens amount in words from the total amount.
Line 4: Declare variable strHundreds as a String, which will store the hundred value from the total amount. Declare variable strThousands as a String, which will store the thousand value from the total amount.
Line 5: Declare variable strDollarAmount as a String, which will store the dollars in words from the total amount. Declare variable strTensAmount as a String, which will store the tens in words from the total amount.
Line 6: Declare variable strHundredAmount as a String, which will store the hundred amount in words from the total amount. Declare variable strThousandAmount as a String, which will store the thousand amount in words from the total amount.

Lines 1 through 5: Store the data in the text boxes you see in the code in an array element (0 - 4) so that the other form may display the employee's information in the label on the check.
Line 6: Store the final pay (net pay) in the global variable (declared in the code module) strEnployeePay so that we may display it on the check.
Lines 7 through 12: Set the BackColor of the labels on the check view form to white (&HFFFFFF) so that they appear more attractive (otherwise, they will appear gray on white). If we set the labels' color to white initially, it is very difficult to see them, so we do it in coding.
Line 13: Display the form frmPayView.

Line 1 and 2: Declare variable bytResponse as a byte, then display a message box asking the user if they would like to print the check.
Line 3: Test to see if the user clicked Yes. When they do, execute lines four through fifteen.
Line 4: Set the font of the printer to MS Sans Serif.
Line 5: Set the font size of the printer to 12.
Line 6: Print the form.
Lines 7 through 15 will not execute unless you end the entire program. I do not know why exactly though. This code tells the printer to print what you see typed after Printer.Print. For instance, Line 7 prints Gross Pay to the printer, then goes to tab space 35 and prints the value of the variable sngGrossPay formatted as Currency (dollars and cents). Line 8 prints a blank line below it to create a space. Line 9 does the same as Line 7, but with a different variable (sngMedicareTax).
Line 16: Unload the form when the user selects No on the message box.

Now be prepared to type a whole lot more code for this application. We will finish on Page 23.

Top of Page

Page 1 | Page 2 | Page 3 | Page 4 | Page 5 | Page 6 | Page 7 | Page 8 | Page 9 | Page 10 | Page 11

Page 12 | Page 13 | Page 14 | Page 15 | Page 16 | Page 17 | Page 18 | Page 19 | Page 20

Page 21 | Page 22 | Page 23 | Page 24 | Page 25 | Page 26 | Page 27 | Page 28 | Page 29 | Page 30