Results 1 to 5 of 5

Thread: [2005] MS Access date problem

  1. #1

    Thread Starter
    Addicted Member Genom's Avatar
    Join Date
    May 2006
    Posts
    186

    [2005] MS Access date problem

    Hi;
    My software tries to write and read date from ms access table. In the MS Access format is DD.MM.YYYY HH.mm.ss. Is there any date format in VB.Net which is like this? How can I write on this?
    I used a code like this:
    INSERT INTO TabelName Values(...., DD.MM.YYYY HH.mm.ss,...) and
    INSERT INTO TabelName Values(...., 'DD.MM.YYYY HH.mm.ss',...)
    but none of them worked
    What can i do?
    Dim Me As Coder

  2. #2
    Addicted Member
    Join Date
    Mar 2006
    Posts
    235

    Re: [2005] MS Access date problem

    Access requires a date to be delimited by the # sign. For example #1/4/2005#.

    Try
    VB Code:
    1. "#" & Format(inDate, "yyyy-MM-dd hh:mm:ss") & "#"

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

    Re: [2005] MS Access date problem

    You shouldn't be worrying about format at all. Format is ONLY an issue when you are converting to or from a string, which you should NOT be doing. Keep your date object as date objects at ALL times, e.g.:
    VB Code:
    1. Dim myCommand As New OleDbCommand("INSERT INTO MyTable (ID, [Date]) VALUES (@ID, @Date)", myConnection)
    2.  
    3. myCommand.Parameters.AddWithValue("@ID", myID)
    4. myCommand.Parameters.AddWithValue("@Date", myDate)
    No need to worry about format at all because a binary date object has no format. Just one more reason to ALWAYS use parameters when building SQL statements with variables.
    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

  4. #4

    Thread Starter
    Addicted Member Genom's Avatar
    Join Date
    May 2006
    Posts
    186

    Re: [2005] MS Access date problem

    I have tried lots of things. Format of date is dd.mm.yyyy hh:mm:ss I put # both sides too
    INSERT INTO Stok VALUES (2, 'Rulman', 'f', 'fh', 5, 5, 'f', 'fs', #18.02.2007 17:02:55#, 'bf')
    this is my string. Everything passes except date. It is identical to MS Access date. I mean as adding or viewing a date I see the same format but still it doesnt work. If I cant find any soluiton I will use string for date.
    Dim Me As Coder

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

    Re: [2005] MS Access date problem

    OK, I say again: dates have NO format. The format you see is only how Access DISPLAYS a date, not how it is STORED. Date objects are always STORED the same way, regardless of how they are DISPLAYED. If you want to insert a literal date then you must represent it as #MM/dd/yyyy HH:mm:ss#. That literal value that you type will be interpreted as and converted to the appropriate binary date value, which will then be stored in Access and displayed to you using whatever format you've specified.

    Now, having said that, I very much doubt that you're going to writing all literal values to insert like that. More likely each field value is going to come from a variable. I've shown you how to handle that in my previous post.
    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

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