PDA

View Full Version : [SOLVED:] Automatic Dates



BarkersIT
03-15-2007, 03:49 AM
Hello,

I have a table and two of the fields are "Purhcase date" and "Warranty Expires"

Basically all warranties expire 3 years from the date of purchase. What can I add to the table so that when a user inputs the purchase date the warranty expires field is automatically filled in as 3 years from then?

I am aware of the Date() and also DatePart("y") to get the year but I dont know where to add the expression.

Any help much appreciated.

OBP
03-15-2007, 05:36 AM
Hello again, it is not normal or even possible to do this at "Table" level, it can be done using a Query, but it is most usual to do it at Form level when the data is first entered.
When the first date is entered it is easiest to "update" the Warranty Expires field with some simple VB code placed in the "After Update" event procedure of the Purchase Date field.
Have a look at the DateAdd function in the Visual Basic Editor's Help, which by the way is totally different from the normal Access Help.


The query method is good for "Updating" records that already exist where you have the Purchase dates but not the Warranty dates.

BarkersIT
03-15-2007, 05:39 AM
Lol, yeah same database.

So write a VBA routine and then call this from the After Update property of the Purhcase date text field?

OBP
03-15-2007, 05:50 AM
Did you see the "amendment" that I made to my previous post about the DateAdd function.
With your form in design mode click on the Purchase date field and in it's Properties click the "Events" tab.
Click on the "After Update" event line and from the drop down list select "Event Procedure".
3 small dots will appear at the right hand side of the "After Update" event line, click on these and it will take you to the code for it.

BarkersIT
03-15-2007, 06:01 AM
Cheers OBP,

Would the procedure I write be a Function that returns a Date?

OBP
03-15-2007, 06:35 AM
No, you can just use the DateAdd function to add 3 years to the Purchase date like this.
me.Warrantydate = DateAdd("yyyy", 3, me.PurchaseDate)

BarkersIT
03-15-2007, 06:45 AM
Worked like a charm. Thanks alot. If it wasnt for forums like this and people like yourself willing to help I wouldnt have a chance. Cheers.