Click here to Skip to main content
15,851,514 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I would like to create a simple program which let user to insert today date (into 3 different text boxes as dd/mm/yy) and their birthday (again into 3 different textboxes) then calculate his current age.


What I have tried:

I followed a tutorial on youtube however calculation is wrong.

This is the code so far.
Dim d1, m1, y1 As Integer
Dim d2, m2, y2 As Integer
d1 = Val(TextBox1.Text)
m1 = Val(TextBox2.Text)
y1 = Val(TextBox3.Text)
d2 = Val(TextBox4.Text)
m2 = Val(TextBox5.Text)
y2 = Val(TextBox6.Text)

If d1 < d2 Then
    d1 = d1 - 30
    m1 = m1 - 1
End If

If m1 < m2 Then
    m1 = m1 - 12
    y1 = y1 - 1
End If

Label8.Text = y1 - y2 : Label7.Text = m1 - m2 : Label6.Text = d1 - d2
Updated 4-Mar-17 5:13am

That's a little complicated, but not too bad.
See here: Working with Age: it's not the same as a TimeSpan![^] - the code itself is in C#, but it's pretty obvious, and if you can't work it out then use this: Code Converter[^] which will convert it to VB.
Share this answer
Share this answer
Bad idea. Why?
1. There is no need to ask a user to enter today date, just code it using DateTime.Today Property (System)[^]
2. Do not ask for date using textboxes. What if the user enter invalid values for year, month, or day? Use a DateTimePicker Class (System.Windows.Forms)[^] instead.
3. On calculating age, check this out: calculate age[^]
Share this answer
When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute, it is an incredible learning tool.

Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900