visual basic 6.0 simple age calculator

  • (2 Pages)
  • +
  • 1
  • 2

23 Replies - 36879 Views - Last Post: 02 July 2011 - 01:57 PM Rate Topic: -----

#1 naser123456  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 11-December 08

visual basic 6.0 simple age calculator

Posted 15 February 2009 - 07:46 PM

hi

actualy i have to do the simple age calculator programme in visual basic 6.0 that allows the user to enter two different date of birth and calculate their average age!!!!!!!!!!!!!!!!!!!!!!!!!!

it's very hard for me because i'm just a beignner and i can't handle this task. if anyone code help me i would really appriciate it.

many thanks

Is This A Good Question/Topic? 0
  • +

Replies To: visual basic 6.0 simple age calculator

#2 bsaunders  Icon User is offline

  • D.I.C Addict

Reputation: 44
  • View blog
  • Posts: 571
  • Joined: 18-January 09

Re: visual basic 6.0 simple age calculator

Posted 15 February 2009 - 08:11 PM

Can you post what you have so far?
Was This Post Helpful? 0
  • +
  • -

#3 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1641
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: visual basic 6.0 simple age calculator

Posted 15 February 2009 - 10:51 PM

[rules][/rules]
Was This Post Helpful? 0
  • +
  • -

#4 luigiplumbersinc  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 82
  • Joined: 16-February 09

Re: visual basic 6.0 simple age calculator

Posted 16 February 2009 - 03:11 AM

mmmmm

make 2 textboxes a label and a button

in form code put...

private sub button1_Click()

intYear = Year(Now) ' Current year
averageAge = ((intYear - val(textbox1.text)) + (intYear - val(textbox2.text))) / 2

label1.caption = str(averageAge) 'its not reaal pretty but the maths is clear i guess...

end sub

as a note this post contains no more info than a google search provides in the previews of site content...

This post has been edited by luigiplumbersinc: 16 February 2009 - 03:12 AM

Was This Post Helpful? 0
  • +
  • -

#5 naser123456  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 11-December 08

Re: visual basic 6.0 simple age calculator

Posted 17 February 2009 - 05:48 AM

View Postbsaunders, on 15 Feb, 2009 - 07:11 PM, said:

Can you post what you have so far?



hi dear


actualy this task is very hard to do for me but i have to . what i have so far it's very simple and itn't working
i put to textbox for date of birth of two person and named txtname1 and txtname2 and also i put cmdcalculate and txttotalaverage

in the private sub cmdcalculate_Click() i put the following code:

private sub cmdcalculate_Click()

dim txttotalaverage as integer

