Results 1 to 4 of 4

Thread: Datasets through a function and back

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2005
    Posts
    259

    Datasets through a function and back

    I want to add a column to my dataset that contains the returned bDay from the NextBday function. Is that possible. This may look familiar to some of you because I attempted to do something similar with ZipCodes a while back. However I never found a proper solution for that problem and used a quick hack to get by for the time.

    VB Code:
    1. Function NextBday(ByVal aDOB As Date) As Date
    2.         Dim bDay As New Date(Date.Today.Year, aDOB.Month, aDOB.Day)
    3.         If bDay <= Date.Today Then
    4.             bDay = bDay.AddYears(1)
    5.         End If
    6.         Return bDay
    7.     End Function
    8.  
    9.     Function GetBirthdays() As DataSet
    10.         Dim strCon As String = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
    11.             "DATA Source=c:\data\info.mdb")
    12.  
    13.         Dim dtDateStart As Date = Date.Today
    14.         Dim dtDateEnd As Date = dtDateStart.AddDays(30)
    15.         Dim conn As OleDbConnection = New OleDbConnection(strCon)
    16.         Dim strSelect As String = _
    17.             "Select Store, Name, bDate From UeosBday Where " & _
    18.             "bDate >=" & dtDateStart & " And " & _
    19.             "bdate <=" & dtDateEnd & " Order by bDate"
    20.  
    21.         Dim Cmd As New OleDbDataAdapter(strSelect, conn)
    22.         Dim ds As New DataSet
    23.         Cmd.Fill(ds, "bDays")
    24.  
    25.         With ds
    26.             ' I am looking for a way to run every bDate in
    27.             ' the ds through the NextBday function. The purpose
    28.             ' of this is to find the next birthday of each employee
    29.             ' in a 30 day period.
    30.  
    31.             ' For instance if a persons birthday is 9/12/1969 the
    32.             ' function brings it current by making it 9/12/200x
    33.             ' and then compares it to todays date. If todays date
    34.             ' is later then the returned date it adds a year. If not
    35.             ' it just returns the converted date (9/12/2006).
    36.         End With
    37.  
    38.         ' Is there a better way to do this then the way I am attempting it?
    39.  
    40.         Return ds
    41.  
    42.     End Function

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Datasets through a function and back

    DataSets don't have columns. DataSets contain DataTables and DataRelations. DataTables contain DataColumns and DataRows.
    VB Code:
    1. Dim dob As Date
    2.         Dim nextBirthday As Date
    3.         Dim limit As Date = Date.Today.AddDays(30)
    4.  
    5.         For Each row As DataRow In ds.Tables("bDays").Rows
    6.             dob = CDate(row("bDate"))
    7.             nextBirthday = dob.AddYears(Date.Today.Year - dob.Year)
    8.  
    9.             If nextBirthday < Date.Today Then
    10.                 nextBirthday.AddYears(1)
    11.             End If
    12.  
    13.             If nextBirthday < limit Then
    14.                 'This birthday is within the next thirty days.
    15.             End If
    16.         Next row
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2005
    Posts
    259

    Re: Datasets through a function and back

    I'm confused. How does that piece of code fit in with mine? Or is it meant to replace it?

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Datasets through a function and back

    That code loops through all rows in a DataTable and uses the value in the bDate column to determine the date of the next birthday after today and whether that birthday falls within the next thirty days. Isn't that what you want? It gets the person's date of birth and changes the year to the current year. If that date falls in the past it adds one year. That's the next birthday. It then tests whether that date is less than a date thirty days in the future. You can use that as a basis and adapt it to your own needs.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width