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:
Function NextBday(ByVal aDOB As Date) As Date
Dim bDay As New Date(Date.Today.Year, aDOB.Month, aDOB.Day)
If bDay <= Date.Today Then
bDay = bDay.AddYears(1)
End If
Return bDay
End Function
Function GetBirthdays() As DataSet
Dim strCon As String = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"DATA Source=c:\data\info.mdb")
Dim dtDateStart As Date = Date.Today
Dim dtDateEnd As Date = dtDateStart.AddDays(30)
Dim conn As OleDbConnection = New OleDbConnection(strCon)
Dim strSelect As String = _
"Select Store, Name, bDate From UeosBday Where " & _
"bDate >=" & dtDateStart & " And " & _
"bdate <=" & dtDateEnd & " Order by bDate"
Dim Cmd As New OleDbDataAdapter(strSelect, conn)
Dim ds As New DataSet
Cmd.Fill(ds, "bDays")
With ds
' I am looking for a way to run every bDate in
' the ds through the NextBday function. The purpose
' of this is to find the next birthday of each employee
' in a 30 day period.
' For instance if a persons birthday is 9/12/1969 the
' function brings it current by making it 9/12/200x
' and then compares it to todays date. If todays date
' is later then the returned date it adds a year. If not
' it just returns the converted date (9/12/2006).
End With
' Is there a better way to do this then the way I am attempting it?
Return ds
End Function
Re: Datasets through a function and back
DataSets don't have columns. DataSets contain DataTables and DataRelations. DataTables contain DataColumns and DataRows.
VB Code:
Dim dob As Date
Dim nextBirthday As Date
Dim limit As Date = Date.Today.AddDays(30)
For Each row As DataRow In ds.Tables("bDays").Rows
dob = CDate(row("bDate"))
nextBirthday = dob.AddYears(Date.Today.Year - dob.Year)
If nextBirthday < Date.Today Then
nextBirthday.AddYears(1)
End If
If nextBirthday < limit Then
'This birthday is within the next thirty days.
End If
Next row
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?
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.