Results 1 to 5 of 5
  1. #1
    teddyv is offline Novice
    Windows XP Access 2007
    Join Date
    Apr 2011
    Posts
    3

    DoCmd.TransferSpreadsheet for specific worksheet in .xls file

    I have been asked to modify the code in a 2007 Access database that was simply importing a monthly single sheet Excel file. Now the user should be able to select the sheet to import from multiple sheets that are now in the file.

    The tab names are consistant from monthly file to monthly file so I have added a drop down list with the available tab names and store the selection in "txt_defaulttab".

    The user is also able to browse to the monthly file and then the path is stored in txt_defaultimport.

    I've been searching for the code to to represent the sheet to import. Access help says "If you are importing from or linking to an Excel version 5.0 or later spreadsheet, you can prefix the range with the name of the worksheet and an exclamation point; for example, Budget!A1:C7. ", but it doesn't work the way I am doing it.



    Private Sub Command0_Click()
    DoCmd.SetWarnings False


    DoCmd.OpenQuery "Delete Monthly Excel Import", acViewNormal

    ' "txt_defaultimport" is the path to the .xls file
    ' "txt_defaulttab" is the name of the worksheet tab to import

    Dim def As String
    def = txt_defaultimport

    DoCmd.TransferSpreadsheet acImport, 8, "Monthly Import", txt_defaultimport, True, txt_defaulttab & "!"

    DoCmd.OpenQuery "Delete Blank Lines", acViewNormal
    def = DCount("[ID]", "Monthly Import")
    MsgBox def & " items Imported", vbInformation + vbOKOnly, "Count Items Imported"

    DoCmd.SetWarnings True
    End Sub


    Any help would be greatly appreciated!

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    Are txt_defaultimport and txt_defaulttab controls on the form? Have you step debugged? Try using qualifer prefix for the controls:
    DoCmd.TransferSpreadsheet acImport, 8, "Monthly Import", Me.txt_defaultimport, True, Me.txt_defaulttab & "!"
    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
    teddyv is offline Novice
    Windows XP Access 2007
    Join Date
    Apr 2011
    Posts
    3
    Quote Originally Posted by June7 View Post
    Are txt_defaultimport and txt_defaulttab controls on the form? Have you step debugged? Try using qualifer prefix for the controls:
    DoCmd.TransferSpreadsheet acImport, 8, "Monthly Import", Me.txt_defaultimport, True, Me.txt_defaulttab & "!"

    Yes, the two variables are controls on the form and qualifying the variables does not help.

    The attached file shows the form and the error I am getting. It is replacing the "!" in the code with a "$". Is that what it's supposed to do?

    Do I need to add a range like A1:Z5000 to the worksheet name? When I do, I get a error 3011, could not find the object "Bank$A1:Z5000".

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    I tested code and cannot replicate the issue. The import runs fine with the concatenated "!". Try without the "!". The sheet name might be sufficient, especially since not referencing range. I didn't think to try before deleting my test file. Let me know.

    You could also test by hard coding those values. That's what I did instead of referencing textboxes. If that works then know something is wrong with the textbox inputs.
    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.

  5. #5
    teddyv is offline Novice
    Windows XP Access 2007
    Join Date
    Apr 2011
    Posts
    3
    Thanks for your help.

    The requestor has just decided to cancel the change request.

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

Similar Threads

  1. Replies: 6
    Last Post: 11-05-2011, 09:01 AM
  2. DoCmd.RunSavedImportExport file name
    By svcghost in forum Import/Export Data
    Replies: 6
    Last Post: 02-08-2011, 08:05 AM
  3. Replies: 1
    Last Post: 11-21-2010, 10:26 PM
  4. Replies: 2
    Last Post: 02-27-2010, 06:53 AM
  5. Replies: 1
    Last Post: 03-25-2009, 02:20 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