Skip to main content

Write a VB Program to accept birth date through textbox from user and calculate age. (Use Message box to display result)

· One min read
Kaustubh Kulkarni
file.vb
Private Sub Command1_Click()  
BirthDate = Text1.Text
MsgBox ("Age :" &ExactAge(BirthDate))

End Sub

Public Function ExactAge(BirthDate As Variant) As String

Dim yer As Integer, mon As Integer, d As Integer
Dim dt As Date
Dim sAns As String

dt = CDate(BirthDate)
If dt> Now Then Exit Function

yer = Year(dt)
Print yer
mon = Month(dt)
Print mon
d = Day(dt)
Print d
yer = Year(Date) - yer
mon = Month(Date) - mon
d = Day(Date) - d
sAns = yer& " year(s) " &mon& " month(s) " & d & " day(s) old."
ExactAge = sAns

End Function