Results 1 to 13 of 13

Thread: How to detect one date field value is later/greater than the other date field?

Hybrid View

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

    Re: How to detect one date field value is later/greater than the other date field?

    If you're using .NET then you should use .NET and stop pretending that you're still using VB6. If you have a teacher or a boss who still thinks it's the 1980s then I guess you're out of luck but, otherwise, join the rest of us in 2020. Here's the same ADO.NET and pure SQL solution I provided at Stack Overflow:
    vb.net Code:
    1. Dim sql = "SELECT COUNT (*)
    2.            FROM Docstable
    3.            WHERE (Date1 >= Date2 AND Date1 BETWEEN @DateFrom1 AND @DateTo1)
    4.            OR (Date2 > Date1 AND Date2 BETWEEN @DateFrom2 AND @DateTo2)"
    5. Dim dateFrom = DatePickerFrom.Value.Date
    6. Dim dateTo = DatePickerTo.Value.Date
    7. Dim count As Integer
    8.  
    9. Using connection As New OleDbConnection("connection string here"),
    10.       command As New OleDbCommand(sql, connection)
    11.     With command.Parameters
    12.         .Add("@DateFrom1", OleDbType.Date).Value = dateFrom
    13.         .Add("@DateFrom2", OleDbType.Date).Value = dateFrom
    14.         .Add("@DateTo1", OleDbType.Date).Value = dateTo
    15.         .Add("@DateTo2", OleDbType.Date).Value = dateTo
    16.     End With
    17.  
    18.     connection.Open()
    19.  
    20.     count = CInt(command.ExecuteScalar())
    21. End Using
    Last edited by jmcilhinney; Jun 4th, 2020 at 08:13 AM.
    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

  2. #2

    Thread Starter
    Member
    Join Date
    Apr 2019
    Posts
    54

    Re: How to detect one date field value is later/greater than the other date field?

    Quote Originally Posted by jmcilhinney View Post
    If you're using .NET then you should use .NET and stop pretending that you're still using VB6. If you have a teacher or a boss who still thinks it's the 1980s then I guess you're out of luck but, otherwise, join the rest of us in 2020. Here's the same ADO.NET and pure SQL solution I provided at Stack Overflow:
    vb.net Code:
    1. Dim sql = "SELECT COUNT (*)
    2.            WHERE (Date1 >= Date2 AND Date1 BETWEEN @DateFrom1 AND @DateTo1)
    3.            OR (Date2 > Date1 AND Date2 BETWEEN @DateFrom2 AND @DateTo2)"
    4. Dim dateFrom = DatePickerFrom.Value.Date
    5. Dim dateTo = DatePickerTo.Value.Date
    6. Dim count As Integer
    7.  
    8. Using connection As New OleDbConnection("connection string here"),
    9.       command As New OleDbCommand(sql, connection)
    10.     With command.Parameters
    11.         .Add("@DateFrom1", OleDbType.Date).Value = dateFrom
    12.         .Add("@DateFrom2", OleDbType.Date).Value = dateFrom
    13.         .Add("@DateTo1", OleDbType.Date).Value = dateTo
    14.         .Add("@DateTo2", OleDbType.Date).Value = dateTo
    15.     End With
    16.  
    17.     connection.Open()
    18.  
    19.     count = CInt(command.ExecuteScalar())
    20. End Using
    Thanks for your effort my friend but it still gives me an error: "Additional information: Syntax error (missing operator) in query expression 'COUNT (*) WHERE (Date1 >= Date2 AND Date1 BETWEEN @DateFrom1 AND @DateTo1) OR (Date2 > Dateto1 AND Dateto2 BETWEEN @DateFrom2 AND @DateTo2)'."

Tags for this Thread

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