Crear un campo [fecha nacimiento] y [edad], dependientes o independientes ...
En el evento afterupdate (despues de actualizar) del campo[fecha nacimiento]de un formulario colocar este código:
Dim totalaños, totalmeses, difmeses
If IsNull([fecha nacimiento]) Then
Edad.Visible = False
Else
Edad.Visible = True
totalaños = DateDiff("yyyy", [fecha nacimiento], Date) + (DateSerial(Year(Date), Month([fecha nacimiento]), Day([fecha nacimiento])) > Date)
totalmeses = DateDiff("m", [fecha nacimiento], Date) + (DateSerial(Year(Date), Month([fecha nacimiento]), Day([fecha nacimiento])) > Date)
difmeses = totalmeses - (totalaños * 12)
Edad = (totalaños & " " & "Años" & " " & "y" & " " & difmeses & " " & "Meses")
End If
End Sub
Si los resultados se debieran de calcular cada vez que se abre el formulario copiar el mismo código también en el evento Al activar registro de dicho formulario ...
Este código parece funcionar muy bien aunque sea transformable en función y que dicha función pueda calcular los días también (no he encontrado el metodo todavía) y que facilmente se puede implementar la conversión a texto del campo de edad a calcular así como hacer que distinga entre singular y plural en la frase resultante ...
Estoy abierto a sugerencias o criticas para mejorar este método de calcular edades ...
Emmanuel Lévêque
Otras funciones alternativas para calcular la edad (EN ingles)
(Q) How do I calculate the age of a person given his/her birthdate?
(A) There are several methods to do this. I'll list two methods that have been posted to the newsgroups in the recent past:
**Posted by Michel Walsh**
Assuming that the birthdate field is called [BDate] and is of type date, you can use the following calculation
Age=DateDiff("yyyy", [Bdate], Now())+ _
Int( Format(now(), "mmdd") < Format( [Bdate], "mmdd") )
Alternate: You can use this function to calculate Age.
Function Age(Bdate, DateToday) As Integer
' Returns the Age in years between 2 dates
' Doesn't handle negative date ranges i.e. Bdate > DateToday
If Month(DateToday) < Month(Bdate) Or (Month(DateToday) = _
Month(Bdate) And Day(DateToday) < Day(Bdate)) Then
Age = Year(DateToday) - Year(Bdate) - 1
Else
Age = Year(DateToday) - Year(Bdate)
End If
End Function
--Posted by Tim Walters---
Here's another detailed Age Checker.
'--- CODE START ---
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
'--- CODE END
               (
geocities.com/es/ensolva/Descargas)                   (
geocities.com/es/ensolva)                   (
geocities.com/es)