Results 1 to 9 of 9
  1. #1
    RayMilhon is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    Aug 2011
    Location
    Southern California
    Posts
    1,071

    Converting a string to date/time

    I have a large text file to be imported into Access 2010. Several of the fields are date/time fields. The text file formatting of the date/time is as follows:

    20120412092134

    I can take this string and convert it so that it looks like this

    04/12/2012 09:21:34

    but when I use any of the builtin conversion functions I normally use all I actually get is the date the time gets stripped off. Is there a builtin conversion function for this that I'm not finding or do I need to build one myself?

  2. #2
    RayMilhon is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    Aug 2011
    Location
    Southern California
    Posts
    1,071
    Never mind right after I posted this I retried the CVDate function and it worked as desired. realized when I tried it before I had a typo and that's what caused it not to work.

  3. #3
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Check out the DateSerial() and TimeSerial() functions, using Mid() to pull out the appropriate parts. Adding the results together should get you the complete date/time.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  4. #4
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Oops, too slow. Didn't realize CDate() could handle that string.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    RayMilhon is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    Aug 2011
    Location
    Southern California
    Posts
    1,071
    Not CDATE CVDate Used the Mid Function to break the string down to the individual components then used CVDate with the components in the right order to translate back to a date time.

    Here's the function
    the original string is 20120412092134
    the result of the below function is 04/12/2012 9:21:34 AM

    Code:
    Public Function convertdate(byval strdate as string) as date
        Dim stryear as string, strmonth as string, strday as string, strhour as string, strmin as string, strsec as string
        stryear = left(strdate,4)
        strmonth = mid(strdate,5,2)
        strday = mid(strdate,7,2)
        strhour = mid(strdate,9,2)
        strmin = mid(strdate,11,2)
        strsec = mid(strdate,13,2)
       converdate = CVDATE(strmonth & "/" & strday & "/" & stryear & " " & strhour & ":" & strmin & ":" & strsec)
    End Function

  6. #6
    RayMilhon is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    Aug 2011
    Location
    Southern California
    Posts
    1,071
    The typo I had where I thought the CVDate wasn't the correct function is I'd left out the space between stryear and strhour so the string it was trying to convert was

    04/12/201209:21:34 which gave a type mismatch I mistakenly thought it was because the time was included. after posting here I noticed there was no space between the 2012 and 09 so I made sure the space was included and viola it worked.

  7. #7
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Wasn't familiar with CVDate, but it appears to be functionally equivalent to CDate:

    ?cdate("04/12/2012 09:21:34")
    4/12/2012 9:21:34 AM
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  8. #8
    RayMilhon is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    Aug 2011
    Location
    Southern California
    Posts
    1,071
    Guess I was doing something wrong in CDate as well every time I tried it I only got the date not the time

  9. #9
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    I was actually surprised it handled the time, which is why I suggested the other functions earlier. Learn something new every day.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

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

Similar Threads

  1. Replies: 12
    Last Post: 02-22-2011, 03:39 PM
  2. Converting a date to length of time
    By Duncan in forum Access
    Replies: 2
    Last Post: 11-10-2010, 05:53 AM
  3. Replies: 2
    Last Post: 07-03-2010, 08:45 PM
  4. Split string into Date and Time Columns?
    By Hobbes29 in forum Queries
    Replies: 2
    Last Post: 06-08-2010, 06:50 PM
  5. Converting/Viewing OLE Data as String...
    By oldgem in forum Access
    Replies: 0
    Last Post: 09-12-2009, 06:35 PM

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