Hello there!

I need your expertise in vb6. I made a new exercise for my vb6 subject using database Microsoft access. With the fields of FirstName, MiddleName, LastName, Birthdate and Age. and there is correct information of my family in every fields. Using navigation command. Im using Microsoft ADO Data control 6.0 or ADODC. Im using Microsoft.Jet. OLEDB 4.0 to connect the database. With text box in the form in every fields. I bind the all fields except the age. I want the correct age display in the text box in every family information i have. Can share me a correct procedure or code for my exercise i made. Thank you very much....

Dani AI

Generated

As noted, a raw year-difference gives a quick result and confirmed it works in many cases. For correctness, however, subtract one when the current month/day is still before the birthday this year — that avoids the common off-by-one around birthdays (including leap-year born people). Keep the Age textbox unbound and populate it from the record’s Birthdate each time the current record changes.

Function CalcAge(ByVal dBirth As Date) As Integer
    Dim iAge As Integer

    If IsNull(dBirth) Then
        CalcAge = -1        ' caller decides how to display "unknown"
        Exit Function
    End If

    If dBirth > Date Then
        CalcAge = 0         ' future birthdate; treat as 0 or blank as you prefer
        Exit Function
    End If

    iAge = Year(Date) - Year(dBirth)

    If (Month(Date) < Month(dBirth)) Or _
       (Month(Date) = Month(dBirth) And Day(Date) < Day(dBirth)) Then
        iAge = iAge - 1
    End If

    CalcAge = iAge
End Function


Private Sub UpdateAgeFromCurrentRecord()
    Dim v
    v = ADODC1.Recordset.Fields("Birthdate").Value

    If IsNull(v) Then
        txtAge.Text = ""
    Else
        txtAge.Text = CStr(CalcAge(CDate(v)))
    End If
End Sub

Call UpdateAgeFromCurrentRecord after navigation (for example from your navigation button handlers or from the control event that runs after a move). Also call it from the Birthdate textbox AfterUpdate if users can edit the date. Keep txtAge unbound and locked (txtAge.Locked = True) so it’s read-only. Handle Null and future dates explicitly and avoid storing Age in the database — keep Birthdate as the source of truth and compute Age on the fly.

Recommended Answers

All 6 Replies

Lets see what you have done so far.

Here's the example:

First Name: Edwin
Middle Name: Quiroben
Last Name: Paglinawan
Birth Date: Oct/6/1970
Age:

On age text bxo it should display the correct age with my birthdate minus with the year of the system. The Edwin Quiroben paglinawan and oct/06/1970 is in text box because they are in the database....

If I understand your problem then in the Change event of the birth date text box put something like the following:

Dim DateNow As Date
Dim DateBD As Date

DateNow = Now()

' The following asumes the Birthday textbox is called txtBD and 
' The age textbox txtAge

DateBD = CDate(txtBD.Text)

txtAge.text = DateDiff("YYYY", DateBD, DateNow)

' you may need a me.refresh or/and a doevents here

Hope that answers your question.

Thank you sir i got your code its working.

txtAge.text = DateDiff("YYYY", DateBD, DateNow)

Sorry but i didn't understand what are you trying to say

commented: What you didn't understand ? -3

very good i was also in need of that..

good job.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.