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.
Open the form's Load event for the check view form.
Press tab, then type the following lines.
strTens = ""
strTeens = ""
strDollars = ""
strHundreds = ""
strThousands = ""
strDollarAmount = ""
strTensAmount = ""
strTeensAmount = ""
strHundredAmount = ""
strThousandAmount = ""
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.
Type the following lines below the lines you just entered:
lblDate.Caption = "Date: " & Format(Date$,
"m/d/yy")
lblAddress.Caption = "Joe Shmoe Carpentry" & vbNewLine &
"123 My Road" & vbNewLine & "MyVille, MA
12345-8502"
lblPayOrder.Caption = "Pay to the" & vbNewLine &
"order of"
lblName.Caption = strInfo(0) & vbNewLine & strInfo(1) &
vbNewLine & strInfo(2) & ", " & strInfo(3) &
" " & strInfo(4)
lblPay.Caption = Format(strEmployeePay, "Currency")
strCents = Right(strEmployeePay, 2)
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.
Type the following lines below the code you just entered:
1 Select Case Len(strEmployeePay)
2 Case 5
3 strDollars = Mid(strEmployeePay, 2, 1)
4 Case 6
5 If Val(Mid(strEmployeePay, 2,
2)) > 10 And Val(Mid(strEmployeePay, 2, 2)) < 20 Then
6 strTeens
= Mid(strEmployeePay, 2, 2)
7 Else
8 strTens
= Mid(strEmployeePay, 2, 1)
9
strDollars = Mid(strEmployeePay, 3, 1)
10 End If
11 Case 7
12 strHundreds =
Mid(strEmployeePay, 2, 1)
13 If Val(Mid(strEmployeePay, 3,
1)) = 1 And Val(Mid(strEmployeePay, 4, 1)) > 0 Then
14
strTeens = Mid(strEmployeePay, 3, 2)
15 Else
16 strTens
= Mid(strEmployeePay, 3, 1)
17
strTeens = ""
18
strDollars = Mid(strEmployeePay, 4, 1)
19 End If
20 Case 9
21 strThousands =
Mid(strEmployeePay, 2, 1)
22 strHundreds =
Mid(strEmployeePay, 4, 1)
23 If Val(Mid(strEmployeePay, 5,
1)) = 1 And Val(Mid(strEmployeePay, 6, 1)) > 0 Then
24
strTeens = Mid(strEmployeePay, 5, 2)
25 Else
26
strTeens = ""
27 strTens
= Mid(strEmployeePay, 5, 1)
28
strDollars = Mid(strEmployeePay, 6, 1)
29 End If
30 End Select
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.
Enter the following code below the lines you just finished:
Select Case strDollars
Case 0
strDollarAmount = ""
Case 1
strDollarAmount = "One "
Case 2
strDollarAmount = "Two "
Case 3
strDollarAmount = "Three "
Case 4
strDollarAmount = "Four "
Case 5
strDollarAmount = "Five "
Case 6
strDollarAmount = "Six "
Case 7
strDollarAmount = "Seven "
Case 8
strDollarAmount = "Eight "
Case 9
strDollarAmount = "Nine "
End Select
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).
Below the lines you just entered, enter the following lines:
Select Case strTens
Case 0
strTensAmount = ""
Case 1
strTensAmount = "Ten"
Case 2
strTensAmount = "Twenty "
Case 3
strTensAmount = "Thirty "
Case 4
strTensAmount = "Fourty "
Case 5
strTensAmount = "Fifty "
Case 6
strTensAmount = "Sixty "
Case 7
strTensAmount = "Seventy "
Case 8
strTensAmount = "Eighty "
Case 9
strTensAmount = "Ninety "
End Select
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.
Select Case strTeens
Case 11
strTeensAmount = "Eleven "
Case 12
strTeensAmount = "Twelve "
Case 13
strTeensAmount = "Thirteen "
Case 14
strTeensAmount = "Fourteen "
Case 15
strTeensAmount = "Fifteen "
Case 16
strTeensAmount = "Sixteen "
Case 17
strTeensAmount = "Seventeen "
Case 18
strTeensAmount = "Eighteen "
Case 19
strTeensAmount = "Nineteen "
Case Else
strTeensAmount = ""
End Select
This code does the same thing for strTeens, which was determined from looking in the string earlier.
Select Case strHundreds
Case 0
strHundredAmount = ""
Case 1
strHundredAmount = "One Hundred "
Case 2
strHundredAmount = "Two Hundred "
Case 3
strHundredAmount = "Three Hundred "
Case 4
strHundredAmount = "Four Hundred "
Case 5
strHundredAmount = "Five Hundred "
Case 6
strHundredAmount = "Six Hundred "
Case 7
strHundredAmount = "Seven Hundred "
Case 8
strHundredAmount = "Eight Hundred "
Case 9
strHundredAmount = "Nine Hundred "
End Select
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.
Select Case strThousands
Case ""
strThousandAmount = ""
Case 1
strThousandAmount = "One Thousand "
Case 2
strThousandAmount = "Two Thousand "
Case 3
strThousandAmount = "Three Thousand "
Case 4
strThousandAmount = "Four Thousand "
Case 5
strThousandAmount = "Five Thousand "
Case 6
strThousandAmount = "Six Thousand "
Case 7
strThousandAmount = "Seven Thousand "
Case 8
strThousandAmount = "Eight Thousand "
Case 9
strThousandAmount = "Nine Thousand "
End Select
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.
lblPayWords.Caption = strThousandAmount & strHundredAmount _
& strTeensAmount & strTensAmount & strDollarAmount & "and " & strCents & "/100"
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.