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 Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Calculating Age on a form 2

Status
Not open for further replies.

Jeddiah

Programmer
Joined
Apr 30, 2002
Messages
17
Location
US
I'm kind of new to Visual Basic. I know there is a way to calculate the age on a form using the date and year but I'm getting some weird results. Can someone out there assist me. Here is what I have so far:
I have an Age Field and a Birthday field, the birthday field is afterupdate reads =Age(), the Age field is unbound.


Private Function Age()
Dim Age As Integer
Dim Now As Date

[Age] = Date() - [Birthday]
End Function

This is a module attached to the

 
As Dates are stored as numbers, the integer part representing days and the fraction part representing parts of the day then your code calculates the numbers of DAYS of the age.

Is that really what you want ?



Private Function Age() As Integer
Dim Age As Integer ' <<< DO NOT DO THIS Age is the Function Name
Dim Now As Date ' <<< DO NOT DO THIS Now() is a system value - don't try to redefine it.

Age = Year(Date()) - Year(Birthday)
If Month(Date()) <= Month(Birthday) AND Day(Date()) < Day(Birthday) Then
Age = Age - 1
End If

End Function




QED.

G LS
 
O.K. ... but ... why all that 'stuff'?

Code:
Public Function basAge(BirthDay As Date) As Long

    'Michael Red 8/8/02     Simplistic Age Cals

    basAge = DateDiff(&quot;yyyy&quot;, BirthDay, Date)
    basAge = basAge + (DateSerial(Year(Date), Month(BirthDay), Day(BirthDay)) > Date)

End Function

while i do realize this is just meant as a demo snippet, in general, one might want to avoid ye olde &quot;IF&quot;, as it tends to slow things a bit. also, the premise of using the intrinsic date part functions is curious, in just retrieveing the year of occurance and then going theough the math part in addition (or even subtraction) when even little old Ms. A is capable of this feat w/p the (direct) artihm op.


MichaelRed
m.red@att.net

There is never time to do it right but there is always time to do it over
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top