473,320 Members | 2,202 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Calculating Retirement Date from DOB in an Employee Table

If DOB is 01.04.1960, the DOR (date of retirement) would be 31.03.2020.
Else, if DOB is 02.04.1960 or any other date in the month, the DOR would be 30.04.2020.
(Retirement principle= DOB+60 years)
Aug 31 '18 #1

✓ answered by NeoPa

You're quite right Phil. That seems to be the question. I guess from the name we can assume that expressing that clearly in English may be even more difficult than it would for someone for whom English is their first language, and let's face it many of those would struggle too.

So, assuming that any day except the first of the month ends up as the end of the same month, but when it's the first they squeak into the previous month. The logic to follow would be :
  1. Take off one day from the original [DOB].
  2. Format the result into month & year (mmm yyyy).
  3. Convert this string back into date format. With no day specified it will always assume the first of the month.
  4. The result is now the start of the correct month so we need to (A) add a month plus sixty years then (B) subtract a day.
    1. Adding a month can also include adding the sixty years if we do it by months. 60 x 12 = 720. We add 721.
    2. Subtract a day again.
In SQL that would be something like :
Expand|Select|Wrap|Line Numbers
  1. DateAdd('d',-1,DateAdd('m',721,CDate(Format(DateAdd('d',-1,[DOB]),'mmm yyyy')))) AS [DOR]

15 13088
zmbd
5,501 Expert Mod 4TB
This is simple enough that the DateAdd Function should do the trick.

DateAdd ( interval, number, date )

