Re: Multiple date formats in a Table

From: Andrew Smith (andydsmith_at_ntlworld.invalid)
Date: 04/07/04

  • Next message: John Nurick: "Re: Multiple date formats in a Table"
    Date: Wed, 7 Apr 2004 22:00:52 +0100
    
    

    >From what you say I assume that you currently have the dates stored in a
    text field - is that correct?

    Access stores dates as numbers where 1 represents 31 Dec 1899 so today is
    stored as (7 Apr 2004) 38084. If you change your text(?) field to a date
    field then I think that Access will do its best to convert the text into a
    date. However, text that it does not recognise as a date will get discarded.
    So 2004 or Apr-2004 could well get lost.

    In any case, a date field could not store just the year 2004 - it would have
    to store a specific date in that year. You could decide to store it as 1 Jan
    2004, but then you would not be able to tell if that really meant 1 Jan 2004
    or just 2004. When it came to formatting the date you would either have to
    display it as 1 Jan 2004, or you would have to just display the year for all
    the dates in the database.

    So, for table storage purposes you are probably best leaving things as they
    are.

    However, for sorting then you do need to convert your text into real dates
    in some way. You will have to decide how you want to sort a value of 2004,
    or Apr-2004. Realistically you only have two options - treat them as if they
    were on the first or last of the year/ month.

    The conversion of your existing text format date should be done in a query,
    leaving the original text in place in the table. This will allow you to sort
    by the converted value, but display the original text so you don't then have
    any problems with formating the dates in reports etc. The big problem, of
    course, is how you convert the text into a date. I suspect you will have to
    write a VBA function to do this. How easy this is will depend on how
    consistent the date entries are in your table.

    If they are all either; a recognisable date (eg 1-Apr-2004, or 1/4/04), or a
    year (eg 2004), or a month and year in the format Apr-2004, then it should
    be fairly simple. The following code should work in this case - it will
    return the first of the year/ month for partial dates, the full date for
    complete dates or 1 Jan 1900 for unrecognised dates. This is a simple
    example, and could be extended to recognise more options if your data is
    less consistent than this.

    Public Function TextToDate(strDate As String) As Date

    Dim strYear As String
    Dim strMonth As String

    If IsDate(strDate) Then
        TextToDate = strDate
    ElseIf IsNumeric(Right(strDate, 4)) Then
        'Last 4 digits are numeric, so assume it is the year
        If Len(strDate) = 4 Then
            TextToDate = DateSerial(CLng(strDate), 1, 1)
        ElseIf Len(strDate) = 8 Then
            strYear = CLng(Right(strDate, 4))
            strMonth = Left(strDate, 3)
            TextToDate = CDate("01-" & strMonth & "-" & strYear)
        Else
            'Unrecognised format - treat all these cases as 1 Jan 1900
            TextToDate = DateSerial(1900, 1, 1)
        End If
    Else
        'Unrecognised format - treat all these cases as 1 Jan 1900
        TextToDate = DateSerial(1900, 1, 1)
    End If

    End Function

    "harvestman" <anonymous@discussions.microsoft.com> wrote in message
    news:195b101c41c7b$3e7ea9a0$a401280a@phx.gbl...
    > I have a quite large Access db with about 6,000 records,
    > each with 32 fields divided into three tables. I'm having
    > a problem with dates in one of my tables, especially since
    > I'm trying to get the dates listed in chronological order.
    >
    > My db relates to my Uni thesis here in Oz, recording
    > occurrences of invertebrate species with dates running
    > from 1883 to 2004. I have reconfigured my short date under
    > Regional Settings Properties to read as: dd-mmm-yyyy
    > (e.g., 07-Apr-2004). However, the problem I have is the
    > fact that for about a quarter of my records I only have
    > a "mmm-yyyy" date or "yyyy" date and have no date
    > information at all for about a third of my records.
    >
    > If I select "Date/Time" as my data type in the field I
    > have shown for the Collection Date, I lose all my "mmm-
    > yyyy" and "yyyy" dates. Similarly, under the General Tab
    > for Field Properties, in the first property: "Format", I
    > can only select a single (particular) date style
    > e.g., "short date", rather than the three date style
    > values I want to select... i.e.,
    > dd-mmm-yyyy e.g., 07-Apr-2004
    > mmm-yyyy e.g., Apr-2004
    > yyyy e.g., 2004
    >
    > So if anyone can advise me here, it would be much
    > appreciated... and in the instances where I have no date
    > info, can someone please advise if I should leave the
    > value for this field blank, or write something like "null"
    > or "unknown".
    >
    > Many thanks...


  • Next message: John Nurick: "Re: Multiple date formats in a Table"

    Relevant Pages

    • Re: Displaying dates in dd/mm/yy format
      ... the short date also contains time information. ... You can however format the string to shortformat. ... Can I somehow force Access to store the short ...
      (microsoft.public.dotnet.framework.adonet)
    • Re: SQL query to format datetime
      ... The data is not stored with any particular format - the format ... If you simply want to display 1/25/2005 when retrieving the datetime ... If you want to change the actual value, and store just the date, ... >Can I ask you a simple SQL query question? ...
      (microsoft.public.sqlserver.programming)
    • Re: Date Time Variant NOT read when loading CSV
      ... Time format ... General to see that XL's parsed the string correctly - the two colons ... To correct the display, ... > In a CSV Excel reads most things OK ...
      (microsoft.public.excel.misc)
    • Re: Radio buttons values other than 1 or 2 added in table
      ... I guess under the object that has to display the Mr. / Mrs. ... Do you mean the format at the field properties ... The display control is Combo Box ... OptionGroup frames will only store numeric values. ...
      (comp.databases.ms-access)
    • Newbie: SQL string construction
      ... I need to construct a string at runtime which will get the data. ... I *only* want to read and store the data, ... If I use a datareader how will I display them and consequently store them in ... I dont need to worry about memory limitations and/or access time. ...
      (microsoft.public.dotnet.languages.vb)