Error when comparing a date in an access database

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jim H

    #1

    Error when comparing a date in an access database

    I'm trying to get a bunch of records based on client id and a date range. I
    keep getting an error when I enclose my date string in quotes in the where
    cleause.

    The error is:
    Microsoft JET Database Engine: Data type mismatch in criteria expression.

    If I remove the quotes and use MyDateVar.ToSho rtDateString the select
    executes but ignores the date in the resulting dataset. I get all dates.

    Here is my select as it goes into the OleDbDataAdapte r:
    select * from TableA where ClientId = 1 and RecDate between '7/15/2003
    12:00:00 AM' and '7/15/2003 11:59:59 PM'

    This select gives me the above error. I get the error even if I use
    ToShortDateStri ng if the date is quoted. I never had a problem with this
    using VS.NET2002 and SqlClients.

    This is VS.NET2003 and an Access database.

    Any help or insight would be greatly appreciated.

    Thanks,
    Jim


  • Chris R. Timmons

    #2
    Re: Error when comparing a date in an access database

    "Jim H" <jim@nospam.now here.com> wrote in
    news:OiFqlKzSDH A.3768@tk2msftn gp13.phx.gbl:
    [color=blue]
    > I'm trying to get a bunch of records based on client id and a
    > date range. I keep getting an error when I enclose my date
    > string in quotes in the where cleause.
    >
    > The error is:
    > Microsoft JET Database Engine: Data type mismatch in criteria
    > expression.
    >
    > If I remove the quotes and use MyDateVar.ToSho rtDateString the
    > select executes but ignores the date in the resulting dataset.
    > I get all dates.
    >
    > Here is my select as it goes into the OleDbDataAdapte r:
    > select * from TableA where ClientId = 1 and RecDate between
    > '7/15/2003 12:00:00 AM' and '7/15/2003 11:59:59 PM'
    >
    > This select gives me the above error. I get the error even if I
    > use ToShortDateStri ng if the date is quoted. I never had a
    > problem with this using VS.NET2002 and SqlClients.
    >
    > This is VS.NET2003 and an Access database.[/color]

    Jim,

    http://msdn.microsoft.com/library/en.../acfundsql.asp

    In MS Access SQL, wrap date literals with #:

    select * from TableA where ClientId = 1 and RecDate between
    #7/15/2003 12:00:00 AM# and #7/15/2003 11:59:59 PM#

    If you use OleDbParameters , you don't have to provide the # wrapping:

    string sql =
    "select * from TableA where ClientId = 1 and RecDate " +
    "between @firstDate and @secondDate";

    OleDbParameter firstDateParam =
    new OleDbParameter( "@firstDate ", OleDbType.DBDat e);
    firstDateParam. Value = new DateTime(2003, 15, 7, 12, 0, 0);
    firstDateParam. Direction = ParameterDirect ion.Input;

    // Similar code for @secondDate...

    OleDbCommand command = new OleDbCommand(sq l, connection);
    command.Paramet ers.Add(firstDa teParam);
    command.Paramet ers.Add(secondD ateParam);


    Hope this helps.

    Chris.
    -------------
    C.R. Timmons Consulting, Inc.

    Comment

    • John Baro

      #3
      Re: Error when comparing a date in an access database

      Another thing you might want to keep in mind is the Universal Date / Time
      Format
      Being
      yyyy-mm-dd hh:mm:ss
      (24 hour clock)
      This has solved datetime problems in the past for me. Wrapping it in single
      quotes should not only be acceptable but necessary for access.
      HTH
      JB
      "Jim H" <jim@nospam.now here.com> wrote in message
      news:OiFqlKzSDH A.3768@tk2msftn gp13.phx.gbl...[color=blue]
      > I'm trying to get a bunch of records based on client id and a date range.[/color]
      I[color=blue]
      > keep getting an error when I enclose my date string in quotes in the where
      > cleause.
      >
      > The error is:
      > Microsoft JET Database Engine: Data type mismatch in criteria expression.
      >
      > If I remove the quotes and use MyDateVar.ToSho rtDateString the select
      > executes but ignores the date in the resulting dataset. I get all dates.
      >
      > Here is my select as it goes into the OleDbDataAdapte r:
      > select * from TableA where ClientId = 1 and RecDate between '7/15/2003
      > 12:00:00 AM' and '7/15/2003 11:59:59 PM'
      >
      > This select gives me the above error. I get the error even if I use
      > ToShortDateStri ng if the date is quoted. I never had a problem with this
      > using VS.NET2002 and SqlClients.
      >
      > This is VS.NET2003 and an Access database.
      >
      > Any help or insight would be greatly appreciated.
      >
      > Thanks,
      > Jim
      >
      >[/color]


      Comment

      Working...