Problems with the age function

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Marren02
    Banned
    New Member
    • Oct 2007
    • 21

    Problems with the age function

    Hi, Whenever I try and find an age function I find that it doesn't work the way I want it to... Here's what I want to do: I want the age function to first ask the user a question ("What is your date of birth?") However, no matter how I formatted it the function would not work I have tried to use "as string" and DateDiff() but nothing seems to work... If anyone can come up with a method of doing this I'd appreciate it...
    kind regards,
    Marren02
  • Killer42
    Recognized Expert Expert
    • Oct 2006
    • 8429

    #2
    Can you show us what sort of code you have tried? Your description was pretty vague. Are you having trouble with getting the date from the user, or in calculating the age, or what?

    Oh, and what version of VB are you using?

    Comment

    • Marren02
      Banned
      New Member
      • Oct 2007
      • 21

      #3
      Originally posted by Killer42
      Can you show us what sort of code you have tried? Your description was pretty vague. Are you having trouble with getting the date from the user, or in calculating the age, or what?

      Oh, and what version of VB are you using?
      The version I am using is VB 6.0
      I have tried examples from the web which suprisingly don't work like:

      [CODE=vbnet]Public Function Age(ByVal Birthdate As System.DateTime ) As Long
      Try
      Dim CurrentDate As System.DateTime = System.DateTime .Today
      Select Case Month(Birthdate )
      Case Is < Month(System.Da teTime.Today)
      Age = DateDiff("YYYY" , Birthdate, Now())
      Case Is = Month(CurrentDa te)
      Select Case Day(Birthdate)
      Case Is < Day(CurrentDate )
      Age = DateDiff("YYYY" , Birthdate, Now())
      Case Is = Day(CurrentDate )
      Age = DateDiff("YYYY" , Birthdate, Now())
      Case Is > Day(CurrentDate )
      Age = DateDiff("YYYY" , Birthdate, Now()) - 1
      End Select
      Case Is > Month(CurrentDa te)
      Age = DateDiff("YYYY" , Birthdate, Now()) - 1
      Case Else
      Age = 0
      End Select
      Catch ex As System.Exceptio n
      'Error handling code does here
      End Try
      End Function[/CODE]
      which is "apparently " suppost to be code for VB 6.0 however the bits of code appear in red so i think this is someone who just likes to mess people around by saying its this version when it aint.

      Basically, I just simply want to know how old the user is in years by his/her birthdate or perhaps a more complicated format with the months, days, hours, etc. In General, it's just the whole code which doesn't work I've tried editing it but with my lack of knowledge in this program it just shot angry messages at me like as if the program hates me.

      I hope you manage to find a way to get round this, I have been trying to work out what is wrong but I can't seem to add in:

      Datenum1 As string

      Datenum1= LineIn("Please enter your date of birth in number form only")

      First time I tried doing this it did not work, so I tried if commands and other functions I could find but it persisted on coming up with messages that roughly translate as "Command not recognised" "Format incorrect" "End IF,etc. needed"
      "Need more ketchup...with my chips...user idiotic...can't come up with proper function..."

      Eventually I gave up so now the problem is passed to you perhaps i might learn from the code which you produce if you produce any... not saying that you are not capable of doing this but it seems impossible to find a website with simple straight forward code that works... I hope these forums differ from that.
      Kind regards,
      marren02
      Last edited by debasisdas; Oct 3 '07, 10:32 AM. Reason: Formatted using code tags.

      Comment

      • debasisdas
        Recognized Expert Expert
        • Dec 2006
        • 8119

        #4
        Originally posted by Marren02
        The version I am using is VB 6.0
        I have tried examples from the web which suprisingly don't work like:
        Before copying and pasting the code you need to understand that first.

        The code you are using is not VB 6.0 code ,that is .NET code.

        Comment

        • Marren02
          Banned
          New Member
          • Oct 2007
          • 21

          #5
          Then why on the website I used does it say it's Visual Basic code?
          CodeWidgets.com: Source Code Library And Software Developer Resources: C#, ASP.NET, VB.NET, Microsoft Access, Excel, JavaScript, JScript,JQuery, Website Design, Website Templates


          Look under widget info it says clearly in black and white under Microsoft Access 2000/XP Visual Basic 6.0

          Comment

          • hariharanmca
            Top Contributor
            • Dec 2006
            • 1977

            #6
            Originally posted by Marren02
            then why on the website I used does it say its visual basic code?
            Visual basic is common name to all versions of VB.
            Just try this code.

            [code=vb]

            Public Function GetAge(ByVal Birthdate As Date) As Long
            On Error GoTo errExc
            Dim CurrentDate As Date ' System.DateTime = System.DateTime .Today
            CurrentDate = Format(Now, "dd/MM/yyyy")
            Select Case Month(Birthdate )
            Case Is < Month(CurrentDa te)
            Age = DateDiff("YYYY" , Birthdate, Now)
            Case Is = Month(CurrentDa te)
            Select Case Day(Birthdate)
            Case Is < Day(CurrentDate )
            Age = DateDiff("YYYY" , Birthdate, Now)
            Case Is = Day(CurrentDate )
            Age = DateDiff("YYYY" , Birthdate, Now)
            Case Is > Day(CurrentDate )
            Age = DateDiff("YYYY" , Birthdate, Now) - 1
            End Select
            Case Is > Month(CurrentDa te)
            Age = DateDiff("YYYY" , Birthdate, Now) - 1
            Case Else
            Age = 0
            End Select
            Exit Function
            errExc:
            'Error handling code does here
            End Function


            [/code]

            which is converted to VB 6.0

            Comment

            • Marren02
              Banned
              New Member
              • Oct 2007
              • 21

              #7
              [code=vb]

              Public Function GetAge(ByVal Birthdate As Date) As Long
              On Error GoTo errExc
              Dim CurrentDate As Date ' System.DateTime = System.DateTime .Today
              CurrentDate = Format(Now, "dd/MM/yyyy")
              Select Case Month(Birthdate )
              Case Is < Month(CurrentDa te)
              Age = DateDiff("YYYY" , Birthdate, Now)
              Case Is = Month(CurrentDa te)
              Select Case Day(Birthdate)
              Case Is < Day(CurrentDate )
              Age = DateDiff("YYYY" , Birthdate, Now)
              Case Is = Day(CurrentDate )
              Age = DateDiff("YYYY" , Birthdate, Now)
              Case Is > Day(CurrentDate )
              Age = DateDiff("YYYY" , Birthdate, Now) - 1
              End Select
              Case Is > Month(CurrentDa te)
              Age = DateDiff("YYYY" , Birthdate, Now) - 1
              Case Else
              Age = 0
              End Select
              Exit Function
              errExc:
              'Error handling code does here
              End Function


              [/code]

              The code is accepted in my visual basic but when I try to run the script nothing is displayed... there must be something else you need to do with the code for it to run properly

              Comment

              • hariharanmca
                Top Contributor
                • Dec 2006
                • 1977

                #8
                Can you explain how you are calling this method?

                Comment

                • debasisdas
                  Recognized Expert Expert
                  • Dec 2006
                  • 8119

                  #9
                  Originally posted by Marren02
                  then why on the website I used does it say its visual basic code?
                  Ask that at the site from where you have copied the code.

                  Comment

                  • Marren02
                    Banned
                    New Member
                    • Oct 2007
                    • 21

                    #10
                    h person
                    ----------------------------
                    what do you mean?

                    Other person
                    -----------------------------------
                    Don't think theres a way to contact this website

                    --------------------------------------------------------------------
                    I'm trying to get a code whereby the user simply enters his or her date of birth and basically it will display his or her age and may also have other things added like months, days, etc.

                    I'd appreciate if anyone can give me a code that works as I can't find any site with a code that will allow me to work out age with vitual basic

                    Kind regards,
                    marren02

                    Comment

                    • hariharanmca
                      Top Contributor
                      • Dec 2006
                      • 1977

                      #11
                      That is a simple thing to get age. i don't know why you are using such big code?

                      Do some validation for DOB and use dateDiff() method.

                      Comment

                      • Killer42
                        Recognized Expert Expert
                        • Oct 2006
                        • 8429

                        #12
                        Originally posted by Marren02
                        ... The code is accepted in my visual basic but when I try to run the script nothing is displayed... there must be something else you need to do with the code for it to run properly
                        Yes. You need to set Option Explicit. Then VB will warn you that variable "Age" doesn't exist.

                        I always recommend keeping the "require explicit variable declaration" option turned on in VB, as it prevents many problems of this sort. )On the other hand, if it's actually VBScript that we're talking about, I don't know whether that's possible.)

                        Here's a corrected and simplified version...
                        [CODE=vb]Public Function GetAge(ByVal Birthdate As Date) As Long
                        GetAge = DateDiff("YYYY" , Birthdate, Now)
                        If Month(Birthdate ) = Month(Now) Then
                        If Day(Birthdate) > Day(Now) Then
                        GetAge = GetAge - 1
                        End If
                        ElseIf Month(Birthdate ) > Month(Now) Then
                        GetAge = GetAge - 1
                        End If
                        End Function[/CODE]

                        Comment

                        • Marren02
                          Banned
                          New Member
                          • Oct 2007
                          • 21

                          #13
                          Ok... we got something right here but now we got a problem

                          Below is the code for the form
                          [CODE=vb]Private Sub Form_Activate()
                          GetAge = DateDiff("YYYY" , Birthday, Now)
                          Birthday = LineIn("Please enter your date of birth in number form only e.g. 20/07/91")
                          If Month(Birthday) = Month(Now) Then
                          If Day(Birthday) > Day(Now) Then
                          GetAge = GetAge - 1
                          End If
                          ElseIf Month(Birthday) > Month(Now) Then
                          GetAge = GetAge - 1
                          End If
                          Display ("You are " & GetAge & " Years old")
                          End Function
                          Private Sub txtDisplay_KeyP ress(KeyAscii As Integer)
                          Call KBDBuffer(KeyAs cii)
                          End Sub[/CODE]

                          And here we got the module:

                          [CODE=vb]Dim Buffer As String
                          Public Sub KBDBuffer(Ascii Code As Integer)
                          If AsciiCode = 8 And Len(Buffer) > 0 Then
                          Buffer = Left(Buffer, Len(Buffer) - 1)
                          Else
                          Buffer = Buffer + Chr(AsciiCode)
                          End If
                          End Sub
                          Public Function InKey() As String
                          Do
                          DoEvents
                          Loop Until Len(Buffer) > 0
                          InKey = Right(Buffer, 1)
                          Buffer = ""
                          frmVisualBasic. txtDisplay.Text = Left(frmVisualB asic.txtDisplay .Text, Len(frmVisualBa sic.txtDisplay. Text) - 1)
                          End Function
                          Public Function LineIn(Prompt As String) As String
                          Dim EndOfText As Boolean

                          If Prompt <> "" Then
                          Display (Prompt & " ;")
                          End If
                          Do
                          DoEvents
                          If Len(Buffer) > 0 Then
                          EndOfText = (Asc(Right(Buff er, 1)) = 13)
                          Else
                          EndOfText = False
                          End If
                          Loop Until EndOfText
                          LineIn = Left(Buffer, Len(Buffer) - 1)
                          Buffer = ""
                          End Function
                          Public Sub Display(OutputT ext As String)
                          If Right(OutputTex t, 1) = ";" Then
                          OutputText = Left(OutputText , Len(OutputText) - 1)
                          Else
                          OutputText = OutputText & vbCrLf
                          End If
                          frmVisualBasic. txtDisplay.Text = frmVisualBasic. txtDisplay.Text + OutputText
                          frmVisualBasic. txtDisplay.SelS tart = Len(frmVisualBa sic.txtDisplay)
                          End Sub
                          Public Sub ClearScreen()
                          frmVisualBasic. txtDisplay.Text = ""
                          End Sub[/CODE]

                          Now the problem is no matter what I put as the date it comes up with "you are 108 years old" - obviously there must be something else missing either in the module or the form hope this helps and sorry I haven't replied in a while.
                          Kind regards,
                          Marren02
                          Last edited by Killer42; Oct 5 '07, 10:55 PM.

                          Comment

                          • brunzire
                            New Member
                            • Aug 2007
                            • 9

                            #14
                            for simplicity you could do a datediff and return the No of Months then

                            Age = int(noofmonths/12) which will return the integer part ie the age.

                            You dont really need a function for this code one line will do

                            TheAge = int(Datedif(mon th,EnterDate, Now) /12)

                            Comment

                            • Marren02
                              Banned
                              New Member
                              • Oct 2007
                              • 21

                              #15
                              Originally posted by brunzire
                              for simplicity you could do a datediff and return the No of Months then

                              Age = int(noofmonths/12) which will return the integer part ie the age.

                              You dont really need a function for this code one line will do

                              TheAge = int(Datedif(mon th,EnterDate, Now) /12)
                              [CODE=vb]Private Sub Form_Activate()
                              EnterDate = LineIn("Please enter your date of birth in number form only e.g. 20/07/1991")
                              TheAge = Int(DateDiff(Mo nth, EnterDate, Now) / 12)
                              Display ("You are " & TheAge & " Years old")
                              End Sub
                              Private Sub txtDisplay_KeyP ress(KeyAscii As Integer)
                              Call KBDBuffer(KeyAs cii)
                              End Sub[/CODE]

                              This code does not work. It says that the argument (being the month) is not correct. I tried editing this a bit but it just says the whole code is incorrect. If a code with one line was that simple I wouldn't be in this mess. So of course that weren't going to work. Too good to be true really ;)
                              Last edited by Killer42; Oct 5 '07, 10:58 PM.

                              Comment

                              Working...