You'd use DLookup() and do the math from there as in...
---
Dim YearsToVestiture as Integer
YearsToVestiture = DLookup("YearsRequired",[TableNameHere],"StartDate =>
[Forms]![FormNameHere]![ControlNameHere] and EndDate <=
[Forms]![FormNameHere]![ControlNameHere]")
VestingDate = DateAdd("yyyy", YearsToVestiture,
[Forms]![FormNameHere]![ControlNameHere]")
---
Where [TableNameHere] is the table with the information,
[FormNameHere] is the name of the form you're displaying the information on
(or report) and [ControlNameHere] is the control on the form (or report) that
displays the HireDate
Note: I can never F***ing remember the syntax for (equal to or greater than)
and (equal to or lesser than). You'll probably have to play with that get it
right.
....and now let me introduce you to Boundary analysis & testing...
To ensure that your logic is working correctly, if you're lookup table has
the following records...
StartDate EndDate YearsRequired
10/1/1950 9/30/2005 5
10/1/2005 9/30/2010 10
10/1/2010 9/30/2020 7
You'll want to do a spot check to ensure that the correct values are
returned. Add the following employees with the following hire dates. If the
code is working properly you should get the results that I noted...
Hire Date Name Result
9/30/1949 Emp 1 Invalid - Access should throw an error. The DLookup
can't find a matching
record since none exists.
10/1/1950 Emp 2 5
10/2/1950 Emp 3 5
9/29/2005 Emp 4 5
9/30/2005 Emp 5 5
10/1/2005 Emp 6 10
10/2/2005 Emp 7 10
9/29/2010 Emp 8 10
9/30/2010 Emp 9 10
10/1/2010 Emp 10 7
10/2/2010 Emp 11 7
9/29/2020 Emp 12 7
9/30/2020 Emp 13 7
10/1/2020 Emp 14 Invalid - As in the first case, the DLookup can't
find a
matching record
Boundary analysis is an approach to testing that basically says if the
values along a boundary return the correct results then it is unneccessary to
test the remaining values unless they occur on another boundary. If you have
a threshold of 100 and 99, 100, and 101 return the correct result, then there
is no need to test any value less than 99 or greater than 101 as the result
will be the same. Technically, in your scenario the test could be conducted
with a single record, but given the total number of possibilities testing all
three is no big deal.
Also, if you have a scenario where two records overlap as in
10/1/1950 9/30/2005 5
9/1/2005 9/30/2010 10
The DLookup will return the first value that it encounters.
Afrosheen via AccessMonster.com said:
Thanks Dave for getting back to me so fast.. I set up the table as per your
instructions. Now in order for the vested date to come up would you use the
code:
If [Hire Date] > [EndDate] then
DateAdd("yyyy", [YearsRequired], [Hire Date])
endif
Would this be correct?
BTW, the [HireDate] would be either <> than the [EndDate].
David said:
For the vested dates, I would go with a table to capture the number of years
required as in...
StartDate EndDate YearsRequired
10/1/1901 9/30/2005 5
10/1/2005 9/30/2010 10
10/1/2010 9/30/2020 7
This will allow the flexibility to easily adapt if the terms change again.
For the current period, you'd just need to set an end date far enough into
the future, such as 30 years to allow the look up to continue somewhat
indefinately.
Thanks for all the replies. On the vested date. A couple of years back,
anyone with a hire date before Oct. 1 2006 would have a vested year at 5
[quoted text clipped - 39 lines]
noted above) you want to be able to edit the retirement or vested date
independent of the actual hire date.