CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13

Hybrid View

  1. #1
    Join Date
    Feb 2009
    Location
    Portland, OR
    Posts
    1,488

    Inserting a date into MS Jet Database

    Hi everyone:

    I'm getting conflicting results from my search, so I'd appreciate if someone can clear this out for me. I need to insert a date & time value from COleDateTime variable into a Microsoft Jet Database. I use the following SQL query for that:
    Code:
    INSERT INTO `table name` (`date field name`)  VALUES ('#yyyy-mm-dd hh:mm:ss#')
    I can't seem to find a consensus whether it should be dash or a slash, or whether it should start with a year or a month? I obviously want to code this independent of a user's locale.

    And second question, say if COleDateTime is not valid, how do I form the SQL date in that case?

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Inserting a date into MS Jet Database

    What are you using to connect to MS Access?

  3. #3
    Join Date
    Feb 2009
    Location
    Portland, OR
    Posts
    1,488

    Re: Inserting a date into MS Jet Database

    Quote Originally Posted by Arjay View Post
    What are you using to connect to MS Access?
    CDao* classes from an MFC project.

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,398

    Re: Inserting a date into MS Jet Database

    One of my friends, Microsoft Access MVP, said me last year that Microsoft Access & Jet works very good with the datetime values passed in US format. And it should work for every used locale.
    Victor Nijegorodov

  5. #5
    Join Date
    Feb 2009
    Location
    Portland, OR
    Posts
    1,488

    Re: Inserting a date into MS Jet Database

    OK, I got most of it to work. Here's my findings in case someone has a similar situation.

    First let me tell you that MS again decided to stand out from the crowd and stepped away from a conventional SQL standard in their MS Jet engine. Here's what I mean:

    1. The date value should not be included in ' ' like one would do for string literals. Instead one should enclose it into # # only, which I'm sure stems from the VB conventions.

    2. I'm still not clear how to format the date. All of the following formats seems to work, and I can't find a consensus on which one should be used as a norm. And unfortunately I cannot test it on any locale other than the US one.
    Code:
    #mm/dd/yyyy#
    #yyyy/mm/dd#
    #yyyy-mm-dd#
    Example of SQL statement:
    Code:
    INSERT INTO `my table` (`my date`) VALUES (#2011-03-20#)
    3. The MS Jet's SQL DATE parameter can be represented by either only date part, only time part or as both date and time. Again, the exact formatting is not clear to me. It also seems like all the dates are stored as text in the database so maybe there's such a variation.

    4. To store an invalid date (for instance from the COleDateTime member) simply assign it to Null (without # # delimiters). Example of SQL statement:
    Code:
    INSERT INTO `my table` (`my date`) VALUES (Null)

  6. #6
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Inserting a date into MS Jet Database

    Quote Originally Posted by ahmd View Post
    OK, I got most of it to work. Here's my findings in case someone has a similar situation.

    First let me tell you that MS again decided to stand out from the crowd and stepped away from a conventional SQL standard in their MS Jet engine.
    I've never had any problems saving or reading dates in Access. Then again, I'm not using obsolete and deprecated technology like CDao* classes.

    Use current technologies and reduce the risk of problems - it's funny how that works.

  7. #7
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,398

    Re: Inserting a date into MS Jet Database

    Quote Originally Posted by ahmd View Post
    ...
    And unfortunately I cannot test it on any locale other than the US one.
    Code:
    #mm/dd/yyyy#
    #yyyy/mm/dd#
    #yyyy-mm-dd#
    Well, the #mm/dd/yyyy# must work (according to the information I got from MS Access MVP)
    The #yyyy-mm-dd# must work too because it is the locale independable format for dates.
    I can say nothing about the #yyyy/mm/dd# format, sorry.

    For Access 97/2000 (and I guess for XP/2003 also) you can pass as a datetime the double value representing datetime in COleDatetime class (just because the datetime representation in MS Access and in COleDatetime was the same)
    I don't know how it was realized in 2007/2010 thogh.
    Victor Nijegorodov

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

Featured