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!