Hello everyone, I’m here again with a new update. Today I will be teaching you guys how to code an age calculator application using visual basic. It’s quite difficult due to the abstract thinking. For example, if your birthday is in September (9) and the month at the moment is January (1) then when doing raw subtracting the outcome will be a negative. Same goes for the day. So you must use a certain formula that will calculate without having the outcome be negative. We use an “if statement” saying that if the current month or day is smaller than the birthday, then subtract 1 from the outcome. We have also added “if statements” for the month’s days since not all months, of course, have the same number of days. Here is the code example:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
Private Sub Command_Click() Dim by, bd, bm, y, m, d, ry, rm, rd As Integer Rem current date y = Val(Text1.Text) m = Val(Text2.Text) d = Val(Text3.Text) Rem birthday by = Val(Text4.Text) bm = Val(Text5.Text) bd = Val(Text6.Text) Rem calculating If m = 1 Or m = 3 Or m = 5 Or m = 7 Or m = 8 Or m = 10 Or m = 12 Then If d < bd Then m = m - 1 d = d + 31 End If rd = d - bd End If If m = 4 Or m = 6 Or m = 9 Or m = 11 Then If d < bd Then m = m - 1 d = d + 30 End If rd = d - bd End If If m = 2 Then If d < bd Then m = m - 1 d = d + 28 End If rd = d - bd End If rd = d - bd If m < bm Then y = y - 1 m = m + 12 End If rm = m - bm If y <= by Then MsgBox ("Please type correct") ElseIf Text4.Text = " " Then MsgBox ("Please fill all information ") Else ry = y - by Label3 = "Your Age is: " & ry & " years and " & rm & " months and " & rd & " days" End If End Sub |

Download The Age Calculator App:

-8ball55

Perfect