Visual Basic Programming

Page 23

Before you continue this application, you may want to test it out to make sure that it at least calculates properly and is error-free. If it is not, I wish the best of luck to you trying to debug it. It should work fine because I copy and paste everything from the code window to the web document. Anyway, let us continue. Now we are going to type a whole load of code to test and manipulate strings. The first thing we need to do is reset all the variables in the form's load event. All the code for the check view is going into the form's load event.

This code simply resets all the variables that will be written to below in a moment so that we do not see extra words on the check. To understand this: Suppose the variable strHundredAmount stores the text Five Hundred. If the user enters new values for a different employee after they have already done another employee. The words will still exist in the variable unless the variable is written over. If an employee makes two hundred dollars, and another employee makes seventy dollars, the tens will change as will soon be instructed, but the hundreds will remain the same until they are written over or cleared in the code. Same goes for all other variables above.

Line 1: Set the caption of the label lblDate to Date: (what it is now) and the current date, formatted as 1/1/02.
Line 2: Set the caption of the label lblAddress to the address of Joe Shmoe Carpentry. The vbNewLine words put a carriage return in the label so that we may use one label for multiple lines.
Line 3: Set the caption of the label lblPayOrder to Pay to the plus a new line and order of.
Line 4: Set the caption of the label lblName to the text in the array element strInfo(0) (stores the name), plus the text in the array element strInfo(1) (stores the address), plus the text in the array element strInfo(2) (stores the city), plus the text in the array element strInfo(3) (stores the state), plus the text in the array element strInfo(4) (stores the zip code).
Line 5: Set the caption of the label lblPay to the value of the variable strEmployeePay, formatted as Currency.
Line 6: Store the rightmost two characters from the string strEmployeePay in the variable strCents.

Now for a whole ton of code to finish off this application. Now we are going to manipulate the string variable strEmployeePay, which stores the value of the employees pay. We stored this in the sub-procedure for the command button cmdView on the form frmPay. The first thing we need to do is test the length of the string that stores the pay value, which is strEmployeePay.

The numbers on the left are just the line numbers to help you understand the explanations below easier because you need to refer to the line number.

Line 1: Begin Select Case selection for the length of the string variable strEmployeePay.
Line 2: When the length of strEmployeePay is 5 (Case 5), do line 3 (Length of 5 looks like $1.25; dollar sign, one, decimal, two, five --> FIVE CHARACTERS!).
Line 3: Set the variable strDollars to 2 characters in from the left in the string strEmployeePay, and get 1 character. Those numbers come from the only numbers you see on line 3.
Line 4: When the length of strEmployeePay is 6 (Case 6), do lines 5 through 10.
Line 5: Test to see if the value of two characters in from the left looking at two are greater than ten and less than twenty (values from 11 to 19).
Line 6: Set the variable strTeens to two characters in from the left and look at two, then store those in the variable strTeens. The number $19.25 has a length of 6. Looking at two characters in from the left and looking at two from that point is 19.
Line 8: Set the variable strTens to two characters in from the left and get one when the numbers in the teens places are not teens.
Line 9: Set the variable strDollars equal to the third character from the left and look at one when the numbers in the teens places are not teens.
Line 11: Do statements below (lines 12 through 19) when the length of the string is 7 (Case 7).
Line 12: Set the variable strHundreds to two characters in from the left of strEmployeePay and retrieve one character.
Line 13: Test the value of the third and fourth character from the left of strEmployeePay; when the third from the left is a one and the fourth from the left is greater than zero, execute line 14. Take this value for example: $119.45. There are seven characters. The character at Mid 3, 1 is 1, and the character at Mid 4, 1 is 9. The line tests for teens so that it knows when to display a teens amount as opposed to twenty through ninety.
Line 14: Set the string variable equal to two characters three in from the left (get the teens).
Line 15: When there are no teens, execute lines 16 through 18.
Line 16: Set the string variable strTens equal to the third character from the left and look at one character.
Line 17: Clear the variable strTeens.
Line 18: Set the variable strDollars equal to the fourth character from the left and look at one character.
Line 20: When the length is nine (thousands now --> $1,295.00 has nine characters. Let us just assume that no one will make above $9,999.99!)
Line 21: Set the variable strThousands to two characters in from the left and look at one. $1,932.00 --> The character at Mid 2, 1 is 1.
Line 22: Set the variable strHundreds to four characters in from the left and look at one. Third character is the comma; fourth is the tens place.
Lines 23 and 24 test for teens again. I am sure you can understand how it looks at it by now.
Line 27: When the If statement is false (there are no teens in the string), set the variable strTens equal to five characters in from the left and look at one. Take the value $1,237.25. The 3 is at position 5, 1, and it is in the tens place.
Line 28: Set the variable strDollars to the sixth character from the left and look at one. Use the example I provided in line 27 if you are a bit confused, using the values 6, 1.

Now that we have search every possible combination, we need to test the results of each variable that we set a character in the pay amount to. We will do this by using Select Case again for the each variable and use every possible value of that number (0 through 9; 11 through 19 for teens). We will start with dollars.

Line 1: Begin selection of the variable strDollars, which was set to a value in the string manipulation above.
Line 3: When strDollars is 0, set strDollarAmount to null (""). When this value is zero, we do not want to see words written out in the label, just as we do not with a regular check.
Line 5: When strDollars is 1, set the variable strDollarAmount to One. This would be the word we would write on a check with a 1 in the dollars place, so we set the word to One.

I am sure the rest is obvious. It just tests what text is in the variable strDollars determined from manipulating it above, then set the word appropriately (2 is Two; 3 is Three etcetera).

This code selects the values of strTens, manipulated earlier, and sets the word for the check appropriately. When a 0 is in the tens place, clear strTensAmount. When a 3 is in the tens place, set the word in strTensAmount to Thirty, the same way we write checks.

This code does the same thing for strTeens, which was determined from looking in the string earlier.

This code does the same thing for strHundreds as we did in the other Select Case structures, which was determined from looking in the string earlier.

I am sure you realize this does the same as the others with a different variable (strThousands).

Now that we have determined what to write for each character in the pay amount, we need to display it.

This simply displays the words that we determined through all the select case structures and displays them in the label lblPayWords. Test the application if you would like to confirm that it works properly (I hope it works for you after all that work!). You could even compile it if you would like!

Now you may move on to Page 24.

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