Below is a complete SUB I have currently in my code. The Specific area of issue is that the DETENTION area is not working. I consistently get back $0.00 as a result. The code runs through the 5th TRY statement, within the included code, 3-4 times in Debug showing one option (or CASE) being all true therefore should run. This works on initial run through then this SUB repeats itself 4x. On the 4th pass the previously all true case mysteriously develops one false statement which then bypasses because where all statements that were previously true, one goes false, resulting in a end result of $0.00 to be returned.

My coding skills are not top shelf and I am just not understanding why the following scenarios are occurring:
  1. Code runs through the 5th TRY statement 4 times, Why?
  2. What causes a previously all TRUE case statement to change to at least one false item on the 4th pass.
  3. Is there a better way to smooth this code out to make it work as intended?



The intended result is to look at the time elapsed between the scheduled date/time and the departure date/time. If that elapsed time is greater then 2 hours, then begin determining the amount of detention time AFTER the first two hours (in other words the first 2 hours after the scheduled date/time is "free" or not claimable for detention). This is to be done ONLY if the arrival date/time is PRIOR to or exactly at the scheduled appointment date/time (in other words if the truck is late to the appointment, then no detention can be claimed). Also one has to allow that the date could potentially roll over into the next date.

Code:
    Private Sub Detention_ValueChanged(sender As Object, e As EventArgs) Handles dtp01DepartDate.ValueChanged, dtp01DepartTime.ValueChanged, dtp90DepartDate.ValueChanged, dtp90DepartTime.ValueChanged

        'Dim c As Decimal
        Dim shipA As Date = (dtp01ArriveDate.Value.Date) + (dtp01ArriveTime.Value.TimeOfDay) ' 01 Arrival Date/Time
        Dim shipD As Date = (dtp01DepartDate.Value.Date) + (dtp01DepartTime.Value.TimeOfDay) ' 01 Departure Date/Time
        Dim shipT As Date = (dtp01PickDate.Value.Date) + (dtp01PickTime.Value.TimeOfDay)     ' 01 Scheduled P/U Date/Time
        Dim rcvrA As Date = (dtp90ArriveDate.Value.Date) + (dtp90ArriveTime.Value.TimeOfDay) ' 90 Arrival Date/Time
        Dim rcvrD As Date = (dtp90DepartDate.Value.Date) + (dtp90DepartTime.Value.TimeOfDay) ' 90 Departure Date/Time
        Dim rcvrT As Date = (dtp90DropDate.Value.Date) + (dtp90DropTime.Value.TimeOfDay)     ' 90 Scheduled Delivery Date/Time

        Dim arrive01 As TimeSpan = shipA.Subtract(shipT) ' 01 Arrival Date/Time - 01 Scheduled Time
        Dim arrive90 As TimeSpan = rcvrA.Subtract(rcvrT) ' 90 Arrival Date/Time - 90 Scheduled Time

        Dim depart01 As TimeSpan = shipD.Subtract(shipT) ' 01 Departure Date/Time - 01 Scheduled Date/Time
        Dim depart90 As TimeSpan = rcvrD.Subtract(rcvrT) ' 90 Scheduled Date/Time - 90 Departure Date/Time

        'Detention Due Box coloring - If at ship/Rcvr more than 2 hours
        Try
            If depart01.TotalMinutes >= 180 Then
                tb01.BackColor = Color.Red
            ElseIf depart01.TotalMinutes >= 120 AndAlso depart01.TotalMinutes < 180 Then
                tb01.BackColor = Color.Yellow
            ElseIf depart01.TotalMinutes < 120 Then
                tb01.BackColor = Color.Chartreuse
            End If
        Catch ex As Exception
        End Try

        ' Is Detention Due from Receiver?
        Try
            If depart90.TotalMinutes >= 180 Then
                tb90.BackColor = Color.Red
            ElseIf depart90.TotalMinutes >= 120 AndAlso depart90.TotalMinutes < 180 Then
                tb90.BackColor = Color.Yellow
            ElseIf depart90.TotalMinutes < 120 Then
                tb90.BackColor = Color.Chartreuse
            End If
        Catch ex As Exception
        End Try

        'Did truck arrive on time at Shipper?
        Try
            If arrive01.TotalMinutes >= 30 Then
                lblPUTime.BackColor = Color.Red
                lblPUTime.ForeColor = Color.White
            ElseIf arrive01.TotalMinutes >= 15 AndAlso depart01.TotalMinutes <= 29 Then
                lblPUTime.BackColor = Color.Yellow
                lblPUTime.ForeColor = Color.Black
            ElseIf arrive01.TotalMinutes >= 5 AndAlso depart01.TotalMinutes < 15 Then
                lblPUTime.BackColor = Color.Cyan
                lblPUTime.ForeColor = Color.Black
            ElseIf arrive01.TotalMinutes <= 4 Then
                lblPUTime.BackColor = Color.Transparent
                lblPUTime.ForeColor = Color.Black
            End If
        Catch ex As Exception
        End Try

        'Did truck arrive on time at Receiver?
        Try
            If arrive90.TotalMinutes >= 30 Then
                lblDroppedDate.BackColor = Color.Red
                lblDroppedDate.ForeColor = Color.White
            ElseIf arrive90.TotalMinutes >= 15 AndAlso depart90.TotalMinutes <= 29 Then
                lblDroppedDate.BackColor = Color.Yellow
                lblDroppedDate.ForeColor = Color.Black
            ElseIf arrive90.TotalMinutes >= 5 AndAlso depart90.TotalMinutes < 15 Then
                lblDroppedDate.BackColor = Color.Cyan
                lblDroppedDate.ForeColor = Color.Black
            ElseIf arrive90.TotalMinutes <= 4 Then
                lblDroppedDate.BackColor = Color.Transparent
                lblDroppedDate.ForeColor = Color.Black
            End If
        Catch ex As Exception
        End Try

        Dim c As Decimal

        Try
            Select Case True
                    'Lipsey Logistics
                Case tbBrokerAgency.Text = "LIPSEY LOGISITCS"
                    If depart01.TotalMinutes > 120 AndAlso arrive01.TotalMinutes <= 10 AndAlso depart90.TotalMinutes < 120 Then
                        c = CDec(Math.Max(CDec((depart01.TotalMinutes - 120) / 30) * 17.5, 0))
                        tbDetentionOwed.Text = c.ToString("C2")
                    ElseIf depart90.TotalMinutes > 120 AndAlso arrive90.TotalMinutes <= 10 AndAlso depart01.TotalMinutes < 120 Then
                        c = CDec(Math.Max(CDec((depart01.TotalMinutes - 120) / 30) * 17.5, 0))
                        tbDetentionOwed.Text = c.ToString("C2")
                    ElseIf depart01.TotalMinutes > 120 AndAlso arrive01.TotalMinutes >= 10 AndAlso depart90.TotalMinutes > 120 AndAlso arrive90.TotalMinutes >= 10 Then
                        c = CDec(Math.Max(CDec((depart01.TotalMinutes - 120) / 30) * 17.5, 0))
                        tbDetentionOwed.Text = c.ToString("C2")
                    Else
                        c = 0
                        tbDetentionOwed.Text = c.ToString("C2")
                    End If
                    'Total Quality Logistics
                Case tbBrokerAgency.Text = "TOTAL QUALITY LOGISTICS"
                    If depart01.TotalMinutes > 120 AndAlso arrive01.TotalMinutes <= 10 AndAlso depart90.TotalMinutes < 120 Then
                        c = CDec(Math.Max(CDec((depart01.TotalMinutes - 120) / 30) * 17.5, 0))
                        tbDetentionOwed.Text = c.ToString("C2")
                    ElseIf depart90.TotalMinutes > 120 AndAlso arrive90.TotalMinutes <= 10 AndAlso depart01.TotalMinutes < 120 Then
                        c = CDec(Math.Max(CDec((depart01.TotalMinutes - 120) / 30) * 17.5, 0))
                        tbDetentionOwed.Text = c.ToString("C2")
                    ElseIf depart01.TotalMinutes > 120 AndAlso arrive01.TotalMinutes >= 10 AndAlso depart90.TotalMinutes > 120 AndAlso arrive90.TotalMinutes >= 10 Then
                        c = CDec(Math.Max(CDec((depart01.TotalMinutes - 120) / 30) * 17.5, 0))
                        tbDetentionOwed.Text = c.ToString("C2")
                    Else
                        c = 0
                        tbDetentionOwed.Text = c.ToString("C2")
                    End If

                    'Coyote Logistics
                Case tbBrokerAgency.Text = "COYOTE"
                    If depart01.TotalMinutes > 120 AndAlso arrive01.TotalMinutes <= 10 AndAlso depart90.TotalMinutes < 120 Then
                        c = CDec((CDec((depart01.TotalMinutes - 120) / 30) * 17.5))
                        tbDetentionOwed.Text = c.ToString("C2")
                    ElseIf depart90.TotalMinutes > 120 AndAlso arrive90.TotalMinutes <= 10 AndAlso depart01.TotalMinutes < 120 Then
                        c = CDec((CDec((depart01.TotalMinutes - 120) / 30) * 17.5))
                        tbDetentionOwed.Text = c.ToString("C2")
                    ElseIf depart01.TotalMinutes > 120 AndAlso arrive01.TotalMinutes >= 10 AndAlso depart90.TotalMinutes > 120 AndAlso arrive90.TotalMinutes >= 10 Then
                        c = CDec((CDec((depart01.TotalMinutes - 120) / 30) * 17.5))
                        tbDetentionOwed.Text = c.ToString("C2")
                    Else
                        c = 0
                        tbDetentionOwed.Text = c.ToString("C2")
                    End If

                    'CH Robinson Logistics
                Case tbBrokerAgency.Text = "CH ROBINSON"
                    If depart01.TotalMinutes > 120 AndAlso arrive01.TotalMinutes <= 10 AndAlso depart90.TotalMinutes < 120 Then
                        c = Math.Max(CDec((depart01.TotalMinutes - 120) / 30) * 25, 0)
                        tbDetentionOwed.Text = c.ToString("C2")
                    ElseIf depart90.TotalMinutes > 120 AndAlso arrive90.TotalMinutes <= 10 AndAlso depart01.TotalMinutes < 120 Then
                        c = Math.Max(CDec((depart01.TotalMinutes - 120) / 30) * 25, 0)
                        tbDetentionOwed.Text = c.ToString("C2")
                    ElseIf depart01.TotalMinutes < 120 AndAlso arrive01.TotalMinutes >= 10 AndAlso depart90.TotalMinutes > 120 AndAlso arrive90.TotalMinutes >= 10 Then
                        c = Math.Max(CDec((depart01.TotalMinutes - 120) / 30) * 25, 0)
                        tbDetentionOwed.Text = c.ToString("C2")
                    Else
                        c = 0
                        tbDetentionOwed.Text = c.ToString("C2")
                    End If
            End Select

        Catch ex As Exception
            Dim frm As New MeMsgCalcError(ex, "'Detention' Calculation Error. Lines 1993-2021")
            frm.Show()
        End Try
    End Sub