Import Sheet2, Sheet3, and Sheet4 into Existing Access Tables

Healey33

New Member
Joined
May 28, 2012
Messages
19
As the title states, I am trying to import and append multiple sheets from a single excel file into tables in my Access DB. The excel Sheet2 will always be appended to Access table1, Sheet3 will always be appended to Table2 and Sheet4 will always be appended to Table3. The sheets in excel already contain the exact column headers in the top row.
The goal is to use the file dialog from Access and navigate to the excel file, select it, and have the data in the 3 sheets appended to the tables in the database. I am an intermediate VBA programmer and have been unable to find a solution to this online. Any help would be greatly appreciated!
I am using MS Office 2010

Thanks
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
Here is what I have so far. Just trying with one sheet for now.

Code:
Dim f As Object
    Dim strFile As String
    Dim strFolder As String
    Dim varItem As Variant


    Set f = Application.FileDialog(3)
    f.AllowMultiSelect = False
    If f.Show Then
        For Each varItem In f.SelectedItems
            strFile = varItem
            MsgBox "File: " & strFile
        Next
    End If
    Set f = Nothing






DoCmd.TransferSpreadsheet acImport, 9, "Cost_Breakdown", strFile, True, , "Sheet2"

When I manually import the sheet into the table "Cost_Breakdown", there are no issues. when running this code, I get Runtime Error '2498' (type mismatch for one of he fields). The only difference I see is that I have an autonumber field in the database that is not populated in the excel table. Is this what is causing me problems?

Edit: It appears the AutoNumber was not the root cause. Tried with another table that does not have an autonumber field and received the same error message.
 
Last edited:
Upvote 0
I solved this. Syntax error with the DoCmd.TransferSpreadsheet command. It should have looked like this:
Code:
DoCmd.TransferSpreadsheet acImport, 10, "Cost_Breakdown", strFile, True, "Sheet2$"
I had the wrong spreadsheet type (10 is needed for Excel 2010) and I had an extra comma before the "Sheet2$". I also missed the "$" after the sheetname.
Hope this helps anyone who runs into the same problem!
 
Upvote 0
Save the excel file to the same place & name everytime.
Link in the 3 sheets.
build 3 queries , each 1 imports 1 sheet.
put in macro.

So you would then
1. Save the new file and overwrite the generic import name.
2. Run import macro. done.
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top