Results 1 to 12 of 12
  1. #1
    Lorlai is offline Competent Performer
    Windows Vista Access 2007
    Join Date
    Jun 2011
    Posts
    107

    Date field not in date format

    I have an excel spreadsheet that is being imported into access. I have many date fields included in this spreadsheet. For some reason, there are a few fields that do not show up properly in access. For example, I have a date in the excel file (08/25/2008) that shows up in access as 39685. Why is this happening?

    When I create some vba code to change the date format in the excel, it works out just fine. My next problem is that I have had to hard code these specific fields in, and while the code works great for the first field, when the second field appears, the entire sheet is transferred into date format.
    Code:
    If ActiveCell.Value = "Project First Date" Then
                ActiveCell.Value = Replace(ActiveCell.Value, " ", "_")
                ActiveCell.EntireColumn.Select
                Selection.NumberFormat = "m/d/yyyy"
                ActiveCell.Offset(0, 1).Select
                    End If
            If ActiveCell.Value = "Project Finish Date" Then
                ActiveCell.Value = Replace(ActiveCell.Value, " ", "_")
                ActiveCell.EntireColumn.Select
                Selection.NumberFormat = "m/d/yyyy"
                ActiveCell.Offset(0, 1).Select
                    End If
            If ActiveCell.Value = "Project Forecast Finish Date" Then
                 ActiveCell.Value = Replace(ActiveCell.Value, " ", "_")
                 ActiveCell.EntireColumn.Select
                 Selection.NumberFormat = "m/d/yyyy"
                 ActiveCell.Offset(0, 1).Select
            End If
    Any thoughts on this would be appreciated!

  2. #2
    boblarson is offline --------
    Windows 7 64bit Access 2010 32bit
    Join Date
    Jun 2011
    Posts
    1,272
    Try changing this:

    Selection.NumberFormat = "m/d/yyyy"

    to this

    Selection.NumberFormat = "m/d/yyyy;@"

  3. #3
    boblarson is offline --------
    Windows 7 64bit Access 2010 32bit
    Join Date
    Jun 2011
    Posts
    1,272
    Oh, and by the way, the 39685 is the serial date of the date you showed.

  4. #4
    Lorlai is offline Competent Performer
    Windows Vista Access 2007
    Join Date
    Jun 2011
    Posts
    107
    Thanks for your response Bob. Unfortunately, when I hit the second if statement, the entire sheet still turns to the date format.

    Any other ideas into why this might be happening? Or is there another way I go about to prevent this?

    Thank you!

  5. #5
    boblarson is offline --------
    Windows 7 64bit Access 2010 32bit
    Join Date
    Jun 2011
    Posts
    1,272
    Is this code in Access or Excel?

  6. #6
    Lorlai is offline Competent Performer
    Windows Vista Access 2007
    Join Date
    Jun 2011
    Posts
    107
    Running it through access, opening up an excel file and using this code to format excel before bringing the data into a spreadsheet.

  7. #7
    ssanfu is offline Master of Nothing
    Windows 2K Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    PMFJI, instead of running the code to modify the formats in Excel, have you tried setting up an import specification in Access? It cut down the number of errors I had importing Excel and CSV files.

  8. #8
    Lorlai is offline Competent Performer
    Windows Vista Access 2007
    Join Date
    Jun 2011
    Posts
    107
    No, I hadn't thought of that. My current import process is pretty generic, in case there are new excel files that i hadn't seen. How might I go about incorporating an import specification for dates?

    My import code is below:
    Code:
    Option Explicit
    
    Private Declare Function GetOpenFileName Lib "comdlg32.dll" Alias _
    "GetOpenFileNameA" (pOpenfilename As OPENFILENAME) As Long
    
    Private Type OPENFILENAME
    lStructSize As Long
    hwndOwner As Long
    hInstance As Long
    lpstrFilter As String
    lpstrCustomFilter As String
    nMaxCustFilter As Long
    nFilterIndex As Long
    lpstrFile As String
    nMaxFile As Long
    lpstrFileTitle As String
    nMaxFileTitle As Long
    lpstrInitialDir As String
    lpstrTitle As String
    flags As Long
    nFileOffset As Integer
    nFileExtension As Integer
    lpstrDefExt As String
    lCustData As Long
    lpfnHook As Long
    lpTemplateName As String
    End Type
    
    Public Function CreateAccess()
    Dim OpenFile As OPENFILENAME
    Dim lReturn As Long
    Dim sFilter As String
    Dim WrksheetName As String
    Dim i As Integer
    Dim oApp As Object
    
    OpenFile.lStructSize = Len(OpenFile)
    OpenFile.lpstrFilter = sFilter
    OpenFile.nFilterIndex = 1
    OpenFile.lpstrFile = String(257, 0)
    OpenFile.nMaxFile = Len(OpenFile.lpstrFile) - 1
    OpenFile.lpstrFileTitle = OpenFile.lpstrFile
    OpenFile.nMaxFileTitle = OpenFile.nMaxFile
    OpenFile.lpstrInitialDir = "E:\"
    OpenFile.lpstrTitle = "Choose a File"
    OpenFile.flags = 0
    lReturn = GetOpenFileName(OpenFile)
    Set oApp = CreateObject("Excel.Application")
    oApp.Visible = True
    oApp.Workbooks.Open OpenFile.lpstrFile
    
    With oApp
    .Visible = False
    DoCmd.TransferSpreadsheet (acImport), acSpreadsheetTypeExcel97, "Project_Data", OpenFile.lpstrFile, True
    
    End With
    oApp.DisplayAlerts = False
    oApp.Workbooks.Close
    oApp.DisplayAlerts = True
    oApp.Quit
    Set oApp = Nothing
    End Function
    Thank you!

  9. #9
    ssanfu is offline Master of Nothing
    Windows 2K Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Now that I stuck my foot in my mouth, I wish I had some mustard and salt.

    Import specs are not available when importing spreadsheets. I had problems (a few years ago) with importing spreadsheets, so I ended up, from Excel, exporting the data as a CSV file. Then I imported the CSV file using the import specs. I tried opening the spreadsheet, then looping through the columns and rows, but always seemed to have issues; the CSV file import seems to have less issues.

    When trying to import the spreadsheet directly, I found this site:

    Ken's Examples for Importing from EXCEL Workbook Files

    at_____ http://www.accessmvp.com/KDSnell/EXCEL_Import.htm

    There are a lot of examples and code....

    Again, sorry, I seem to suffer from CRS more and more......


  10. #10
    Lorlai is offline Competent Performer
    Windows Vista Access 2007
    Join Date
    Jun 2011
    Posts
    107
    Thanks anyway ssanfu, your emoticons and time are appreciated

  11. #11
    ssanfu is offline Master of Nothing
    Windows 2K Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Just found this mdb...... Maybe the code will help..

  12. #12
    boblarson is offline --------
    Windows 7 64bit Access 2010 32bit
    Join Date
    Jun 2011
    Posts
    1,272
    The reason I asked about whether it was in Access or Excel, is that if it is in Access you really need to use instantiated objects for this code and not using code that is not tied to an instantiated object. So, like using

    ActiveCell.Offset(0,1).Select

    is not good. It should be tied to an instantiated object. So, like:
    Code:
    Dim oAppAs Object
     
    Set oApp = CreateObject("Excel.Application")
    oApp.ActiveCell.Offset(0,1).Select
    Read my quick tutorial about this (it was also published by the Access Team on their Access Blog in September of 2010):
    http://www.btabdevelopment.com/ts/excelinstance

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

Similar Threads

  1. Replies: 1
    Last Post: 02-06-2011, 06:36 PM
  2. Date Format
    By lonewolfwfk in forum Programming
    Replies: 4
    Last Post: 11-25-2010, 09:00 PM
  3. Set date field on subform to date variable
    By laavista in forum Access
    Replies: 4
    Last Post: 06-30-2010, 06:32 PM
  4. set date field on form to date variable
    By laavista in forum Access
    Replies: 3
    Last Post: 06-28-2010, 03:03 AM
  5. date format
    By dollygg in forum Access
    Replies: 2
    Last Post: 01-19-2010, 10:14 AM

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