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.
Open a new project.
Name the project CarpentryPay.
Name the form frmPay.
Set the Width property to 8400 and the Height property to 5820.
Set the BorderStyle property to 1-Fixed Single and the MinButton property to True.
Set the StartUpPosition property to 2-Center Screen.
Set up the interface below:
Name the title label lblTitle.
Name the labels on the left appropriately as follows:
lblName
lblAddress
lblCity
lblState
lblZip
lblHours
lblRate
lblOvertime
Be sure to set their AutoSize property to True and to set their captions as seen above.
Set the font size of those labels to 10.
Set the access keys of those labels you just placed which have one in the image.
Name the text boxes appropriately as follows:
txtName
txtAddress
txtCity
txtState
txtZip
txtHours
txtRate
Set the Index property of lblOvertime to 0.
Place a label next to lblOvertime and name it lblOvertime.
Set its AutoSize property to True.
Place the labels you see on the right in the image and set the captions for the labels on the right as seen above.
Set each label's Index property to 0.
Name them appropriately as follows:
lblMedicare
lblSocialTax
lblStateTax
lblFedTax
lblGrossPay
lblWithholdings
lblNetPay
Set their captions as seen above.
Place labels next to each label you just named and line them up vertically.
Name them the same as you did with the labels we just named going sown the column. Their Index will automatically become 1 because the name already exists.
Be sure to set the font to bold in the font property for the labels lblGrossPay and lblNetPay.
Name the command buttons from left to right as follows:
cmdCalc
cmdView
cmdClear
cmdExit
Set their captions and access keys as seen above.
Set the Enabled property of the command button cmdView to False.
Set the Default property of the command button cmdCalc to True.
Lock the controls on the form.
Now we can finally code this application.
Open the code window for the command button cmdClear.
Press tab, then type the following lines:
txtName.Text = ""
txtAddress.Text = ""
txtCity.Text = ""
txtState.Text = ""
txtZip.Text = ""
txtHours.Text = ""
txtRate.Text = ""
lblOvertime(1).Caption = ""
lblGrossPay(1).Caption = ""
lblMedicare(1).Caption = ""
lblSocialTax(1).Caption = ""
lblStateTax(1).Caption = ""
lblFedTax(1).Caption = ""
lblWithholdings(1).Caption = ""
lblNetPay(1).Caption = ""
cmdView.Enabled = False
txtName.SetFocus
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.
Select the txtState object in the object box of the code window.
Press tab, then type If Len(txtState.Text) = 2 Then txtZip.SetFocus.
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.
Select the GotFocus event in the event box of the code window for the txtState object.
Press tab, then type the following lines.
txtState.SelStart = 0
txtState.SelLength = Len(txtState.Text)
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.
Select the LostFocus event in the event box of the code window.
Press tab, then type txtState.Text = UCase(txtState.Text).
This line converts the text in the text box txtState to all uppercase characters using the UCase function.
Select the txtZip text box in the object box of the code window.
Press tab, then type If Len(txtZip.Text) = 5 Then txtHours.SetFocus
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.
Select the cmdExit object in the object box of the code window.
Press tab, then type the following lines:
Dim bytResponse As Byte
If frmPayView.Visible = True Then Unload frmPayView
bytResponse = MsgBox("Do you want to exit?", conBtns, "")
If bytResponse = vbYes Then End
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.
Select the Project menu, then choose Add Module.
Name the module modPay.
Type the following lines in the code module:
Public strInfo(4) As String
Public strEmployeePay As String
Public Const conBtns As Byte = vbYesNo + vbQuestion
Public strCity As String, strState As String, strZip As String
Public sngOvertime As Single, sngPayRate As Single, sngGrossPay As Single
Public sngFedTax As Single, sngWithholdings As Single, sngNetPay As Single
Public sngMedicareTax As Single, sngSocialTax As Single, sngStateTax As Single
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.
Select the cmdCalc object in the code window's object box.
Press tab, then type the following lines:
Dim strLength As String
If Left(txtRate.Text, 1) = "$" Then
sngPayRate = Val(Right(txtRate.Text, Len(txtRate.Text) - 1))
Else: sngPayRate = Val(txtRate.Text)
End If
If Val(txtHours.Text) > 40 Then
sngOvertime = Val(txtHours.Text - 40)
sngGrossPay = (40 + (1.5 * sngOvertime)) * sngPayRate
If sngOvertime <> 1 Then
lblOvertime(1).Caption = sngOvertime & " hours"
Else: lblOvertime(1).Caption = sngOvertime & " hour"
End If
Else
sngGrossPay = Val(txtHours.Text) * sngPayRate
lblOvertime(1).Caption = ""
End If
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.
Below the code you just entered, type the following lines:
sngMedicareTax = sngGrossPay * 0.015
sngSocialTax = sngGrossPay * 0.06
sngStateTax = sngGrossPay * 0.04
sngFedTax = sngGrossPay * 0.09
sngWithholdings = sngMedicareTax + sngSocialTax + sngStateTax + sngFedTax
sngNetPay = sngGrossPay - sngWithholdings
strEmployeePay = sngNetPay
txtRate.Text = Format(txtRate.Text, "Currency")
lblGrossPay(1).Caption = Format(sngGrossPay, "Currency")
lblMedicare(1).Caption = Format(sngMedicareTax, "Currency")
lblSocialTax(1).Caption = Format(sngSocialTax, "Currency")
lblStateTax(1).Caption = Format(sngStateTax, "Currency")
lblWithholdings(1).Caption = Format(sngWithholdings, "Currency")
lblFedTax(1).Caption = Format(sngFedTax, "Currency")
lblNetPay(1).Caption = Format(sngNetPay, "Currency")
strLength = txtName.Text + txtAddress.Text + txtCity.Text + txtState.Text + txtZip.Text + txtHours.Text + txtRate.Text
If Len(strLength) > 25 Then
cmdView.Enabled = True
cmdView.SetFocus
Else: cmdView.Enabled = False
End If
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.
Name the label in the upper-left corner of the form lblAddress.
Name the label below it lblPayOrder.
Name the label to the right lblName.
Name the label below them lblPayWords.
Name the label displaying Date lblDate. Set the caption as seen.
Name the label farthest to the right lblPay.
Set all the label's AutoSize properties to True.
Lock the controls on the form.
Set the font size of the labels lblDate, lblPay, and lblPayWords to 10.
Set the font size of lblName to 12.
Now we need to declare a whole bunch of variables.
Open the code window for this form and select General in the code window's object box.
Type the following lines:
Dim strCents As String
Dim strDollars As String, strTens As String
Dim strTeens As String, strTeensAmount As String
Dim strHundreds As String, strThousands As String
Dim strDollarAmount As String, strTensAmount As String
Dim strHundredAmount As String, strThousandAmount As String
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.
Go to the other form (frmPay) and open the code window for the cmdView command button.
Press tab, then type the following lines:
strInfo(0) = txtName.Text
strInfo(1) = txtAddress.Text
strInfo(2) = txtCity.Text
strInfo(3) = txtState.Text
strInfo(4) = txtZip.Text
strEmployeePay = lblNetPay(1).Caption
frmPayView.lblAddress.BackColor = &HFFFFFF
frmPayView.lblName.BackColor = &HFFFFFF
frmPayView.lblPay.BackColor = &HFFFFFF
frmPayView.lblPayWords.BackColor = &HFFFFFF
frmPayView.lblPayOrder.BackColor = &HFFFFFF
frmPayView.lblDate.BackColor = &HFFFFFF
frmPayView.Show
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.
Open the other form (frmPayView) and open the code window for the form's Unload event.
Press tab, then type the following lines:
Dim bytResponse As Byte
bytResponse = MsgBox("Would you like to print this check?", conBtns, "")
If bytResponse = vbYes Then
Printer.Font = "MS Sans Serif"
Printer.FontSize = 12
PrintForm
Printer.Print "Gross Pay:"; Tab(35); Format(sngGrossPay, "Currency")
Printer.Print ""
Printer.Print "Medicare:"; Tab(35); Format(sngMedicareTax, "Currency")
Printer.Print "Social Security:"; Tab(35); Format(sngSocialTax, "Currency")
Printer.Print "State:"; Tab(35); Format(sngStateTax, "Currency")
Printer.Print "Federal:"; Tab(35); Format(sngFedTax, "Currency")
Printer.Print ""
Printer.Print "Total withholdings:"; Tab(35); Format(sngWithholdings, "Currency")
Printer.Print "Net pay:"; Tab(35); Format(sngNetPay, "Currency")
Else: Unload frmPayView
End If
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.