Solved

Using VBA to import a spreadsheet into Access from Excel

Posted on 2013-01-17
6
706 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
  • 2
  • 2
  • 2
6 Comments
 
LVL 47

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 119

Accepted Solution

by:
Rey Obrero 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 119

Expert Comment

by:Rey Obrero
ID: 38789392
import it.
0
 
LVL 47

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

I originally created this report in Crystal Reports 2008 where there is an option to underlay sections. I initially came across the problem in Access Reports where I was unable to run my border lines down through the entire page as I was using the P…
A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…

747 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now