Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Date add 1

Status
Not open for further replies.

wattyl

Technical User
May 1, 2002
14
0
0
GB
I Have three boxes on a form
1. Number of years (not constant)
2, Start Date ("dd"mmm"yy")
3. End Date ("dd"mmm"yy")
The end date should = Start date + box1 The figure in box 1 is always different for each record.ie 12 15 16 20
Can anyone help with the formula for my query to automatically calculate the end date
 
Try This

'***Code Begins

Private Sub Number_of_Years_AfterUpdate()
Dim DayDiff As Integer

DayDiff = DateDiff("d", [Start Date], Format(Day([Start Date]) & "/" & Month([Start Date]) & "/" & [Number of Years], "dd/mm/yy"))
[End Date] = [Start Date] + [DayDiff]
MsgBox [End Date]
End Sub

'***End Of Code

Hope it helps
[yinyang]
 
Oops - try this one. As long as 'Number of Years' does not exceed 27 you'll be fine with this code...

'***Code Begins

Private Sub Number_of_Years_AfterUpdate()
Dim DayDiff As Integer

DayDiff = DateDiff("d", [Start Date], Format(Day([Start Date]) & "/" & Month([Start Date]) & "/" & [Number of Years] + Year([Start Date]), "dd/mm/yy"))
MsgBox [DayDiff]
[End Date] = [Start Date] + [DayDiff]
MsgBox [End Date]
End Sub

'***End Of Code
[yinyang]
 
Having problem running this code.
Is there any way I can do this using a query and placing the code in the "Field Box" of the query
 
Try using this in your query (make sure you have included [Number of Years] and [Start Date] in your query.

If you wanted to store these values to a table then use an update query (to update 'Finish Date') instead of a standard select query.

---
Finish Date: Format(Day([Start Date]) & "/" & Month([Start Date]) & "/" & Year([Start Date])+[Number Of Years],"Medium Date")
---

Cheers
[yinyang]
 
Hi

Surely your code should be

EndDate = DateAdd("y",NoOfYears,StartDate)

This code needs to be executed in the after update event of StartDate and NoOfYears, if it is user input, if it is comming from a recordsource of a bound form, put it in the oncurrent event of the form Regards

Ken Reay
Freelance Solutions Developer
Boldon Information Systems Ltd
Website needs upgrading, but for now - UK
 
Ken,
I have a query and tried to use
"DateAdd("y",NoOfYears,[StartDate])" as you suggested.
I used -3 as the "NumberOfYears" to subtract but this is not subtracting the three years , instead it only gives a subtraction of 3 days on my end date from start date.

The start date is already calculated from an initial date in the same query.
Any suggestions to help appreciated.
Watty
 
Check the help file for the DateAdd Function.

To respond correctly, your function should read:

EndDate = DateAdd("yyyy",NoOfYears,StartDate)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top