473,382 Members | 1,665 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,382 software developers and data experts.

Function issue to calculate age from birthday

19
Hi,

I downloaded a function from the Microsoft Knowledge base to calculate a persons age as at the current date based on DOB. The function is as follows:

Function Age(varBirthDate As Variant) As Integer
Dim varAge As Variant

If IsNull(varBirthDate) Then Age = 0: Exit Function

varAge = DateDiff("yyyy", varBirthDate, Now)
If Date < DateSerial(Year(Now), Month(varBirthDate), Day(varBirthDate)) Then
varAge = varAge - 1
End If
Age = CInt(varAge)
End Function

I've tested it in the VB immediate window (Access 2K) by entering
?Age(#17/12/1975#)

This returns 30 - correct, as at today's date - 02/11/2006. However if I enter
?Age(#10/12/1975#), it returns 31. The only reason for this that I can think of is that the date format is being transposed into MM/DD/YYYY format. I've been trying to correct this by formatting the dates using the format function, converting all of the dates to YYYY-MM-DD format, but it still isn't working. I would really appreciate some help on this! Thanks.
Nov 2 '06 #1
6 6171
VALIS
21
Hi,

I downloaded a function from the Microsoft Knowledge base to calculate a persons age as at the current date based on DOB. The function is as follows:

Function Age(varBirthDate As Variant) As Integer
Dim varAge As Variant

If IsNull(varBirthDate) Then Age = 0: Exit Function

varAge = DateDiff("yyyy", varBirthDate, Now)
If Date < DateSerial(Year(Now), Month(varBirthDate), Day(varBirthDate)) Then
varAge = varAge - 1
End If
Age = CInt(varAge)
End Function

I've tested it in the VB immediate window (Access 2K) by entering
?Age(#17/12/1975#)

This returns 30 - correct, as at today's date - 02/11/2006. However if I enter
?Age(#10/12/1975#), it returns 31. The only reason for this that I can think of is that the date format is being transposed into MM/DD/YYYY format. I've been trying to correct this by formatting the dates using the format function, converting all of the dates to YYYY-MM-DD format, but it still isn't working. I would really appreciate some help on this! Thanks.
Hi,

This might not be very orthodox declaring varBirthDate as String would avoid the ambiguity with the date format.
Nov 2 '06 #2
NeoPa
32,556 Expert Mod 16PB
With date literals for testing - always use form #10 Jan 2006# rather than any form that could be ambiguous.
This is particularly sensible when dealing with databases (SQL) as it won't use the default as set up for your PC in certain circumstances.
Nov 2 '06 #3
NeoPa
32,556 Expert Mod 16PB
Hi,

This might not be very orthodox declaring varBirthDate as String would avoid the ambiguity with the date format.
Unorthodox can be good, I wouldn't recommend wasting the benefits of typing of variables here though. Better to understand the problem and get the code right.
Nov 2 '06 #4
VALIS
21
Unorthodox can be good, I wouldn't recommend wasting the benefits of typing of variables here though. Better to understand the problem and get the code right.
Agreed. I was dealing with a problem that didn't exist. The problem was the test not the code. Doh!
Nov 2 '06 #5
birchw
19
Thanks guys for your prompt responses. I've incorporated the function into my database, rather than testing it in the immediate window and the results are OK. You learn something new every day...
Nov 2 '06 #6
PEB
1,418 Expert 1GB
To assign a format that you want you can use the following:

Cvdate(Format("04/03/2006","dd/mm/yyyy")) and it should be considered as 04 mars 2006

:)
Nov 4 '06 #7

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

Similar topics

3
by: Brynn | last post by:
Here is a function where you don't have to worry about the leap year. You will have to still be aware of time differences between you and the server, and correct your data before entering it into...
1
by: Brynn | last post by:
My function versus the Int(DateDiff()) approach Here was the code used ... then the results By the way, my function and aspfaq.com's calculations are equal (and correct for that matter). ...
26
by: Frank | last post by:
For my website i would like to display the age of my son in years, months, days and hours. For now i manage to get a result for totals. Like the total number of days. This is the beginning: ...
6
by: charliewest | last post by:
Can someone pls point me to or recommend the easiest way to calculate someone´s age using the TimeSpan object, in .NET CF? Isn´t there a simple way to use the TimeSpan object to calculate the...
3
by: CWWong | last post by:
I am using DetailsView using ObjectDataSource with DataSourceTypeName assigned to the specific class. SelectMethod, UpdateMethod and DeleteMethod is working successfully, except InsertMethod. The...
0
by: sakurasyi | last post by:
hi there... i really need your help.. i develop a calendar event using java. all event that i insert will be saved into xml file. But, i have problem when trying to remove the event that i...
3
by: rwise5 | last post by:
I have been tasked with finishing the following C program. I need to develop the sort by birthday function and the print grade function. I have been working on the print function for the last week...
9
by: Pierre Quentel | last post by:
Hi all, I have searched in the standard distribution if there was a function to return the difference between 2 dates expressed like an age : number of years, of months and days. The difference...
4
by: OzNet | last post by:
I have some functions to calculate the working days in a given period. This includes a table that is queried to calculate the number of public holidays that don’t occur on a weekend. If I test...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.