Calculate a person's age not working.

john527

Registered User.
Local time
Today, 09:45
Joined
Jul 9, 2006
Messages
36
Hi all ,
I am trying to calculate a person’s age.
Went to Customer Form
Have a field called cust_birthday ( date/time ) ( 99;00;00;\>LL;0; )
Have a field called cust_age ( Number )

Went to Madules / New / then add this code,

Public Function Age(dteDOB As Date, Optional SpecDate As Variant) As Integer
Dim dteBase As Date, cust_age As Date, cust_birthday As Integer
If IsMissing(SpecDate) Then
dteBase = Date
Else
dteBase = SpecDate
End If
cust_birthday = DateDiff("yyyy", dteDOB, dteBase)
cust_age = DateSerial(Year(dteBase), Month(dteDOB), Day(dteDOB))
Age = cust_birthday + (dteBase < cust_age)
End Function


Not Working get no return. Can someone help me. Thank You if you can….

John
 
I just copied and pasted your code into a standard module and it seems to work just fine. Do you know how to single step your code with the debugger to see what is going on?
 
Debugger .

Hi, no I do not know how to single step my code with a debugger to see what is going on. Do you think you can explain it to me, would appreciate it.

Thank You For your Time

John527
 
Go to the module where the age() function is located. Left click outside the left border of the edit box on the line starting with If IsMissing(SpecDate) Then. That will highlight the line and place a dot outside the edit box which is a breakpoint. when you run your form the code will stop on the breakpoint and hovering over variables will display their value. Pressing F8 will single step the code and you can watch your variables change value and follow the path of your code. Good Luck.
 
Actual VBA Code to calc age

'V1.00 (tests ok)
Public Function CalcAge(DOB As Date, Today As Date) As Long
Dim y1 As Long
Dim m1 As Long
Dim d1 As Long

Dim y2 As Long
Dim m2 As Long
Dim d2 As Long

Dim y As Long

y1 = year(DOB)
m1 = month(DOB)
d1 = day(DOB)

y2 = year(Today)
m2 = month(Today)
d2 = day(Today)

If ((m2 > m1) Or ((m2 = m1) And (d2 >= d1))) Then
y = y2 - y1
Else
y = y2 - y1 - 1
End If

CalcAge = y

End Function
 

Users who are viewing this thread

Back
Top Bottom