+ Reply to Thread
Results 1 to 10 of 10

Module to convert field to a date format unsuccessful

  1. #1
    Registered User
    Join Date
    04-27-2010
    Location
    USA
    MS-Off Ver
    Excel 2002
    Posts
    14

    Module to convert field to a date format unsuccessful

    I have a table in Access that contains a field which was setup as a 'Number' type field instead of a 'Date' type field and as a result, it displays the dates as 91205, 121409, etc. In trying to change the format of the dates so that it displays them as 9/12/2005, 12/14/2009, etc.,the module I had created in Access only converts some of the dates to the desired format. The dates that did manage to convert successfully were 5 digits, as none of the 6 digit dates converted. The query that calls the function is as follows: [codebox]SELECT QQQF_SPTRN0201.ST1ST & "-" & QQQF_SPTRN0201.ST2ST AS Expr1, QQQF_SPTRN0201.TRNCST, QQQF_SPTRN0201.TRNDST, NumToDate([TRNDST]) AS TrndstDate, QQQF_SPTRN0201.QTYST
    FROM QQQF_SPTRN0201
    WHERE (((QQQF_SPTRN0201.TRNCST)="D"))
    ORDER BY QQQF_SPTRN0201.ST1ST;[/codebox]

    The code that converts the field is as follows:
    [codebox]Function NumToDate(lInput As Integer) As Date
    Dim strTemp As String

    strTemp = Format(lInput, "000000")
    NumToDate = DateSerial(Val(Right(strTemp, 2)), Val(Left(strTemp, 2)), Val(Mid(strTemp, 3, 2)))
    End Function[/codebox]

    If there's a better way to do the date conversion, please let me know as I've even tried a different approach to the problem by trying to export the query results to Excel, but I'm not sure how to get it to work.

    Any suggestions/ideas to this dilemma are welcomed.

    Thanks!

  2. #2
    Forum Contributor
    Join Date
    04-23-2009
    Location
    IOWA
    MS-Off Ver
    2010 Professional
    Posts
    270

    Re: Module to convert field to a date format unsuccessful

    WARNING!!!!! BACK UP ALL DATA PRIOR TO ATTEMPTING THIS!!!

    I just did a very quick test, what I did was convert the Number field to text first - which should have 0 data loss. Then I inserted / in the appropriate spots, ie 31099 became 3/10/99 in text format, i tried various lengths and they worked. Then after they all had the appropriate "/"s in them I converted it to date type short date format. It worked great.

    To get the slashes I did the following:

    Please Login or Register  to view this content.

    Hope this helps,

    Dan
    "I am not a rocket scientist, I am a nuclear engineer." - Split_atom18
    If my advice has been helpful to you, then please help me by clicking on the "Star" and adding to my reputation, Thanks!

  3. #3
    Registered User
    Join Date
    04-27-2010
    Location
    USA
    MS-Off Ver
    Excel 2002
    Posts
    14

    Re: Module to convert field to a date format unsuccessful

    Thanks Dan for the feedback. I'll give that a try (after I make a good backup of the data)...was there anything out of the ordinary in the code you used to convert the number type to text? I'm not an expert in coding, but I understand enough to cause trouble..lol

    Thanks again!

    Steve

  4. #4
    Forum Contributor
    Join Date
    04-23-2009
    Location
    IOWA
    MS-Off Ver
    2010 Professional
    Posts
    270

    Re: Module to convert field to a date format unsuccessful

    Steve,

    I opened the table in design view and then changed it from number to text in the drop down menu, in Access 2007. I didn't use code, since I assumed it would be a one time ordeal.

    V/R

    Dan

  5. #5
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Module to convert field to a date format unsuccessful

    ready2drum,

    Please fix the code tags in your first post.
    Entia non sunt multiplicanda sine necessitate

  6. #6
    Registered User
    Join Date
    04-27-2010
    Location
    USA
    MS-Off Ver
    Excel 2002
    Posts
    14

    Re: Module to convert field to a date format unsuccessful

    Thanks for the heads up...I updated the code tags....another obstacle that I noticed is that because it's a 'linked' table, Access won't allow me to change the property for a linked table (which is the error message I received when I tried to change the field type from 'number' to 'text'.....the initial warning message stated that the 'table is a linked table with some properties that can't be modified'. If I can get past that hurdle, my next question would be where would I drop the code statement that adds the '/' to the date field? would it replace this code statement?
    Please Login or Register  to view this content.
    Thanks for your help!


    Here's the script to convert the field to a date format....
    Please Login or Register  to view this content.

  7. #7
    Forum Contributor
    Join Date
    04-23-2009
    Location
    IOWA
    MS-Off Ver
    2010 Professional
    Posts
    270

    Re: Module to convert field to a date format unsuccessful

    Is this going to be something you have to do often or once?

  8. #8
    Registered User
    Join Date
    04-27-2010
    Location
    USA
    MS-Off Ver
    Excel 2002
    Posts
    14

    Re: Module to convert field to a date format unsuccessful

    Thankfully, it just has to happen once.

  9. #9
    Forum Contributor
    Join Date
    04-23-2009
    Location
    IOWA
    MS-Off Ver
    2010 Professional
    Posts
    270

    Re: Module to convert field to a date format unsuccessful

    I would open the database where the linked table is stored and change it there. If you can't do that and won't be using that linked table in the future create a new table from it in your database. Do a query with a make table that copies all the records.

    Hope this helps,

    Dan

  10. #10
    Registered User
    Join Date
    04-27-2010
    Location
    USA
    MS-Off Ver
    Excel 2002
    Posts
    14

    Re: Module to convert field to a date format unsuccessful

    That's a great idea...thanks Dan! It'll be easier to create the new table and set things up so that I'm not having to change anything to do with the linked tables.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1