15,847,263 members
1.00/5 (1 vote)
See more:
i have used above code to calculate age of person. but its getting me error. Type mismatched when i enter any value in textbox i have wrote this code like as below...

i want to display age in textbox when user enter date of birth in textbox like 01/02/1985. age should be display in textbox. how to do this. ?

What I have tried:

```Private Sub txtdateofBirth_Change()
If txtcurrentDate.Text Is Date Then
txtdateofBirth.Text = DateDiff(DateInterval.Year, Now, CDate(txtcurrentDate.Text)).ToString
End If
End Sub```
Posted
Updated 6-Apr-21 4:35am
Richard Deeming 6-Apr-21 9:27am
NB: A person's age is not simply the difference between the year of birth and the current year.

Consider a baby born on 31st December 2020; are they one year old on 1st January 2021?

## Solution 2

You have tagged your question VB6 yet you appear to be trying to use VB.NET constructs - ToString and DateInterval.Year.
I am going to assume that you are indeed using VB.NET - either way you will need to use the `IsDate` function

E.g.
VB
```Private Sub txtdateofBirth_Change()
If IsDate(txtcurrentDate.Text) Then
txtdateofBirth.Text = DateDiff(DateInterval.Year, Now, CDate(txtcurrentDate.Text)).ToString
End If
End Sub```

or in VB6 probably
VB
```Private Sub txtdateofBirth_Change()
If IsDate(txtcurrentDate.Text) Then
txtdateofBirth.Text = Cstr(DateDiff(DateInterval.Year, Now, CDate(txtcurrentDate.Text)))
End If
End Sub```
I also think you have `Now` and your textbox date in the wrong order

Bhavesh Vankar 7-Apr-21 2:25am
i have used vb6.0..not vb.net...
CHill60 7-Apr-21 3:57am
Then try the 2nd solution I suggested. You may need to correct minor errors - I am unable to test it unfortunately because VB6 went out of support decades ago and I'm not prepared to pay good money for it. I stick to VB.NET which is free
Bhavesh Vankar 7-Apr-21 4:56am
ok thanks....

## Solution 1

VB
`If txtcurrentDate.Text Is Date Then`

You cannot do that; `txtcurrentDate.Text` is always a `String`, never a `Date`. You need to parse the string to a Date object using the `CDate` function. I have never used VB6 and since it went out of support some 20 years ago it is not a good choice of language. Switch to VB.NET which is a much better, and fully supported, language.

v2