T
Tony Williams
I have a control that calculates the age here is the code
Private Sub txtAge_Enter()
Dim dateBirthday As Date
dateBirthday = Date_of_Birth.Value
Dim varAge As Variant
varAge = -DateDiff("yyyy", Date, dateBirthday)
Dim varDiff As Variant
varDiff = DateDiff("d", DateAdd("yyyy", varAge, dateBirthday), Date)
If varDiff < 0 Then
varAge = varAge - 1
End If
txtAge.Value = varAge
If txtAge.Value < 18 Then
MsgBox "This person is under 18!", vbOKOnly, "Warning"
End If
End Sub
However sometimes I don't know the Date of Birth (Date_of_Birth) and when I
tab to Age I get an error message that says Runtime error 94 Illegal use of
null. So I suspect I need some code that says if the Date of Birth is empty
ignore and age is 0 or also empty if date of birth is present then carry on
with the calculation. I've tried something like
If isNull(Date_of_Birth.Value) Then
datebirthday = "" Else
But this gives me error message and I don't think it's that simple
Can anyone help please?
TIA
Tony
Private Sub txtAge_Enter()
Dim dateBirthday As Date
dateBirthday = Date_of_Birth.Value
Dim varAge As Variant
varAge = -DateDiff("yyyy", Date, dateBirthday)
Dim varDiff As Variant
varDiff = DateDiff("d", DateAdd("yyyy", varAge, dateBirthday), Date)
If varDiff < 0 Then
varAge = varAge - 1
End If
txtAge.Value = varAge
If txtAge.Value < 18 Then
MsgBox "This person is under 18!", vbOKOnly, "Warning"
End If
End Sub
However sometimes I don't know the Date of Birth (Date_of_Birth) and when I
tab to Age I get an error message that says Runtime error 94 Illegal use of
null. So I suspect I need some code that says if the Date of Birth is empty
ignore and age is 0 or also empty if date of birth is present then carry on
with the calculation. I've tried something like
If isNull(Date_of_Birth.Value) Then
datebirthday = "" Else
But this gives me error message and I don't think it's that simple
Can anyone help please?
TIA
Tony