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

    Access is Acting Strange... Import excel into access table


    I have some code that allows me to import an excel spreadsheet into a new access table. This has been working great for me for the last year, but today I have started getting strange results. The code performs some basic maintenance on the excel sheet for formatting before importing the data. This has allows worked in the past, but now, the formatting is not sticking. I have stepped through the code and watched the changes take place, but once the data is in access, all the junk data that had been deleted is suddenly back and I can no longer use the data as I need it. Does anyone have any idea why this is happening, and how I might be able to get past this?

    The import/formatting code I am using is:
    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
    MsgBox "Please select the saved Weekly Report", vbExclamation, "Attention"
    OpenFile.lStructSize = Len(OpenFile)
    'OpenFile.hwndOwner = Form.Hwnd
    'OpenFile.hInstance = App.hInstance
    'sFilter = "acSpreadsheetTypeExcel (*.xlxs)" & Chr(0) & "*.xlxs" & Chr(0)
    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 = xxxxxxx
    OpenFile.lpstrTitle = "Choose a File"
    OpenFile.flags = 0
    lReturn = GetOpenFileName(OpenFile)
    Set oApp = CreateObject("Excel.Application")
    oApp.Visible = True
    oApp.Workbooks.Open OpenFile.lpstrFile
    
    'Finds the first empty cell in a column
    oApp.Range("A1").End(xlDown).Offset(1, 0).Select
    
    'The first few rows of the spreadsheet feature info
    'about the filters used from <program>. Not necessary for
    'a database. This will delete these rows
    oApp.Range(ActiveCell.Row & ":" & ActiveCell.Row).Select
    oApp.Range("1:1", ActiveCell.Row & ":" & ActiveCell.Row).Delete
    
    oApp.Range("A1").Select
    'Jumps down to the next row Loops through the row until
    'there is an empty cell, clearing the color formats and making the
    'font black (automatic)
    Do
        With oApp.Selection.Interior
        .Pattern = xlNone
        .TintAndShade = 0
        .PatternTintAndShade = 0
            End With
        With oApp.Selection.Font
        .ColorIndex = xlAutomatic
        .TintAndShade = 0
            End With
        
         'Finds and replaces spaces with an underscore
        oApp.ActiveCell.Value = Replace(ActiveCell.Value, " ", "_")
        oApp.ActiveCell.Offset(0, 1).Select
    Loop Until IsEmpty(ActiveCell)
    
    'Deletes the last row of data
    oApp.Range("A" & Cells(Rows.Count, "A").End(xlUp).Row).Select
    oApp.Selection.Delete Shift:=xlUp
    
    With oApp
    .Visible = False
    DoCmd.TransferSpreadsheet (acImport), acSpreadsheetTypeExcel97, "xxxxxxx", OpenFile.lpstrFile, True
    
    End With
    oApp.DisplayAlerts = False
    oApp.Workbooks.Close
    oApp.DisplayAlerts = True
    oApp.Quit
    Set oApp = Nothing
    End Function

  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,928
    The data imported with all the 'junk'?

    Possibly need to save the Excel file after the edits before import. Makes me wonder how this worked before.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    PRMiller is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Mar 2012
    Location
    St. Paul, MN
    Posts
    16
    Definitely need to save!

    Code:
    oApp.ActiveWorkbook.Save
    oApp.Workbooks.Close

  4. #4
    Lorlai is offline Competent Performer
    Windows Vista Access 2007
    Join Date
    Jun 2011
    Posts
    107
    Thanks for the save suggestion. I added the line, but am still experiencing the same problem. Again, I've stepped through and watched the changes happen, and can open up the saved workbook with the changes made, but for some very odd reason, Access is still importing the junk data. I know that I can now import the formatted sheet manually, but my whole process was created to make this process as automated as possible. Are there any other ideas as to why Access is being so weird with this file?
    Last edited by Lorlai; 05-08-2012 at 01:39 PM. Reason: grammer...oops

  5. #5
    June7's Avatar
    June7 is online now VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,928
    Did you add the line before the import action?
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  6. #6
    Lorlai is offline Competent Performer
    Windows Vista Access 2007
    Join Date
    Jun 2011
    Posts
    107
    Yes, the save line is:

    Code:
    'Deletes the last row of data
    oApp.Range("A" & Cells(Rows.Count, "A").End(xlUp).Row).Select
    oApp.Selection.Delete Shift:=xlUp
    
    'Saves the workbook
    oApp.ActiveWorkbook.Save
    
    With oApp
    .Visible = False
    DoCmd.TransferSpreadsheet (acImport), acSpreadsheetTypeExcel97, "PD_Temp", OpenFile.lpstrFile, True
    
    End With
    oApp.DisplayAlerts = False
    
    oApp.Workbooks.Close
    oApp.DisplayAlerts = True
    oApp.Quit
    Set oApp = Nothing
    End Function

  7. #7
    June7's Avatar
    June7 is online now VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,928
    Don't know, never tried to programmatically modify sheet before import. Do you want to provide sample file for testing?
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  8. #8
    PRMiller is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Mar 2012
    Location
    St. Paul, MN
    Posts
    16
    Echoing June7's request. I have an Access app that imports Excel data every day after making some edits, as I like to be as lazy... er, EFFICIENT as possible. With some sample data we should be able to help you get this squared away.

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

Similar Threads

  1. Import from Excel to Access
    By Phred in forum Programming
    Replies: 13
    Last Post: 05-16-2012, 12:25 AM
  2. Replies: 1
    Last Post: 01-23-2012, 11:44 AM
  3. import excel file to access 2003 yes, access 2007 no
    By stapik in forum Import/Export Data
    Replies: 9
    Last Post: 07-27-2011, 07:09 AM
  4. Replies: 1
    Last Post: 11-21-2010, 10:26 PM
  5. Replies: 6
    Last Post: 03-17-2010, 10:09 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