Solved

Using VBA to import a spreadsheet into Access from Excel

Posted on 2013-01-17
6
733 Views
Last Modified: 2013-01-17
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
Comment
Question by:gracie1972
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
6 Comments
 
LVL 48

Expert Comment

by:Dale Fye (Access MVP)
ID: 38789336
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
 
LVL 120

Accepted Solution

by:
Rey Obrero (Capricorn1) earned 500 total points
ID: 38789348
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
 

Author Comment

by:gracie1972
ID: 38789387
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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 38789392
import it.
0
 
LVL 48

Expert Comment

by:Dale Fye (Access MVP)
ID: 38789411
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
 

Author Comment

by:gracie1972
ID: 38789428
Thank you :-)
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
If you need a simple but flexible process for maintaining an audit trail of who created, edited, or deleted data from a table, or multiple tables, and you can do all of your work from within a form, this simple Audit Log will work for you.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

690 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question