Ayuda en Access 2003

#1
Hola que tal compañeros Bakunos, estoy atorado con un problema en access requiero su apreciable ayuda.

Bueno resulta que me pidieron obtener la edad exacta de una persona en access 2003, con una tabla que contiene los campos de Fecha de Nacimiento y Fecha actual, claro ademas del campo de Edad.

Lo que necesito es obtener la edad en Años y Meses de las personas que estan en la tabla, ya probe con diferentes funciones y no logro obtenerlo (ya puede obtener los años, pero con los meses no mas no se deja).

Si alguien me puede hechar la mano para obtener la informacion se lo agradeceria eternamente.

De antemano Muchas Gracias.
 

mariosl

Bovino adicto
#2
ahi va en programacion
Saludos


Public Sub CalcAge(vDate1 As Date, vdate2 As Date, ByRef vYears As Integer, ByRef vMonths As Integer, ByRef vDays As Integer)
' Comments : calculates the age in Years, Months and Days
' Parameters:
' vDate1 - D.O.B.
' vDate2 - Date to calculate age based on
' vYears - will hold the Years difference
' vMonths - will hold the Months difference
' vDays - will hold the Days difference
vMonths = DateDiff("m", vDate1, vdate2)
vDays = DateDiff("d", DateAdd("m", vMonths, vDate1), vdate2)
If vDays < 0 Then
' wierd way that DateDiff works, fix it here
vMonths = vMonths - 1
vDays = DateDiff("d", DateAdd("m", vMonths, vDate1), vdate2)
End If
vYears = vMonths \ 12 ' integer division
vMonths = vMonths Mod 12 ' only want leftover less than one year
End Sub

Sub x()
Dim anios As Integer
Dim meses As Integer
Dim dias As Integer

Call CalcAge(Now - 1400, Now, anios, meses, dias)
MsgBox anios
MsgBox meses
MsgBox dias

End Sub
 

mariosl

Bovino adicto
#3
Otra manera por si no te cuadra la anterior


'*************************************************************
' FUNCTION NAME: Age()
'
' PURPOSE:
' Calculates age in years from a specified date to today's date.
'
' INPUT PARAMETERS:
' StartDate: The beginning date (for example, a birth date).
'
' RETURN
' Age in years.
'
'*************************************************************
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 Now < DateSerial(Year(Now), Month(varBirthDate), Day(varBirthDate) _
) Then
varAge = varAge - 1
End If
Age = CInt(varAge)
End Function

'*************************************************************
' FUNCTION NAME: AgeMonths()
'
' PURPOSE:
' Compliments the Age() function by calculating the number of months
' that have expired since the last month supplied by the specified date.
' If the specified date is a birthday, the function returns the number of
' months since the last birthday.
'
' INPUT PARAMETERS:
' StartDate: The beginning date (for example, a birthday).
'
' RETURN
' Months since the last birthday.
'*************************************************************
Function AgeMonths(ByVal StartDate As String) As Integer
Dim tAge As Double
If IsNull(StartDate) Then AgeMonths = 0: Exit Function
tAge = (DateDiff("m", StartDate, Now))
If (DatePart("d", StartDate) > DatePart("d", Now)) Then
tAge = tAge - 1
End If
If tAge < 0 Then
tAge = tAge + 1
End If

AgeMonths = CInt(tAge Mod 12)

End Function
 
Arriba