Results 1 to 11 of 11
  1. #1
    Jerseynjphillypa is offline Advanced Beginner
    Windows Vista Access 2007
    Join Date
    Apr 2012
    Posts
    83

    Convert Number to Date/Time

    Currently I have a field called DateEnter as a number, but I am looking to convert it to a Date/Time.

    The number is enter the following way: 201003 --> yyyymm but I want to show up as Date/Time as mm/yyyy.

    I tried to play with the formatting,



    Format([DateEnter],"mm/yyyy") in the query but that didn't work

  2. #2
    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() function, along with the Mid() function to grab the two different components (you'll need to hard-code a day).
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    RayMilhon is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    Aug 2011
    Location
    Southern California
    Posts
    1,067
    If your talking about a permanent conversion that is saved in your table Paul's is the method to use. However if you're talking about doing the conversion in a query for display on a report or form you can do it this way: In a module add the following


    Code:
    Public function dateconversion(Byref lngvalue as Long) As string
        Dim tmpmonth as string
        Dim tmpyear as string
        If len(trim(str(lngvalue)))=6 Then
            tmpmonth = Right(trim(str(lngvalue)),2)
            If tmpmonth <1 or tmpmonth > 12
                    dateconversion = "Invalid Month"
            Else 
                  ' here you may want to do some validation similar to the month as
                  '(Personal Assumption here)  years greater than 2200 probably aren't valid and depending on how far back your data goes years prior should
                  'be validated as well
                  tmpyear = left(trim(str(lngvalue)),4)
                  dateconversion = tmpmonth & "/" & tmpyear
            End if
        Else
             dateconversion = ""
        End if
    End function

  4. #4
    Jerseynjphillypa is offline Advanced Beginner
    Windows Vista Access 2007
    Join Date
    Apr 2012
    Posts
    83
    Thanks for the help

    So I did was
    Mid([DateEnter],5,2) & "/" & Mid([DateEnter],1,4)

    But if the DateEnter field is blank, it still shows "/". How can I remove the / or do the commend only when there is a Date.

    Would I use ISNA?

  5. #5
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    You understand that's not converting it to date/time, just displaying it differently? You can test the field with IIf() and IsNumeric() and only use your function when appropriate.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  6. #6
    Jerseynjphillypa is offline Advanced Beginner
    Windows Vista Access 2007
    Join Date
    Apr 2012
    Posts
    83
    I tried to do the following expression so I can get it into a Date/Time

    DateSerial(Mid([DateEnter],5,2)) but i get the error "The expression you entered has a function containing the wrong number of arguments

  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
    Did you look in help at the DateSerial() function? It requires all 3 components of a date.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  8. #8
    Jerseynjphillypa is offline Advanced Beginner
    Windows Vista Access 2007
    Join Date
    Apr 2012
    Posts
    83
    yes...but what if I don't know the day and want to leave that part blank

  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
    Then you can't use a date/time data type, as there's no such thing as a date without a day. I might just put a 1 in for the day for every record.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  10. #10
    RayMilhon is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    Aug 2011
    Location
    Southern California
    Posts
    1,067
    Which is why I made the suggestion I made there was no day in the original post. which means you can leave it as numeric or change it to text. Which is what my function does. The function can be used to convert the current numeric field into a text field I'm just not sure why you would want to do that when the data already exists you're simply changing the way it looks.

  11. #11
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    But the original request was "I am looking to convert it to a Date/Time", which presumably meant data type.
    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: 2
    Last Post: 11-22-2011, 11:45 AM
  2. Replies: 1
    Last Post: 08-07-2011, 07:58 AM
  3. Number convert to date
    By Argus in forum Programming
    Replies: 1
    Last Post: 08-05-2009, 02:21 PM
  4. how to convert week number to date range
    By qwang1115 in forum Access
    Replies: 1
    Last Post: 02-13-2009, 11:35 AM
  5. date/time number
    By playfuljade in forum Forms
    Replies: 4
    Last Post: 12-19-2005, 12:52 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