Antwort von
nach 11 Tagen
hilfreich
Re^2: Altersberechnung in Access
Achtung, Jahr 2000 Problem:
Access 97 (SR-2) interpretiert Daten so:
(Format: Datum kurz)
Jahr von 0 bis 29: Jahr 20XX
Jahr von 30 bis 99: Jahr 19XX
Das heisst, meine Grossmutter ist dann mit dieser Funktion -14 Jahre alt..... statt 86 Jahre
Meine Lösung:
Public Function WieAlt(GebDat, Optional Stichtag = Null)
Dim s As String, g As String, j As Long, Dat As Date
If IsNull(GebDat) Then
j = 0
Else
g = Format(GebDat, "mmdd")
If IsNull(Stichtag) Then
Dat = Date
Else
Dat = Stichtag
End If
s = Format(Dat, "mmdd")
j = Year(Dat) - Year(GebDat)
If g > s Then
j = j - 1 ' Korrektur
End If
' Problem Jahr 2000: Access 97 (SR-2)
' Datumwerte, Jahreszahl ??XX
' XX >= 30 Jahr 19XX
' XX < 30 Jahr 20XX
' Meine Lösung: Falls das Alter negativ ist, 100 dazuzählen
' Das Alter einer Person kann nie negativ sein....
If j < 0 Then
j = j + 100
End If
End If
WieAlt = j
End Function
Ausserdem habe ich noch den Fall "kein Datum im Formular" abgefangen, da sonst eine Fehlermeldung auftritt...
Gruss
Armin
Public Function WieAlt(GebDat, Optional
Stichtag = Null)
Dim s As String, g As String, j As Long,
Dat As Date
g = Format(GebDat, "mmdd")
If IsNull(Stichtag) Then
Dat = Date
Else
Dat = Stichtag
End If
s = Format(Dat, "mmdd")
j = Year(Dat) - Year(GebDat)
If g > s Then j = j - 1 ' Korrektur
WieAlt = j
End Function