Adding a year(s) to a specific date

R

Rene

G'day
I have 3 fields on a form
1 From [date]
2 To [date]
3 Years [number of years]

The "to" date, depends on the number of years entered in
the "years" field

What code can I use in the after_update property of "years"

so that when I enter a number in "years" it automatically
updates the "to" field to the correct number of years
added to the "from" field.

ie: From 01/01/2004 enter "years" ie: 3(but needs to be
used for any number)and automatically the "To" date will
show 01/01/2007

Much obliged
 
G

Graham R Seach

Rene,

Use the DateDiff function.
=DateDiff("yyyy", Int(Me("Years")), Me("From"))

Then in the AfterUpdate event of both the From and Years textboxes, add the
following code:
Me("To").Requery

Normally, I'd use this syntax, Me!MyTextBox, but you've used reserved
keywords (From, To, and Years), so to avoid any problems, I used the
Me("MyTextBox") syntax. I'd recommend renaming your controls.

Regards,
Graham R Seach
Microsoft Access MVP
Sydney, Australia
 
R

rene

Thank you Graham, much obliged for you taken the time and
effort.....where do I put the fuction?

I put it in the control source of "to"..but it comes up
with #name, when I open the form
-----Original Message-----
Rene,

Use the DateDiff function.
=DateDiff("yyyy", Int(Me("Years")), Me("From"))

Then in the AfterUpdate event of both the From and Years textboxes, add the
following code:
Me("To").Requery

Normally, I'd use this syntax, Me!MyTextBox, but you've used reserved
keywords (From, To, and Years), so to avoid any problems, I used the
Me("MyTextBox") syntax. I'd recommend renaming your controls.

Regards,
Graham R Seach
Microsoft Access MVP
Sydney, Australia
---------------------------

G'day
I have 3 fields on a form
1 From [date]
2 To [date]
3 Years [number of years]

The "to" date, depends on the number of years entered in
the "years" field

What code can I use in the after_update property of "years"

so that when I enter a number in "years" it automatically
updates the "to" field to the correct number of years
added to the "from" field.

ie: From 01/01/2004 enter "years" ie: 3(but needs to be
used for any number)and automatically the "To" date will
show 01/01/2007

Much obliged


.
 
R

Rene

Thanks Graham, found something wich I used and worked and
put it in the after_update property of years.
[To] = DateAdd("yyyy", [Years], [From])

But took notice and will use your Me!MyTextBox..as off
now :)
-----Original Message-----
Rene,

Use the DateDiff function.
=DateDiff("yyyy", Int(Me("Years")), Me("From"))

Then in the AfterUpdate event of both the From and Years textboxes, add the
following code:
Me("To").Requery

Normally, I'd use this syntax, Me!MyTextBox, but you've used reserved
keywords (From, To, and Years), so to avoid any problems, I used the
Me("MyTextBox") syntax. I'd recommend renaming your controls.

Regards,
Graham R Seach
Microsoft Access MVP
Sydney, Australia
---------------------------

G'day
I have 3 fields on a form
1 From [date]
2 To [date]
3 Years [number of years]

The "to" date, depends on the number of years entered in
the "years" field

What code can I use in the after_update property of "years"

so that when I enter a number in "years" it automatically
updates the "to" field to the correct number of years
added to the "from" field.

ie: From 01/01/2004 enter "years" ie: 3(but needs to be
used for any number)and automatically the "To" date will
show 01/01/2007

Much obliged


.
 
G

Graham R Seach

Rene,

I meant for you to put it in the [To] textbox's ControlSource property. You
might say, "the ControlSource? But if I put it there, I won't be able to
save the value to the table!"

My answer is that that's correct. Because you can calculate the [To] value
any time you want, with negligible performance effect, so there's no need to
store it. It is standard practice to NOT store calculated values.

Regards,
Graham R Seach
Microsoft Access MVP
Sydney, Australia

Microsoft Access 2003 VBA Programmer's Reference
http://www.wiley.com/WileyCDA/WileyTitle/productCd-0764559036.html


Rene said:
Thanks Graham, found something wich I used and worked and
put it in the after_update property of years.
[To] = DateAdd("yyyy", [Years], [From])

But took notice and will use your Me!MyTextBox..as off
now :)
-----Original Message-----
Rene,

Use the DateDiff function.
=DateDiff("yyyy", Int(Me("Years")), Me("From"))

Then in the AfterUpdate event of both the From and Years textboxes, add the
following code:
Me("To").Requery

Normally, I'd use this syntax, Me!MyTextBox, but you've used reserved
keywords (From, To, and Years), so to avoid any problems, I used the
Me("MyTextBox") syntax. I'd recommend renaming your controls.

Regards,
Graham R Seach
Microsoft Access MVP
Sydney, Australia
---------------------------

G'day
I have 3 fields on a form
1 From [date]
2 To [date]
3 Years [number of years]

The "to" date, depends on the number of years entered in
the "years" field

What code can I use in the after_update property of "years"

so that when I enter a number in "years" it automatically
updates the "to" field to the correct number of years
added to the "from" field.

ie: From 01/01/2004 enter "years" ie: 3(but needs to be
used for any number)and automatically the "To" date will
show 01/01/2007

Much obliged


.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top