Results 1 to 7 of 7
  1. #1
    Sephaerius is offline Advanced Beginner
    Windows XP Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    62

    Import from Excel using TransferSpreadsheet

    I have a small issue I think should be fairly easy to resolve, but I'm a beginner so I'm stuck.


    I need to pull spreadsheets from Excel into an Access 2010 database I'm building. Here's the code I'm using right now.

    Code:
    Function import_hl7()
    
    On Error GoTo import_hl7_Err    
    
    DoCmd.TransferSpreadsheet acImport, 8, "master_table", "C:\Documents and Settings\redacted\Desktop\test\redacted.xls", True, ""
    
    import_hl7_Exit:
        Exit Function
    
    import_hl7_Err:
        MsgBox Error$
        Resume import_hl7_Exit
    
    End Function
    This works just fine. The problem is that each day the spreadsheet will have a different name, so ideally I would like to be able to import the Excel document I currently have open. That way I can just open 'todays' document and run the script to import it without worrying about paths and file names.

    Thank you,

  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,644
    Sorry, TransferSpreadsheet doesn't work that way.

    What you can do is populate a variable with the path\filename then use that variable in TransferSpreadsheet.

    One way to retrieve path\filename is with FileDialog. Review http://msdn.microsoft.com/en-us/libr...ffice.11).aspx
    Code:
    Sub ImportExcel()
        ' Requires reference to Microsoft Office 11.0 Object Library.
        Dim fDialog As Office.FileDialog
        Dim varFile As Variant
        Dim strFile As String
        Set fDialog = Application.FileDialog(msoFileDialogFilePicker)
        With fDialog
           .AllowMultiSelect = False
           .Title = "Please select files"
           .Filters.Clear
           .Filters.add "Access Databases", "*.XL*"
           If .Show = True Then
              For Each varFile In .SelectedItems
                 DoCmd.TransferSpreadsheet acImport, 8, "master_table", strFile, True, ""
              Next
           Else
              MsgBox "You clicked Cancel in the file dialog box."
           End If
        End With
    End Sub
    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
    Sephaerius is offline Advanced Beginner
    Windows XP Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    62
    June7,
    Thank you for that... I think this will suffice for my needs, but it isn't working just yet. After I select my Excel document I receive 'error 2522 The action or method requires a File Name argument.' Debug takes me to the TransferSpreadsheet line. I think it doesn't like the strFile variable. Thoughts?

  4. #4
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,642
    FYI, you can use late binding with that, replacing these 2 lines:

    Code:
      Dim fDialog                 As Object    'Office.FileDialog
      Set fDialog = Application.FileDialog(3)    'msoFileDialogFilePicker
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,642
    Try replacing that variable with varFile
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  6. #6
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,644
    Ooops, sorry, I didn't use the strFile variable after all. Paul is correct, can just use varFile.
    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.

  7. #7
    Sephaerius is offline Advanced Beginner
    Windows XP Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    62
    Yep, using varFile in place of strFile seems to work perfectly! Thanks everyone!

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

Similar Threads

  1. Import from Excel Using TransferSpreadsheet
    By P5C768 in forum Import/Export Data
    Replies: 10
    Last Post: 05-01-2013, 01:17 PM
  2. Replies: 4
    Last Post: 12-17-2012, 01:21 PM
  3. TransferSpreadsheet to Excel (Multiple Tabs)
    By smoothlarryhughes in forum Import/Export Data
    Replies: 9
    Last Post: 11-10-2012, 12:59 PM
  4. VBA to open excel, import on close of excel
    By bdaniel in forum Programming
    Replies: 0
    Last Post: 03-20-2010, 02:45 PM
  5. Replies: 2
    Last Post: 02-27-2010, 06:53 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