txttotalaverage = format((date-cdate(txtname1)) +(date-cdate(txtname2))/2

end sub

but this is not working i really confused i don't know what should i do . i would appreciate it if you could help me in this matter
best regards
Was This Post Helpful? 0
  • +
  • -

#6 luigiplumbersinc  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 82
  • Joined: 16-February 09

Re: visual basic 6.0 simple age calculator

Posted 17 February 2009 - 07:50 PM

View Postnaser123456, on 17 Feb, 2009 - 04:48 AM, said:

View Postbsaunders, on 15 Feb, 2009 - 07:11 PM, said:

Can you post what you have so far?



hi dear


actualy this task is very hard to do for me but i have to . what i have so far it's very simple and itn't working
i put to textbox for date of birth of two person and named txtname1 and txtname2 and also i put cmdcalculate and txttotalaverage

in the private sub cmdcalculate_Click() i put the following code:

private sub cmdcalculate_Click()

dim txttotalaverage as integer

txttotalaverage = format((date-cdate(txtname1)) +(date-cdate(txtname2))/2

end sub

but this is not working i really confused i don't know what should i do . i would appreciate it if you could help me in this matter
best regards


firstly i suppose i should tell you what they keep telling me post your code between code and /code tags you know like it says in the big yellow sqaure when your posting that has the heading "READ THIS FIRST:"

anyway whatever method you use to find the different between their birthdate and the current date as u did above
txttotalaverage = format((date-cdate(txtname1)) +(date-cdate(txtname2))/2



if that is giving you the answer you want you can find out for yourself by adding to our code an actual way of viewing the result and actually using the contents of the text boxes as the dates instead of their control ID's "txtname1.text"


private sub cmdcalculate_Click() 

txttotalaverage.text = str(format((date-cdate(txtname1.text)) +(date-cdate(txtname2.text))/2

end sub




however since it is a "simple" age calculator you may as well just input a year of birth into each
and use the following code

private sub cmdcalculate_Click() 

' yyyy meaning the year in 4 digits e.g: 2009
'Mid(Date, 6) should take omit the days and months and just grab the end of the current date yyyy (2009)
txttotalaverage.text = Str((DateDiff("yyyy", txtname1.text, Mid(Date, 6)) + DateDiff("yyyy", txtname2.text, Mid(Date, 6))) / 2)

end sub



goodluck :D

This post has been edited by luigiplumbersinc: 17 February 2009 - 07:51 PM

Was This Post Helpful? 0
  • +
  • -

#7 naser123456  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 11-December 08

Re: visual basic 6.0 simple age calculator

Posted 18 February 2009 - 02:08 AM

hi dear i really appreciate it i could done my home work many thanks but there is one problem just i want to set the date in the format dd/mm/yyyy and i don't want to allows the user to enter invalid date of birth i have this but it's not working :

private sub cmdcalculate_click()

if IsDate(Txt3.Text) then
   Txt3.Text = Format(Txt3.text, "dd/mm/yyyy")
Else
  MsgBox ("Wrong Date"), vbCritical
  
Elseif IsDate(Txt3.Text) then
   Txt3.Text = Format(Txt3.text, "dd/mm/yyyy")
Else
  MsgBox ("Wrong Date"), vbCritical

End If





bt it's not working ?!!!!!!!!!!!!!
best regards

View Postnaser123456, on 18 Feb, 2009 - 01:06 AM, said:

hi dear i really appreciate it i could done my home work many thanks but there is one problem just i want to set the date in the format dd/mm/yyyy and i don't want to allows the user to enter invalid date of birth i have this but it's not working :

private sub cmdcalculate_click()

if IsDate(Txt3.Text) then
   Txt3.Text = Format(Txt3.text, "dd/mm/yyyy")
Else
  MsgBox ("Wrong Date"), vbCritical
  
Elseif IsDate(Txt4.Text) then
   Txt4.Text = Format(Txt4.text, "dd/mm/yyyy")
Else
  MsgBox ("Wrong Date"), vbCritical

End If





bt it's not working ?!!!!!!!!!!!!!
best regards

Was This Post Helpful? 0
  • +
  • -

#8 luigiplumbersinc  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 82
  • Joined: 16-February 09

Re: visual basic 6.0 simple age calculator

Posted 18 February 2009 - 02:19 AM

your if structure is a bit odd it may work but i rewrote it a lil...

private sub cmdcalculate_click()

if IsDate(Txt3.Text) then
   Txt3.Text = Format(Txt3.text, "dd/mm/yyyy")
Else
  MsgBox ("Wrong Date"), vbCritical
End If



if your IsDate isnt working properly then you can try a custom version to determine whether its a date yourself

If Len(Txt3.Text) = 10 Then
  If IsNumeric(Mid(Txt3.Text, 1, 2)) And IsNumeric(Mid(Txt3.Text, 4, 2)) And IsNumeric(Mid(Txt3.Text, 7)) Then
	Txt3.Text = Format(Txt3.Text, "dd/mm/yyyy")
  Else
	MsgBox ("Invalid Date!, non-numeric input"), vbCritical
  End If
Else
  MsgBox ("Invlid Date!, date length incorrect please format dd/mm/yyyy"), vbCritical
End If



hope it helps, Goodluck :D

This post has been edited by luigiplumbersinc: 18 February 2009 - 02:21 AM

Was This Post Helpful? 0
  • +
  • -

#9 naser123456  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 11-December 08

Re: visual basic 6.0 simple age calculator

Posted 18 February 2009 - 07:41 AM

[hi lumbersinc

thank you very mach you are a really helpful person thank you very mach . but the code you have sent me earlier is not workind ?! :/
Was This Post Helpful? 0
  • +
  • -

#10 luigiplumbersinc  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 82
  • Joined: 16-February 09

Re: visual basic 6.0 simple age calculator

Posted 18 February 2009 - 04:46 PM

View Postnaser123456, on 18 Feb, 2009 - 06:41 AM, said:

[hi lumbersinc

thank you very mach you are a really helpful person thank you very mach . but the code you have sent me earlier is not workind ?! :/


well i have never used the format function... and i dont own VB to test the code... what error are you having?
Was This Post Helpful? 0
  • +
  • -

#11 naser123456  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 11-December 08

Re: visual basic 6.0 simple age calculator

Posted 18 February 2009 - 08:09 PM

hi lumbersic thank you in advance for your co operation to help me to do this course but the code that you have gave me it's not working. some time it says else without if and when i correcvt it the calculation won't work . and some times is working but as soon as i click in the text box i mean txt3.text the message box will appear when i clik ok to dis appear the message bog again when i enter the number the message box will appear again and it gave me alot of headache i don't know ehat should i do? <_<
Was This Post Helpful? 0
  • +
  • -

#12 luigiplumbersinc  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 82
  • Joined: 16-February 09

Re: visual basic 6.0 simple age calculator

Posted 18 February 2009 - 10:40 PM

ok well can you perhaps post your entire form code so i and perhaps others can find your problem
Was This Post Helpful? 0
  • +
  • -

#13 naser123456  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 11-December 08

Re: visual basic 6.0 simple age calculator

Posted 19 February 2009 - 12:01 PM

hi lumbersinc
i hope you are alright.

this is my project entire code :

Private Sub cmdCalculate_Click()
lblAverage.ForeColor = vbBlue
lblresult.ForeColor = vbBlue


If Len(Txt3.Text) = 10 Then
  If IsNumeric(Mid(Txt3.Text, 1, 2)) And IsNumeric(Mid(Txt3.Text, 4, 2)) And IsNumeric(Mid(Txt3.Text, 7)) Then
	Txt3.Text = Format(Txt3.Text, "dd/mm/yyyy")
  Else
	MsgBox ("Invalid Date!, non-numeric input"), vbCritical
  End If
Else
  MsgBox ("Invlid Date!, date length incorrect please format dd/mm/yyyy"), vbCritical
End If



If txtname1.Text = "" Or txtname2.Text = "" Or Txt3.Text = "" Or Txt4.Text = "" Then
	MsgBox "Please complete all the fields", vbExclamation + vbOKOnly, "Error Message"
Else

c = (txtname1.Text)
d = (txtname2.Text)
adate = Txt4.Text
bdate = Txt3.Text
curdate = Now
lblresult.Caption = Format(DateDiff("yyyy", bdate, curdate) + DateDiff("yyyy", adate, curdate)) / 2 & Space(1) & "years"
lblAverage = c & Space(1) & "and" & Space(1) & d & Space(1) & " have an average age of"
End If
End Sub

Private Sub cmdClear_Click()
txtname1.Text = ""
txtname2.Text = ""
Txt3.Text = ""
Txt4.Text = ""
lblresult.Caption = ""
lblAverage.Caption = ""
End Sub

Private Sub cmdExit_Click()
Response = MsgBox("Exit The Project?", vbInformation + vbYesNo, "Are you sure?")
		If Response = vbYes Then
			Unload Me
		ElseIf Response = vbNo Then
			Me.Refresh
		End If
End Sub

Private Sub Form_Load()

Dim c As String
Dim d As String
Dim e As String
Dim curdate As Date
Dim bdate As Date
Dim adate As Date
End Sub




thank you very mach
Was This Post Helpful? 0
  • +
  • -

#14 luigiplumbersinc  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 82
  • Joined: 16-February 09

Re: visual basic 6.0 simple age calculator

Posted 19 February 2009 - 08:05 PM

i just built a project using your code and adding the appropriate buttons labels and text boxes it works fine i have attatched a screen shot of the VB project... im sat school atm so i have access to VB...


the only problem you need to fix is that you only used the code i sent you to determine whether the text box contains a date for Txt3 and not Txt4

please open the attatchment

Attached image(s)

  • Attached Image

Was This Post Helpful? 0
  • +
  • -

#15 naser123456  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 11-December 08

Re: visual basic 6.0 simple age calculator

Posted 20 February 2009 - 04:45 AM

hi lumbersinc thank you very mach in advance finaly i could do it and i have finished with my project but i have another project to do if i get any problem i'll tell you .

best regards
good luck
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2