Link to home
Start Free TrialLog in
Avatar of gracie1972
gracie1972Flag for United States of America

asked on

Using VBA to import a spreadsheet into Access from Excel

I am trying to create a command button to import data from an excel spreadsheet to an table in my database.  I want to append the data that is there.

Here is my code:
-----------------------------------------------------------------------------------
Private Sub cmdImport_Click()

DoCmd.TransferSpreadsheet acImport, , "tblResources", "C:\Documents\NACS\Resources.xlsx", True
 
End Sub
------------------------------------------------------------------------------------

When i run the code, I get a "Field F23" doesn't exist in the destination table 'tblResources'.

I don;t have a field name with this name.  My table is set up exactly as my spreadsheet, which I attached to give an idea of field names, etc .
Resources.xlsx
Avatar of Dale Fye
Dale Fye
Flag of United States of America image

Rather than importing directly into your table,

Use the linking feature of the TransferSpreadsheet method to link the spreadsheet to your database, then use a query to selectively append the fields from the spreadsheet into your application.

The problem is that MS has not given use the ability to define import specifications for importing directly from Excel.  So the alternatives include the method above or importing into a staging table, then taking the data to your production table.

Either of these methods gives you the ability to identify rows in the Excel data that have invalid data, and avoid importing those until the data is fixed.
ASKER CERTIFIED SOLUTION
Avatar of Rey Obrero (Capricorn1)
Rey Obrero (Capricorn1)
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gracie1972

ASKER

Which is the best method?  

I am setting this up for end users to import data without having to add resources line by line.......
If you know for certain that the Excel file contains a specific format, which cannot be changed, the importing works fine.  But if someone could inadvertently enter a text character into a numeric field, you might want to consider the staging table technique.

Just sayin', when users are involved, anything can happen, and I try to design my applications to take that into account.  Depends on the needs of your client and whether they are willing to pay for the extra risk prevention.
Thank you :-)