Solved

Import Excel Spreadsheet into Access using SQL...

Posted on 2004-08-25
12
349 Views
Last Modified: 2008-02-01
Hello. I'm trying to import an Excel spreadsheet into Access using SQL. Below is the SQL statement that I'm using... any ideas why it's not working?


SELECT * INTO [TestTable] FROM [Data$] in [EXCEL 8.0;HDR=YES;IMEX=2;DATABASE=C:\Temp\Book1.xls]


The error I get is: '[EXCEL 8.0;HDR=YES;IMEX=2;DATABASE=C:\Temp\Book1.xls]' is not a valid name.


When I use this same type of method with .txt files, I have to create a schema first... is that also necessary for Excel?


WATYF
0
Comment
Question by:WATYF
  • 4
  • 3
  • 2
  • +2
12 Comments
 
LVL 65

Expert Comment

by:Jim Horn
ID: 11895830
Good God, don't do it that way.  Use this VBA code line instead...

DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel8, "TestTable", "c:\Temp\Book1.xls", {HasFieldNames: True or False}, {Excel cell range, if anything but the entire sheet}

Hope this helps.
-Jim
0
 
LVL 26

Expert Comment

by:dannywareham
ID: 11895893
Jim is right.
There are commands built into access (TRANSFERSPREADSHEET is the best)
Although you can also link the spreadsheet as a table.
0
 
LVL 11

Author Comment

by:WATYF
ID: 11895950
LOL... Good God, I'm working from Excel, not Access.


This is being done entirely programmatically from Excel, not by someone working in an Access database.


The procedure was written (a while ago) to allow the user to upload the file they are currently working on into an Access DB. It currently opens an instance of Access in the background (waste of memory) and uses the TransferSpreadsheet method (the you mentioned above) to import the file,... but I am making improvements to this particular program, and one of those is switching completely to SQL which is more efficient and faster.


I have done this before in the past (quite a while ago), and just need a refresher on the syntax of this particular statement.


WATYF
0
 
LVL 65

Expert Comment

by:Jim Horn
ID: 11896017
In that case, it might not be a bad idea to re-post this question in the Microsoft Excel channel.  (After giving danny and me points for moral support...  :)

It might not be a bad idea to export from Excel as a comma-separated text file, then let Access import it as part of its processing.  There could be a lot of probems going from Excel straight to Access, namely table locks, someone changing the Access table definition, etc. etc.

My only comment on SQL would be if your SQL Server is guarded by a Database Administrator (DBA)-type individual, they will likely howl at the idea of letting a super-user export from Excel directly into a SQL Server table.  

Hope this helps.
-Jim
0
 
LVL 44

Expert Comment

by:GRayL
ID: 11896134
When you did this with text files, I believe you linked to the text file using a schema. Then the resulting Access table can be queried appropriately. If you did the same in the instance of Access and linked to the Excel file, the resulting Access table can also be queried appropriately.  However, you cannot query an Excel file in its native Excel directory. It must first be imported or linked. If it is linked, you cannot update the table, and consequently the Excel file because of formulas and other Excel features which make non-excel updates unwise.
0
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.

 
LVL 11

Author Comment

by:WATYF
ID: 11896194
Well... you won't get points for moral support, but you may get them for suggesting the interim step. :o)

This process is a necessary evil unfortunately... I have an Access database that houses tables that are accessed by a UDF in Excel. Most of those tables are built by me (or rather, by a program I wrote which runs on a daily basis). But the users want the ability to upload their own "tables" as they need them. I'm aware of the implications of this, and have gone to great lengths to instruct them on how to "cleanse" their Excel sheets before uploading. It works out OK. Since it's all done through a GUI, they have no way of accidentally corrupting the DB or altering any pre-existing tables.


If I can't get this statement to work, I may have to use an interim step (much like you mentioned above) in which I save a copy of the Excel file as a tab-delimited file and use an SQL statement to import that into the DB. I already have working code that I use to do that (for the tables that my program builds daily). But it requires that I build a schema each time the user uploads a file, and that bring variables into the picture that I'd like to avoid if at all possible...


....which is why I'm attempting to do it straight from Excel in the first place.



WATYF
0
 
LVL 11

Author Comment

by:WATYF
ID: 11896432
well... looks like I figured it out.


SELECT * INTO [TestTable] FROM [EXCEL 8.0;HDR=YES;IMEX=2;DATABASE=C:\Temp\Book1.xls].[Data$]


No schema necessary.


Dang... I could swear I tried that earlier too. Oh well.



WATYF
0
 
LVL 65

Expert Comment

by:Jim Horn
ID: 11896498
Cool beans man.
0
 
LVL 44

Expert Comment

by:GRayL
ID: 11896578
WATYF: Good work! I've got A2K and the help files are rather skimpy on this subject. This is a good one to keep tucked away.
0
 
LVL 11

Author Comment

by:WATYF
ID: 11896807
Yeah... I'll have the mods PAQ it.

It's a pain, because using SQL directly, instead of relying on the crumbs that Access throws you (i.e. their pre-define methods), just gives you so much more flexibility. There really should be a good place to go to find the breakdown on all the SQL statements that you can use to communicate between Office apps. Standard SQL is easy to find online... but when you get into this weirdness, you don't really have many places to go.


WATYF
0
 

Accepted Solution

by:
CetusMOD earned 0 total points
ID: 11934628
Closed, 250 points refunded.
CetusMOD
Community Support Moderator
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Experts-Exchange is a great place to come for help with solutions for your database issues, and many problems are resolved within minutes of being posted.  Others take a little more time and effort and often providing a sample database is very helpf…
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
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…
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

757 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

23 Experts available now in Live!

Get 1:1 Help Now