Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 810
  • Last Modified:

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
0
gracie1972
Asked:
gracie1972
  • 2
  • 2
  • 2
1 Solution
 
Dale FyeCommented:
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.
0
 
Rey Obrero (Capricorn1)Commented:
your excel file have a line that extend to the right end of the worksheet, limit your import codes with

DoCmd.TransferSpreadsheet acImport, , "tblResources", "C:\Documents\NACS\Resources.xlsx", True,"Sheet1!A1:V500"
0
 
gracie1972Author Commented:
Which is the best method?  

I am setting this up for end users to import data without having to add resources line by line.......
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
Rey Obrero (Capricorn1)Commented:
import it.
0
 
Dale FyeCommented:
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.
0
 
gracie1972Author Commented:
Thank you :-)
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

  • 2
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now