CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Jul 2007
    Posts
    51

    Imported access file(date values) into excel sprdsheet converts to numbers(integer)

    Good morning everyone - I am using VB6.
    I need your assistance. I used the "CopyFromRecordset" function to populate my spreadsheet. Some of the fields in the access file has date values and some has number values (integers), but after importing into the spreadsheet, the numbers convert to date values(1/1/1900) and the date fields displays number values in the columns that was supposed to display dates.

    I have used the code below to try and fix the problem and the numbers displays correctly, but the date values are still displaying numbers (integers).
    My code:

    Code:
    Columns("A:A").Select
        xlApp.Selection.NumberFormat = "###"
        xlApp.Selection.Columns.AutoFit
    <==This works fine.

    Code:
    Columns("L:L").Select
        xlApp.Selection.NumberFormat = "mm/dd/yyyy"
        xlApp.Selection.Columns.AutoFit
    <==This does not work.
    Thanks.
    GiftX

  2. #2
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Imported access file(date values) into excel sprdsheet converts to numbers(integ

    Record a macro in Excel, and see what needs to be done.
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  3. #3
    Join Date
    Nov 2004
    Location
    LA. California Raiders #1 AKA: Gangsta Yoda™
    Posts
    616

    Re: Imported access file(date values) into excel sprdsheet converts to numbers(integ

    Make sure your columns are not already formatted with an incorrect formatting type for what you are expecting.

    Since you are using the .CopyFromRecordset method there isnt a whole lot you can do because its done internally and a macro wont show its process.

    You can do like you have done and code in the proper formatting of the columns.

    I would go with this code instead though in order to avoid using the Selection method in an effort to improve resource useage and speed.


    Code:
    xlApp.YourWorkbookObject.Sheets("Sheet1").Columns("A:A").NumberFormat = "###"
    xlApp.YourWorkbookObject.Sheets("Sheet1").Columns("A:A").AutoFit
    
    xlApp.YourWorkbookObject.Sheets("Sheet1").Columns("L:L").NumberFormat = "mm/dd/yyyy"
    xlApp.YourWorkbookObject.Sheets("Sheet1").Columns("L:L").AutoFit
    VB/Office Guru™ (AKA: Gangsta Yoda™)
    VB Forums - Super Moderator 2001-Present

    Microsoft MVP 2006-2011

    Please use [code]your code goes in here[/code] tags when posting code.

    Senior Software Engineer MCP, BSEE, CET
    VS 2012 Premium, VS 6.0 Enterprise SP6, VSTO, Office Ultimate 2010, Windows 7 Ultimate
    Star Wars Gangsta Rap SE Reputations & Rating Posts Office Primary Interop AssembliesAdvanced VB/Office Guru™ Word SpellChecker™.NETAdvanced VB/Office Guru™ Word SpellChecker™ VB6Outlook Global Address ListVB6/Crystal Report Ex.VB6/CR Print Setup Dialog Ex.

  4. #4
    Join Date
    Jul 2007
    Posts
    51

    Re: Imported access file(date values) into excel sprdsheet converts to numbers(integer)

    Thanks for your imput. I'll try it out on Monday.
    Meanwhile, you all enjoy your weekend.
    GiftX.

  5. #5
    Join Date
    Jul 2007
    Posts
    51

    Re: Imported access file(date values) into excel sprdsheet converts to numbers(integer)

    I tried using "DateFormat" in the code below, but I got error msg:
    "Object does not support this property".

    First, I tried this code:

    xlApp.xlWbk.Sheets("Sheet1").Columns("H:H").DateFormat = "mm/dd/yyyy"

    And I also tried this code:

    xlWksht.Range("G:G,H:H,K:K,L:L").Select
    Selection.DateFormat = "mm/dd/yyyy"

    In all the codes above, I used both "DateFormat" and "NumberFormat", but the date fields in access file still returns numbers in my spreadsheet.
    GiftX

  6. #6
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Imported access file(date values) into excel sprdsheet converts to numbers(integ

    Do your cells look like this?

    123107 7/8/2007 112206 8/8/2007

    I had no problem formatting that as Date
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured