| Computing.Net: Over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to sign up now, it's free! |
vb calculate age
|
Original Message
|
Name: mullen103
Date: October 31, 2003 at 03:55:16 Pacific
Subject: vb calculate ageOS: 98seCPU/Ram: 4 |
Comment: Hello I would like to know how to calculate age using VB. For example you have two text boxes and a command button. In hte first command button you have to input date of birth and then you press the command button and it outputs age into the other text box Ive tried something already but it does not work it gives lots of numbers Thanks Richard
Report Offensive Message For Removal
|
|
Response Number 1
|
Name: Davour
Date: October 31, 2003 at 04:56:55 Pacific
|
Reply: (edit)Show us what your code looks like, i have the answer for you but i wanna see where your going so i can tweak your code rather than give you new code to use
Report Offensive Follow Up For Removal
|
|
Response Number 2
|
Name: Console
Date: November 3, 2003 at 13:18:04 Pacific
|
Reply: (edit)vb.net? if it is then you will have to play around with the date function( var = DATE.today ) and the split function to put it into days months and years ie. if the date is 03/03/2003(dd/mm/yyyy) then (var()=split("/")) it will dynamically put the values into the array you declared as var(as a string). then the array would be var(0)=03 var(2) = 03 var(3)=2003. the rest is easy...if you ask nicely i will write the code and show it to you.
Report Offensive Follow Up For Removal
|
|
Response Number 3
|
Name: Console
Date: November 3, 2003 at 13:29:20 Pacific
|
Reply: (edit)sorry thats var = date.split("/") where date is the varable you stored your date in, and var is a array not given a index. well here is a real simple example in quickly gonna give you. Hope it will help,it only takes the year as input(ie. 1984 in the first textbox) and outputs the age in years(ie. 19). THE CODE IN MY BUTTON = Dim born, today As Integer Dim now() As String Dim now1 As String now1 = Date.Today now = now1.Split("/") born = CInt(TextBox1.Text) today = CInt(now(2)) born = today - born TextBox2.Text = born
Report Offensive Follow Up For Removal
|
|
Response Number 4
|
Name: Stuart
Date: November 3, 2003 at 13:57:21 Pacific
|
Reply: (edit)Function CalcAge(Birthday As String) As Integer Dim YearDay As Date If Val(Format(Birthday, "y")) > Val(Format(Date, "y")) Then YearDay = DateAdd("yyyy", 1, (Birthday)) Else YearDay = Birthday End If CalcAge = DateDiff("yyyy", YearDay, Date) End Function This is independant of Date format. The first line of the routine checks if the birthday for this year has passed and add one year as applicable. Stuart
Report Offensive Follow Up For Removal
|

Post Locked
This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
Go to Programming Forum Home