Sylvia Fergusson
09-22-2001, 03:40 PM
I have three fields - one named visits/year,
maint due and next maint due.
I would like to have the next maint due field fill in automatically but this would depend on how many visits/year. If one visit/year then maint due would be 1 year after maint due. If 2 visits/year then 6 months after maint due etc etc.
Can anyone help with this.
jwindon
09-22-2001, 03:53 PM
You could do this through an IF-Then statement that activates on the AfterUpdate control of visits/year on a form.
Private Sub Name_AfterUpdate()
If Me.visits/year = 1 Then
Me.MaintDue = date() + 365
Me.NextMaintDue = date() +730
Else
If Me.visits/year =2 Then
Me.MainDue = date() + 183
Me.NextMaintDue = date() +365
DoCmd.RunCommand acCmdRefresh
End If
End Sub
You didn't mention the date part of the equation so I am assuming you wanted a date based on the time the record is entered.
R. Hicks
09-22-2001, 05:01 PM
If I got this right .....
You enter "Vists per Year" and the date of the first "Maintenance Due" then you want the "Next Maintenance Due" to be calculated.
Is that correct??????
If it is then place the following as the Control Source of "NextMaintDue".
=DateAdd("m",(12/nz([Visits/Year],1)),[MaintDue])
Word of warning ......
You should not use the forward slash (/) in the naming of your fields/controls. Access can get confused in thinking that the "/" is the divide operator.
HTH
RDH
[This message has been edited by R. Hicks (edited 09-22-2001).]
jwindon
09-22-2001, 05:06 PM
Ooops Rich! Totally overlooked the storing of a calculated control. Guilty as charged.
I did use that Code I offered before to calculate and save a review date value.
R. Hicks
09-22-2001, 05:11 PM
There are certain special occasions where it is allowable or absolutely necessary to store a calculated value. But this should be a last resort only.
RDH
Pat Hartman
09-22-2001, 05:44 PM
Rather than storing the next two maintenance dates, it is better practice to store the most recently completed maintenance date and then calculate the other two dates as you need them. The following are two options for accomplishing this task. The first is much simpler since it does not require writing any VBA code. But, it does require changing the data you store.
1. Rather than storing a number indicating the number of times that maintenance is performed during a year, store the number of months or weeks or days (pick one and use that interval for everything) between maintenance cycles. Then you can simply use the DateAdd() function to calculate each date as needed -
DateAdd("m",YourInterval,LastMaintDate) As MaintDue
or
DateAdd("m",YourInterval * 2,LastMaintDate) As NextMaintDue
2. Create two public functions in a standard code module. One to calculate each date. Then in a query you would use -
FuncMaintDue(TimesPerYear, LastMaintDate) As MaintDue
or
FuncNextMaintDue(TimesPerYear, LastMaintDate) As NextMaintDue