Results 1 to 8 of 8
  1. #1
    GregTheSquarePeg is offline Novice
    Windows 7 64bit Access 2013
    Join Date
    Sep 2013
    Posts
    16

    CDate function to convert from dd.mm.yyyy format

    Hello,



    I have a linked text file that imports all fileds as text type. I want to use CDate (or possibly CVDate?) to convert the text dates to a real date format (not just to look like dates)

    My text dates are in this format: dd.mm.yyyy
    I would like to convert them to dates like: dd/mm/yyyy

    When I use criteria such as: CDate([FieldName]) - the function fails to convert the dates.
    My query is appending to a 'template' table that has the relevant field set to the Date/Time format.

    Any idea's?

    Many thanks,
    Gregg

  2. #2
    Kirtap is offline Novice
    Windows 7 64bit Access 2010 32bit
    Join Date
    Oct 2013
    Posts
    22
    When I try convertedDate = CDate(Format("03.04.1988", "mm/dd/yyyy")), I get a Type Mismatch Error.
    If your dates are text, might I recommend a Replace function to replace the /'s.
    So it'd be like CDate(Format(Replace("03.04.1988", "mm/dd/yyyy"),".","/"))?
    When I did a message box, this worked.
    Last edited by Kirtap; 10-11-2013 at 05:45 AM. Reason: Corrected Replace, my bad.

  3. #3
    GregTheSquarePeg is offline Novice
    Windows 7 64bit Access 2013
    Join Date
    Sep 2013
    Posts
    16
    Hi Kirtap,

    Thanks for taking the time to look at this.

    So following your suggestion, we have: CDate(Format(Replace([Created on],"mm/dd/yyyy"),".","/"))

    Although this comes back with error: The expression you entered has a function containing the wrong number of arguments

    Could it be because I'm using a field name ([Created on]) rather than an actual dd.mm.yyyy string in the first criteria?

    Thanks,
    Gregg

  4. #4
    Kirtap is offline Novice
    Windows 7 64bit Access 2010 32bit
    Join Date
    Oct 2013
    Posts
    22
    Wrong number of arguements? I think I might've posted things wrong.

    CDate(Format(Replace([Created On],".","/"), "mm/dd/yyyy"))

    My bad, that's what happens when I copy my text wrong, edit it multiple times. lol

    Edit: Also, if its a field name, maybe you need to add .Value?

  5. #5
    Bob Fitz's Avatar
    Bob Fitz is offline Access Developer
    Windows XP Access 2003
    Join Date
    May 2011
    Location
    Essex UK
    Posts
    3,544
    Gregg
    I have a linked text file that imports all fileds as text type. I want to use CDate (or possibly CVDate?) to convert the text dates to a real date format (not just to look like dates)

    My text dates are in this format: dd.mm.yyyy
    I would like to convert them to dates like: dd/mm/yyyy
    Access does not store dates in either format, rather as a number. The CDate function converts anything that access can read as a date into such a number. The help files say:
    Any expression that can be interpreted as a date, including date literals, numbers that look like dates, strings that look like dates, and dates returned from functions. A date expression is limited to numbers or strings, in any combination, that can represent a date from January 1, 100 – December 31, 9999.
    Dates are stored as part of a real number. Values to the left of the decimal represent the date; values to the right of the decimal represent the time. Negative numbers represent dates prior to December 30, 1899.
    So CDate will convert a value to a numeric value. How you see that value (dd.mm.yyyy or dd/mm/yyyy or mm/dd/yyyy etc) is only about formatting.
    If this helped, please click the star at the bottom left of this posting and add to my reputation . Many thanks.
    Bob Fitzpatrick

  6. #6
    GregTheSquarePeg is offline Novice
    Windows 7 64bit Access 2013
    Join Date
    Sep 2013
    Posts
    16
    Kirtap - thank you very much, that works a treat (although I was confused for a second - you changed the date format to mm/dd in your reply, serves me right for being lazy, copying and pasting rather than editing my own expression myself!)
    Thanks again for your help.

    Bob - thanks for the info - I had read the help section, but struggled slightly to process Microsofts explanation. I better understand it now.

    Cheers both, keep up the good work.

    Gregg

  7. #7
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows XP Access 2003
    Join Date
    Aug 2013
    Posts
    7,862
    Gregg, just some FYI

    As long as the text you are placing into your field with data type "Date" is in the correct order, i.e. mm/dd/yy, there should not be a problem. If your SQL grabs text that is in a different order, I believe this is a problem.

    For example, Oct 1, 2013 may look like this: 01.10.2013

    Paste 01/10/2013 into a date field and you will get Jan 10, 2013. Access determines this as American format.

    Paste 30/01/2013 into a date field and you will get Jan 30, 2013 because the day is >12.

    Example 2 in this Allen Browne link touches on the subject of international date formatting.
    http://allenbrowne.com/ser-36.html

  8. #8
    GregTheSquarePeg is offline Novice
    Windows 7 64bit Access 2013
    Join Date
    Sep 2013
    Posts
    16
    Thanks ItsMe, well explained. Will have a look at the link too.

    Gregg

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

Similar Threads

  1. Can access format a date in an odd way? DD-MMM-YYYY
    By redbull in forum Programming
    Replies: 3
    Last Post: 08-23-2013, 11:34 AM
  2. UK Date Format In Linked Table ("dd/mm/yyyy")
    By smoothlarryhughes in forum Queries
    Replies: 1
    Last Post: 06-18-2013, 08:29 PM
  3. Replies: 4
    Last Post: 03-12-2013, 06:49 PM
  4. Convert to MMM/YYYY
    By Brian62 in forum Queries
    Replies: 10
    Last Post: 08-31-2009, 04:54 PM
  5. Replies: 1
    Last Post: 06-11-2009, 05:40 PM

Tags for this Thread

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