Results 1 to 8 of 8
  1. #1
    Philosophaie is offline Novice
    Windows Vista Access 2010 32bit
    Join Date
    Aug 2012
    Posts
    28

    Changing the date from Excel to Access

    I have in a cell in Excel: MM/DD/YYYY

    I want to add it to my Access database as: YYYY/MM/DD

    This is what I have so far in Excel VBA. It is giving me a date error. I have tried both as MM/DD/YYYY still did not work.



    Code:
        n=1
        accDateSub="01/01/2011"
        Dim conn As New ADODB.Connection
        conn.Open ConnectionString:="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=c:\path\file1.accdb;"
        thisSQL = "INSERT INTO [Table1] VALUES (" & n & ", #" & Format(accDateSub, "yyyy/mm/dd")  & "#);"
        conn.Execute CommandText:=thisSQL

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    Dates in Access date/time field are saved as a long integer. The integer translates to mm/dd/yyyy. You can apply formatting to the field to display date in other configurations but the data remains unchanged.

    Format function results in a string value, not a date or number but concatenation with # character should convert to date of mm/dd/yyyy and save. However, since you encounter issue, don't apply formatting to the import, just save the data and apply formatting in forms and reports.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    Philosophaie is offline Novice
    Windows Vista Access 2010 32bit
    Join Date
    Aug 2012
    Posts
    28
    Changed it to this but error"

    Data type mismatch in criteria expression.

    thisSQL = "INSERT INTO [Table1] VALUES (" & n & ", '" & accDateSub & "');"

  4. #4
    John_G is offline VIP
    Windows XP Access 2003
    Join Date
    Oct 2011
    Location
    Ottawa, ON (area)
    Posts
    2,615
    Hi -

    Your date string needs to delimited with "#", not single quotes:

    thisSQL = "INSERT INTO [Table1] VALUES (" & n & ", #" & accDateSub & "#);"

    In your original post, there are two things wrong.
    First, accDateSub = "01/01/2012" does not result in a date in accDateSub - accDateSub contains a string which happens to look like a date, but you cannot use it as a date. To put a date into accDateSub, use accDateSub = #01/01/2012#

    Once you do that, if you change your format to
    Format(accDateSub, "mm/yy/dddd")

    it should work.

    John
    Last edited by John_G; 08-21-2012 at 06:49 PM. Reason: correct the info

  5. #5
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    Yes, still need the # delimiter, just not the Format function.

    @John, "mm/yy/dddd" doesn't make sense, do you mean "mm/dd/yyyy"?
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  6. #6
    John_G is offline VIP
    Windows XP Access 2003
    Join Date
    Oct 2011
    Location
    Ottawa, ON (area)
    Posts
    2,615
    Hi June7 -

    Thanks; yes I do mean "mm/dd/yyyy" !!

    Must be an age thing... sigh!

    John

  7. #7
    Philosophaie is offline Novice
    Windows Vista Access 2010 32bit
    Join Date
    Aug 2012
    Posts
    28
    if you extract:

    accDateSub = cells(1,1)

    will it not come out as 1/1/2012 which throws off the "#" format making this not work:

    thisSQL = "INSERT INTO [Table1] VALUES (" & n & ", #" & accDateSub & "#);"


    It gives the error:

    Syntax error in date in query expression'#'.

  8. #8
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    The placeholder 0 should not be an issue.

    Sorry, should have looked at the entire SQL more carefully. If you don't name the fields, data will go into table left to right. Safest to name the fields. Otherwise, I don't see any syntax error.

    thisSQL = "INSERT INTO [Table1](fieldname1, fileldname2) VALUES (" & n & ", #" & accDateSub & "#);"

    What type is fieldname1?

    Also just hit me you are pushing this data to Access by code in Excel. Not something I have done. I've always had Access pull from Excel.

    Show the complete procedure. Want to provide the Excel for analysis? Attach file to post.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Changing combo-box to check box (a la Excel filter)
    By kagoodwin13 in forum Queries
    Replies: 3
    Last Post: 07-31-2012, 08:23 PM
  2. Updating date field in access from excel causes error
    By madamson86 in forum Programming
    Replies: 2
    Last Post: 12-14-2011, 02:38 AM
  3. Importing ever-changing Excel data
    By jtf1972 in forum Database Design
    Replies: 7
    Last Post: 08-05-2011, 11:24 AM
  4. Access date export to excel
    By jituknows in forum Access
    Replies: 1
    Last Post: 02-05-2011, 01:32 PM
  5. Changing data in excel from access
    By ricardo9211 in forum Import/Export Data
    Replies: 1
    Last Post: 08-26-2009, 01:46 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums