gracie1972
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\Resourc es.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
Here is my code:
--------------------------
Private Sub cmdImport_Click()
DoCmd.TransferSpreadsheet acImport, , "tblResources", "C:\Documents\NACS\Resourc
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.......
I am setting this up for end users to import data without having to add resources line by line.......
import it.
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.
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.
ASKER
Thank you :-)
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.