Dim thisMonth As Integer
            DateCollected = DataSet.Repair.Rows(2).Item("DateCollected")
            thisMonth = Month(Now)

For........
            If Month(DateCollected) = thisMonth Then
                partsprofit = (partswholesalerpricetotal) / (partstotalcost) * 100
            Else
            End If
Next

Basically i am writing up some code that loops through all the records in a table looking at a column called 'Datecollected'.
it checks to see whether the date collected is in the same month as the current month . for example say the date collected is 26/04/2010 and the current date is today(23/04/2010) then datecollected is in the same date where as if the current date was 12/05/2010 it wouldnt.
i am trying to come up with some code that can check the two dates, any advice or help would be appreciated

Something like this.

Private Sub CompareMonths()
   'I don't know what DataSet.Repair is or does.

   Dim currentMonth As Integer = DateTime.Now.Month
   Dim parseMonth As Integer = 0
   Dim parseDate As DateTime = Nothing

   ' Iterate all rows in table
   For Each row As DataRow In DataSet.Tables(0).Rows
      ' Check if field is NULL
      If Not IsDBNull(row("DateCollected")) Then
         ' Try to parse the date from the current row
         If DateTime.TryParse(row("DateCollected"), parseDate) Then
            ' Retrieve the month part of the date
            parseMonth = parseDate.Month
            ' Compare the two months
            If currentMonth.Equals(parseMonth) Then
               'Do something
            End If
         End If
      End If
   Next
End Sub

Something like this.

Private Sub CompareMonths()
   'I don't know what DataSet.Repair is or does.

   Dim currentMonth As Integer = DateTime.Now.Month
   Dim parseMonth As Integer = 0
   Dim parseDate As DateTime = Nothing

   ' Iterate all rows in table
   For Each row As DataRow In DataSet.Tables(0).Rows
      ' Check if field is NULL
      If Not IsDBNull(row("DateCollected")) Then
         ' Try to parse the date from the current row
         If DateTime.TryParse(row("DateCollected"), parseDate) Then
            ' Retrieve the month part of the date
            parseMonth = parseDate.Month
            ' Compare the two months
            If currentMonth.Equals(parseMonth) Then
               'Do something
            End If
         End If
      End If
   Next
End Sub

I have come up with this as i wasnt really sure what to do with tryparse etc (kinda new to me) and its still not working.....it keeps saying the labourprofit is 0 even though there are two rows that have the same month (in this case april)

Dim increment As Integer
        Dim DateDue As Date
        Dim labourhours As Integer
        Dim overheads As Integer = 12.5
        Dim labourprofit As Integer
        Dim thisMonth As Integer = DateTime.Now.Month

        MaxRows = KiwicyclesDataSet.Repair.Rows.Count

        For increment = 0 To MaxRows - 1
            DateDue = KiwicyclesDataSet.Repair.Rows(increment).Item("Date Due")
            labourhours = KiwicyclesDataSet.Repair.Rows(increment).Item("NoOfLabourHours") * (30)
            If Month(DateDue) = thisMonth Then
                labourprofit = labourprofit + ((overheads) / (labourhours) * 100)
            Else
            End If
        Next
        MsgBox("This months labour profit is " & labourprofit)
    End Sub

TryParse takes two arguments.
The first argument is the date to parse.
The second argument is a referenced variable that will contain the parsed date.
TryParse returns a boolean value indicating if the parsing was successful. And if True, the second argument will contain the parsed date.

Try this:

Private Sub CompareMonths()
   Dim labourhours As Integer
   Dim overheads As Integer = 12.5
   Dim labourprofit As Integer

   Dim currentMonth As Integer = DateTime.Now.Month
   Dim parseMonth As Integer = 0
   Dim DateDue As DateTime = Nothing

   ' Iterate all rows in table
   For Each row As DataRow In KiwicyclesDataSet.Repair.Rows
      ' Check if field is NULL
      ' Must use brackets if the field contains spaces
      If Not IsDBNull(row("[Date Due]")) Then 
         ' Try to parse the date from the current row
         If DateTime.TryParse(row("[Date Due]"), DateDue) Then
            ' Retrieve the month part of the date
            parseMonth = DateDue.Month
            labourhours = row.Item("NoOfLabourHours") * (30)
            ' Compare the two months
            If currentMonth.Equals(parseMonth) Then
               labourprofit = labourprofit + ((overheads) / (labourhours) * 100)
            End If
         End If
      End If
   Next
End Sub

It still returns the labour profit as 0. i am not sure whether it is the format of the date because in the table in the database the format is DD/MM/YYYY

TryParse takes two arguments.
The first argument is the date to parse.
The second argument is a referenced variable that will contain the parsed date.
TryParse returns a boolean value indicating if the parsing was successful. And if True, the second argument will contain the parsed date.

Try this:

Private Sub CompareMonths()
   Dim labourhours As Integer
   Dim overheads As Integer = 12.5
   Dim labourprofit As Integer

   Dim currentMonth As Integer = DateTime.Now.Month
   Dim parseMonth As Integer = 0
   Dim DateDue As DateTime = Nothing

   ' Iterate all rows in table
   For Each row As DataRow In KiwicyclesDataSet.Repair.Rows
      ' Check if field is NULL
      ' Must use brackets if the field contains spaces
      If Not IsDBNull(row("[Date Due]")) Then 
         ' Try to parse the date from the current row
         If DateTime.TryParse(row("[Date Due]"), DateDue) Then
            ' Retrieve the month part of the date
            parseMonth = DateDue.Month
            labourhours = row.Item("NoOfLabourHours") * (30)
            ' Compare the two months
            If currentMonth.Equals(parseMonth) Then
               labourprofit = labourprofit + ((overheads) / (labourhours) * 100)
            End If
         End If
      End If
   Next
End Sub

Managed to solve the problem now, thanks! :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.