One thing to note: Internally Access handles dates using the USA format #MM/DD/YYYY# especially in VBA. Usually, both the SQL and the VBA engines deal with the international formats correctly; however, if you have issues with the function returning the correct date then you might try explicitly setting the date in the USA format (note the "#" which indicates a date literal):
DateAdd("Y",60,#04/01/1960#)
Aug 31 '18 #2
PhilOfWalton
1,430 Expert 1GB
Don't think it is as simple as that.

Without my usual rant about the total illogicality of the US date format, and the somewhat less illogicality of the UK date format, I am at a bit of a loss to understand the logic of Nityanda's question.

He or she seems to say that if the DOB was the first of the month, the DOR is DOB + 59 years + 361, 362, 363, 364 days (Depending on the month) to take you to the last date in the previous month.

On the other hand, if the DOB is after the first of the month, the DOR is DOB + 60 years + up to 29 days (depending on how many days in the month) to take you to the end of the month.

That really seems illogical. Can the OP confirm this is correct before giving a solution.

Phil
Aug 31 '18 #3
NeoPa
32,556 Expert Mod 16PB
ZMBD:
One thing to note: Internally Access handles dates using the USA format #MM/DD/YYYY# especially in VBA.
Back to school with you. That's just wrong. Only date strings in SQL could be considered to behave that way, but even then that isn't 100% as that's only one of the valid formats for a SQL date string. See Literal DateTimes and Their Delimiters (#).

VBA, and the data within the database itself, stores dates internally as numbers. These numbers typically reflect the number of days (and parts thereof for times) from a known starting date. In MS Office that date is 30 december 1899.
Aug 31 '18 #4
NeoPa
32,556 Expert Mod 16PB
You're quite right Phil. That seems to be the question. I guess from the name we can assume that expressing that clearly in English may be even more difficult than it would for someone for whom English is their first language, and let's face it many of those would struggle too.

So, assuming that any day except the first of the month ends up as the end of the same month, but when it's the first they squeak into the previous month. The logic to follow would be :
  1. Take off one day from the original [DOB].
  2. Format the result into month & year (mmm yyyy).
  3. Convert this string back into date format. With no day specified it will always assume the first of the month.
  4. The result is now the start of the correct month so we need to (A) add a month plus sixty years then (B) subtract a day.
    1. Adding a month can also include adding the sixty years if we do it by months. 60 x 12 = 720. We add 721.
    2. Subtract a day again.
In SQL that would be something like :
Expand|Select|Wrap|Line Numbers
  1. DateAdd('d',-1,DateAdd('m',721,CDate(Format(DateAdd('d',-1,[DOB]),'mmm yyyy')))) AS [DOR]
Aug 31 '18 #5
PhilOfWalton
1,430 Expert 1GB
@ Neopa

Certainly the SQL works perfectly when converted to VBA and changing all the single quote to double quotes.

It also gives the correct UK dates in SQL

Phil
Aug 31 '18 #6
NeoPa
32,556 Expert Mod 16PB
Yes Phil.

SQL & VBA aren't entirely dissimilar. Certainly the quotes used for VBA are different from those recommended for SQL.

Bear in mind the expression is mostly based on functions and their returned values.
Sep 1 '18 #7
twinnyfo
3,653 Expert Mod 2GB
NeoPa,

I like your very logical approach to this problem, and (yet again) a one-liner solution to the problem. I actually come across date calculations similar to this from time to time at work—I just slogged through it. Now I have a more logical and elegant approach.

Thanks.
Sep 1 '18 #8
zmbd
5,501 Expert Mod 4TB
ZMBD:
One thing to note: Internally Access handles dates using the USA format #MM/DD/YYYY# especially in VBA.
NeoPa: Back to school with you. That's just wrong. Only date strings in SQL could be considered to behave that way, but even then that isn't 100% as that's only one of the valid formats for a SQL date string. See Literal DateTimes and Their Delimiters (#).

VBA, and the data within the database itself, stores dates internally as numbers. These numbers typically reflect the number of days (and parts thereof for times) from a known starting date. In MS Office that date is 30 december 1899.
Neopa - you know I have a great respect for you - so so much you have taught me and yet so much more to teach me!
Your solution, as usual, is quite elegant in design and application. You caught OP's intent that I allowed my chemistry background to mislead me from as I will explain later ( I need to start re-reading these posts two or three times! )
> Yes, as you well know, I am aware of how Access and Excel actually store numbers, as a double (dateserial) of the elapsed time since 1900-01-01 (or is that 1899-12-31, or 1899-12-30 (and Lotus' opps with leap years!) we just discussed that in another thread and I've ran across that same information again last week - funny how things run in cycles like that )

> Reading thru OP's post I noticed that the dates were in DD.MM.YYYY format - early on, as I live in the USA, International dates were often a thorn in my side (great gobbledygook - couldn't anyone agree on a global date format a century ago - would have saved us all from that that stupid Y2K mess!), and until I read Allen's website, I had no clue as to why this was so - and it is Allen's article (how long ago was that? How long has Allen been working with Access - looks like 1992 on his site - doesn't seem that long ago) which was the bases for my earlier post: Allen Browne: International Dates in Access
1. Misinterpretation in the User Interface
Define the date format for your locale in Windows Control Panel | Regional Settings. You can therefore use your local date format when you enter dates into the user interface part of Access: tables, queries, forms, or the Criteria of Query Design View. (...)
So using the DateAdd() with a DD.MM.YYYY format in the SQL editor should be just fine with the regional formatting as set in the OS; however, that may not always be true and I've had to debug some databases I've inherited that didn't work quite as intended until I forced the format to #MM/DD/YYYY# - I've seen some weird stuff happen with VBA functions used in SQL, I think that this is due to the attempted interpretation of the date as Allen mentions in the remainder of the above paragraph and the fact that I was receiving data with dates in YMD, DMY, and MDY some with dots or hyphens others with slashes... gives me a headache revisiting that nightmare!

As for the VBA statement - again in part from Allen's article:
2. Wrong Formatting in Code In VBA code, delimit dates with the "#" symbol. Regardless or your regional settings, Access expects these literal dates to be in the American format, e.g. #12/31/1999#.

To demonstrate this, enter any date in the Criteria row under a date field in Query Design, and then switch to SQL View. In Query Design view, you see the date according to your local settings, but the SQL statement uses mm/dd/yyyy format.

SQL clauses are not always obvious, e.g. the Filter of a form, the WhereCondition of OpenReport, or the third argument of a domain aggregate function. Examples:
DoCmd.OpenReport "MyReport", acViewPreview, , "InvoiceDate > #12/31/2000#"

Debug.Print DLookup("StudentID", "tblStudent", "EntryDate = #6/30/1953#")

strSQL = "SELECT * FROM tblDonation WHERE DonationDate > #" & Format(Me.StartDate, "mm\/dd\/yyyy") & "#;"

The third example demonstrates how to concatenate a date into a SQL string. The Format() function is essential to force the date into American format. Unfortunately, Format() replaces the slashes with the date separator character defined in Control Panel | Regional Settings, so you must specify literal slashes in the format string by preceding the slash with backslashes. (...)
-Whenever I know that I'll be dealing with international dates I've used the principle as given and then shown in the examples in my vba coding to enclose my dates in the "#" to ensure that there are no issues with international dates.

Speaking of principle ideas:
nityanda10
If DOB is 01.04.1960, the DOR (date of retirement) would be 31.03.2020.
Else, if DOB is 02.04.1960 or any other date in the month, the DOR would be 30.04.2020.
(Retirement principle= DOB+60 years)
My apologies for missing that you were using the last day of the Birth Month 60 years from the date of birth - Both Phil and Neopa were a bit more astute in catching that there was a glitch between the examples and your final statement.
I Highlight the last statement in your post where you state the "Principle" of the retirement is DOB+60years - this is a very specific terminology which is not in-line with the examples given - and the red herring I followed in answering your question. Perhaps my misinterpretation of your post is due to my chemistry background; if one states that something is "the principle" upon which an methodology is built upon, then that principle becomes the guiding force behind any solution to the problem at hand - I incorrectly assumed your examples were simply mis-calculated instead of more carefully reading your examples as given - I do apologize for that error. I only point this out to help you with future questions.


PhilOfWalton:(...) Without my usual rant about the total illogicality of the US date format, and the somewhat less illogicality of the UK date format(...)
Rant away! The only logical format, IMNSHO, is YYYYMMDDHHmmss. Preferably no punctuation to confuse the matter; however, if one would have to use punctuation then the underscore or hyphen so that one can use them in file names without issues as such: YYYY-MM-DD-HHmmss .
Sep 1 '18 #9
PhilOfWalton
1,430 Expert 1GB
@ zmbd

I agree completely the logical order is year, month day - the time bit seems reasonably universal, so does' really enter into the discussion, but I feel punctuation is necessary.
If you were to enter 2018111 without punctuation, would that be interpreted as 1st November 2018 or 11th January?

Phil
Sep 1 '18 #10
PhilOfWalton
1,430 Expert 1GB
Going back to the dates problem, here is a simple function.
Remember I live in the UK

Expand|Select|Wrap|Line Numbers
  1. Public Function ShowDate(DateIn As Date) As Date
  2.  
  3.     Debug.Print Format(DateIn, "dd mmm yyyy")
  4.  
  5. End Function
If I type ?ShowDate(#1/2#) in the immediate window, I get 02 Jan 2018 - wrong as far as I am concerned
If I type ?ShowDate(#1 Feb#) in the immediate window,I get 01 Feb 2018 - Correct

Now from a UK point of view, those "DateIns" are identical. So Access makes the wrong assumption that if the UK day is 12 or less that it actually the month.

I have a very involved routine that gets the correct answers but relies in the dates being entered as strings rather than dates, and uses the regional settings to get the correct values.

There must be a simpler way.

Phil
Sep 1 '18 #11
zmbd
5,501 Expert Mod 4TB
PhilOfWalton
If you were to enter 2018111 without punctuation, would that be interpreted as 1st November 2018 or 11th January?
However, note that the format as given is explicitly: Four digit year, two digit month, two digit day - YYYYMMDDHHmmss; therefor, 2018111 would be malformed and the value would be rejected as a valid date. The value would have to be entered as either 20181101 for November 1, 2018 or as 20180111 for January 11, 2018. The only reason I can see for using punctuation is as a visual clue that one is talking about a date 2018-01-11 and not twenty-million-one-hundred-eighty-thousand-one-hundred-eleven. Herein, again, a standard delimiter such as the dash or underscore would be ideal to allow for file names without confusion with directory paths or extensions.
Going back to the dates problem, here is a simple function. (...)
If I type ?ShowDate(#1/2#) in the immediate window, I get 02 Jan 2018 - wrong as far as I am concerned
If I type ?ShowDate(#1 Feb#) in the immediate window, I get 01 Feb 2018 - Correct (...)
Allen discusses this aspect in the remainder of section 1 that I partially quoted.
(...)relies in the dates being entered as strings rather than dates(...)There must be a simpler way.
I agree there should be an easier way. When I parse external data I have some fairly lengthy code to check for things such as [data] Like "##[. , , , /, -]*" then check Left([Data],2)>=13 etc... fortunately our business model dictates two digit day (the 1st is "01" not "1"), either text month (MMM or MMMM) or two digit month, and four digit year and 24-hour clock; thus, I can reject the data file on the first record if this isn't followed - many are not so blessed. (more than one customer unhappy with me when I rejected their data - if everyone would adopt ISO 8601 - ROTFL).
Sep 1 '18 #12
NeoPa
32,556 Expert Mod 16PB
ZMBD:
Neopa - you know I have a great respect for you
Hi my friend. I hope you understand that this is also true in reverse. Not simply for your understanding of the subject, but also for your volunteer efforts to help others here on Bytes.com. Make no mistake - they are considerable and extremely helpful. I hope my earlier post was seen for what it was intended - a playful slap-down when you made a rare mistake. For most others I'd have been a lot more careful how I expressed it - to avoid causing offense.

Moving back to the discussion about dates though - what I was trying to point out, and you may notice that Allen Browne (An Aussie cleric and ex-MVP, as he's now retired bless him.) also tries to make clear with his repeated use of the term "date literals" to describe strings which are expected to be interpreted as dates, as opposed to date values which are something else again, was the difference of handling between dates themselves, and those date literals that need to be interpreted into dates one way or another.

What is often confusing, especially in Access where we often have to deal with SQL strings directly, even going as far as to build up strings in VBA code that we later plan to execute as SQL commands, is that when using the QBE grid in a QueryDef the literal date strings we type in are interpreted using our local settings, yet when we look at the SQL version of that same QueryDef, the literal date string has been converted to that SQL date literal standard that matches the US date format (m\d\y).

I believe strongly that in such a complicated & confusing situation it is important that we help new users by being as accurate and specific as we can in our terminology - just as Allen Browne was always careful to do.
Sep 1 '18 #13
NeoPa
32,556 Expert Mod 16PB
As far as principles go, one can refer in English to a basic principle which might be a fundamental idea that is open to specific qualifications. Unlike the usage in Chemistry circles certainly. Bear in mind also that the OP is unlikely to be a native English speaker.

So it was that I interpreted the post. The fundamental concept is [DOB] + 60 years. Fine. More specifically though, some extra rules need to be applied. In chemistry this would be included within that which is the principle. The extra rules, from my interpretation of the post - and particularly the examples - is that the [DOR] always falls at the end of a month and that, unless you're born on the first of a month - when instead you're considered to be from the month before - your [DOR] will fall at he end of the same month as your [DOB].

From experience, and a certain understanding of human nature, I've realised that most people struggle to express ideas clearly in an explanation, but they can better express them using examples. If you've ever worked on projects you'll know that in all but the rarest of cases they get thrown back after you've produced it exactly to their specification, simply because what they said never matched what they were actually thinking. Many of us now tend to rewrite what they say into a detailed specification document that they have to sign before we start the work. As those doing the work generally have much better skills at specifying requirements precisely, a great deal of misunderstanding is avoided when they see in black and white what "what they said" exactly means.
Sep 1 '18 #14
PaulamaG
1 Bit
Thanks for sharing your experiences here. I have just got such a task from my employer, and I didn’t know how to even start, but now I understand how to do it.

(...)
May 10 '22 #15
cactusdata
214 Expert 128KB
You should never fall back to string handling of dates if it can be avoided. And here it easily can:

Expand|Select|Wrap|Line Numbers
  1. DateSerial(Year([DOB]) + 60, Month([DOB]) + Sgn(Day([DOB]) - 1), 0) As [DOR]
May 11 '22 #16

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: William Wisnieski | last post by:
Hi Everyone, Access 2000 I have some code behind a button that performs a word merge with a query data source. The merge works fine. But what I'd like to do somehow is after the merge is...
2
by: S.Dickson | last post by:
I have a mysql table, with a field called DATE, how do i get it to automatilly put the date on it every time a new recored it put on the table Date and time would be good, Tahnks
0
by: Shawger Lager | last post by:
Hi, I am new to DB2 (and SQL) and I am having some problems with the current date. I am trying to make sure the date in the table review is not past the current day using a constriant. I have tried...
1
by: slynch401k | last post by:
I am trying to import a paradox table into an access database. I need to do this daily as I get a download of this file everyday. I want to do this on startup of a form. How can I check the date...
1
by: vp.softverm | last post by:
hi all . Am facing problem with the date picker. when i click on date picker in a popup window. the date table is opened in the middle of the pop up window. and it is unable to scroll with...
5
by: ezra | last post by:
I have created an employee database in MS Access 2003. However, I have now discovered a couple of cases of the same person being entered into the database two or more times (The problem being they...
2
tuxalot
by: tuxalot | last post by:
I have a textbox, on with a record source . FrmMain is a tabbed form showing injury data for a given EmployeesID. I have a textbox, on a subform . with as the record source. is entered by a...
0
by: Souniel Gaikwad | last post by:
I want save current date in my table. For that i was used System.DateTime.Now.ToShortDateString(); but it gives following error: The conversion of a char data type to a datetime data type...
5
by: Pappu | last post by:
Hello, I have created a table called EmployeeTable and am done with login page, But problem is, when ever a user try to change password it shows "password change sucessfull" but the new password...
3
by: Adict | last post by:
I am trying to log my users action into a back-end table if a employee master data is change. Below is my code but i am getting an error with inserting a date. Dim strSQL As String strSQL =...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.