Year of birth from age (1 Viewer)

Kuleesha

Member
Local time
Today, 12:36
Joined
Jul 18, 2021
Messages
50
Hi,
I have a form which contains the fields "first visit date" (contains today's date in date/time format), age and birth year (as number).
I want to caculate the year of birth by filling the other 2 fields. I wrote the following code for this but it doesn't work.

Code:
Private Sub txtAge_AfterUpdate()
    Dim thisyear As Integer
    Dim birthyear As Integer
    Dim age As Integer
    
    thisyear = Year(#Me.txtFirstVisit#)
    age = Me.txtAge
    birthyear = thisyear - age
    
    If Me.txtBirthYear = Null Then
    Me.txtBirthYear = birthyear
    End If

End Sub

I'm an absolute newbie. Please help.
 

theDBguy

I’m here to help
Staff member
Local time
Today, 00:06
Joined
Oct 29, 2018
Messages
21,493
Hmm, try changing this line to remove the hash tags.
Code:
thisyear = Year(Me.txtFirstVisit)
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 03:06
Joined
Feb 19, 2002
Messages
43,346
BirthYear = Year(DateAdd(,"yyyy", Me.Age * -1, Date())

Storing Age is redundant and will cause conflicts because it changes over time. Maybe even tomorrow:)

If you can't store the actual birth date, store just the BirthYear and calculate the age as needed
 

isladogs

MVP / VIP
Local time
Today, 08:06
Joined
Jan 14, 2017
Messages
18,246
Agree with previous comments.
You cannot get the Year of Birth from the Age with absolute certainty.
For example two people born on 31 Dec 2000 and 1 Jan 2001 are both currently 20 years old but they have different birth years
 

Kuleesha

Member
Local time
Today, 12:36
Joined
Jul 18, 2021
Messages
50
Above solutions did not work for me.
I know this does not feel like sound practice but I have met some people who know their age but cannot remember their birth year. I want to use this in this situation where I enter age into non-bound text box and it auto-populates the birth year for me which is the value that is stored. I will later use this birth year to calculate the age of the person at each visit. I don't mind a 1 year difference in the the birth year.
I tried chaging my code a bit as follows but still doen't work
Code:
Private Sub txtAge_LostFocus()
Dim ThisYear As Integer
Dim BirthYear As Integer
Dim Age As Integer

Age = CInt(Me.txtAge)
ThisYear = Year(Now)
BirthYear = ThisYear - Age

If Me.txtBirthYear = Null Then
Me.txtBirthYear = BirthYear
End If
End Sub

I have attached a smaller version of my database here.
Please help.
 

Attachments

  • Age.accdb
    512 KB · Views: 434

plog

Banishment Pending
Local time
Today, 02:06
Joined
May 11, 2011
Messages
11,653
This should be the code you need:

Code:
Private Sub txtAge_LostFocus()

If IsNumeric(Me.txtAge) Then Me.txtBirthYear = Year(Date) - Me.txtAge

End Sub
 

theDBguy

I’m here to help
Staff member
Local time
Today, 00:06
Joined
Oct 29, 2018
Messages
21,493
Oh, I didn't catch this earlier. You can't compare null to anything. Try it this way.
Code:
If IsNull(Me.txtBirthYear) Then
 

Kuleesha

Member
Local time
Today, 12:36
Joined
Jul 18, 2021
Messages
50
Oh, I didn't catch this earlier. You can't compare null to anything. Try it this way.
Code:
If IsNull(Me.txtBirthYear) Then
Yes this solved the issue. Much appreciated.
 

Users who are viewing this thread

Top Bottom