VBA - HW2: Selection Logic For Employer 401K Contribution

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

VBA_HW2: Selection Logic for Employer

401K Contribution
In the VBA Introduction Hands-on Practice Exercises, you added VBA code to the About, 401K, Bonus,
Close Form, and Quit buttons. Now you will enhance the code for the 401K button.

• Currently the VBA code for the 401K button calculates and displays the monthly 401K
contribution for an employee.

• You will add code that will include the employer’s contribution in this calculation.

• The employer’s matching contribution is 3% of the employee’s current salary, however for
Senior Project Managers the employer’s matching contribution is 5%.

• You need to add additional VBA code to your existing cmd401K_Click()procedure.


You will use an IF – Then – Else decision structure to determine whether to add a 5%
matching contribution for Senior Project Managers or else add a 3% matching contribution for
all other employees.

1. Open your VBAIntro_LastName.accdb Access database.


Do a Save As . . . and name the database VBA_HW2_LastnameFirstName.accdb

2. Open the form frmEmployee.

3. Open the VB Editor.

4. Place the insertion point after Dim sng401K as Single in the


Sub cmd401K_Click() procedure, press Enter to start a new line.

5. To declare a string memory variable named strTitle , type the following code:

Dim strTitle As String

Place the insertion point after the sng401K = Val(txt401K.Text) / 100


statement, press Enter twice.

6. Type the following VBA code to store the employee’s Job Title into the memory variable strTitle

'Obtain Employee's Job Title


txtTitle.SetFocus
strTitle = txtTitle.Text

Scottsdale Community College, CIS105, Intro to VBA 1


7. Press Enter, next you will add the following VBA decision structure code to determine if the
employee’s Job Title is “Senior Project Manager”. If this is TRUE then the employer’s matching
rate is 5%, ELSE the employer’s matching rate is 3%. The employer’s contribution rate is added
to the employee’s contribution rate.

'Decision Structure to Test employee’s Job Title


If strTitle = "Senior Project Manager" Then
sng401K = sng401K + 0.05
Else
sng401K = sng401K + 0.03
End If

8. Check your VBA code with the code in Figure 1. Make any necessary corrections.

Figure 1

9. Click Save and then close the VB Editor.

10. Test your code for the accuracy of the new 401K contribution calculation.

11. For record #1: Dennis Seinfeld, Associate Consultant his 401K contribution is $373.33.
For record #8: Ben Wilder, Senior Project Manager, his 401K contribution is $730.00.

12. This completes the VBA_HW2 Assignment.

Scottsdale Community College, CIS105, Intro to VBA 2

You might also like