Invalid use of Null Error (1 Viewer)

kawai6rr

Registered User.
Local time
Yesterday, 23:32
Joined
Aug 26, 2009
Messages
21
below I have some code that calculates the age from their brirth date. I keep getting the Invalid use of Null Error when I insert a DOB and the age is already dispalyed. Does anyone know how to correct this?

Dim lngAge As Long
Dim dteBirthdate As Date
dteBirthdate = Me.DOB.Value
If Not IsNull(Me.DOB.Value) Then
'If Not IsNull(Me.Age.Value) Then

' Make sure passed-in value is a date.
If Not IsDate(dteBirthdate) Then
dteBirthdate = Date
End If

' Make sure birthdate is not in the future.
' If it is, use today's date.
If dteBirthdate > Date Then
dteBirthdate = Date
End If

' Calculate the difference in years between today and birthdate.
lngAge = DateDiff("yyyy", dteBirthdate, Date)
' If birthdate has not occurred this year, subtract 1 from age.
If DateSerial(Year(Date), Month(dteBirthdate), Day(dteBirthdate)) > Date Then
lngAge = lngAge - 1
End If
CalcAge = lngAge
Me.Age.Value = lngAge
Else
MsgBox "The age is already determined, delete the age and then add the date of birth."
'End If
End If
 

boblarson

Smeghead
Local time
Yesterday, 23:32
Joined
Jan 12, 2001
Messages
32,059
Where's the error actually show up highlighted?

Also, are you making sure to exit the text box so that the value is updated?
 

Brianwarnock

Retired
Local time
Today, 07:32
Joined
Jun 2, 2003
Messages
12,701
Code:
dteBirthdate = Me.DOB.Value
If Not IsNull(Me.DOB.Value) Then
'If Not IsNull(Me.Age.Value) Then

' Make sure passed-in value is a date.
If Not IsDate(dteBirthdate) Then
dteBirthdate = Date
End If

' Make sure birthdate is not in the future.
' If it is, use today's date.
If dteBirthdate > Date Then
dteBirthdate = Date
End If
This is a bit messy you need to test values before using not after, it is also annoying not to see the first line which contains important information.

Brian
 

kawai6rr

Registered User.
Local time
Yesterday, 23:32
Joined
Aug 26, 2009
Messages
21
I had it as an onDirty so this is where I was running into problems. I changed it to after update and this seemed to be the better way.
 

Users who are viewing this thread

Top Bottom