Search code examples
vb.netms-accessoledb

Missing operator in date time selection


 Dim rmfrom As Date
        rmfrom = DateTime.ParseExact(hapfromrm.Value, "dd/mm/yyyy", Nothing)
        Dim rmto As Date
        rmto = DateTime.ParseExact(hapfromrm.Value, "dd/mm/yyyy", Nothing)
        con.Open()
        cmd = New OleDbCommand("SELECT * FROM writes WHERE date_written BETWEEN " & rmfrom & " AND " & rmto & "", con)

It gives me the following error:

Syntax error (missing operator) in query expression 'date_written BETWEEN 1/2/2015 12:11:00 AM AND 1/2/2015 12:11:00 AM'.

What's the workaround? Any help will be greatly appreciated.


Solution

  • You need properly formatted string expressions for the dates:

    cmd = New OleDbCommand("SELECT * FROM writes WHERE date_written BETWEEN #" & rmfrom.ToString("yyyy'/'MM'/'dd") & "# AND #" & rmto.ToString("yyyy'/'MM'/'dd") & "#", con)