Question

Import Excel into Access DB

Asked by: IThema

Hi,

I need some advanced help on importing Excel-files into an Access database.
Consider the following:
- I have a VB program that allows users to import their own Excel files into our fixed format database.
- An excel file may contain up to 65.535 records per sheet and trust me, these are being used sometimes, so the import needs to be speedy
- I have already found a way to import excel data into a table, see the query below
- I need a way to get some import log, like the "Import Excel data"-wizard in Access does

Here's the query I use to import Excel data into a table that has been created in code by VB, using one of the user defined import definitions. This query uses the Jet Excel 8.0 ISAM driver.
INSERT INTO [MyDBTable] (ProductID, EAN, Description)
SELECT [ProdID], [EAN-Code], [ProdDescription]
FROM [EXCEL 8.0;Database=D:\MyImports\Products.xls;HDR=YES].[MyProducts$]

Now, this query works and it's d*mn fast. The only thing is, that when a user defined a field to be numeric for example and a non-numeric value is entered in the excel-file, the non-numeric values get imported as NULL-values, and there's currently no way to see these import errors.

I'd like to expose these import errors like Access does itself when one uses the import wizard so I could log these things to inform the user. I NEED to know the line number, but the name or number of the field in addition to the error description would be nice to have too.

Does someone know how to programmatically import an excel file into Access, using the speedy Jet Excel ISAM and displaying the individual cells that couldn't be imported correctly?


Cheers and thanks in advance,

Luc Derckx

ps: I've placed a link to this topic in the VB Database area too

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2006-03-10 at 00:44:53ID21768351
Tags

access

,

excel

,

import

Topic

Microsoft Access Database

Participating Experts
4
Points
500
Comments
18

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. **ISAM not found**
    Using ADO I have connected to a access table In the following syntax - DBCON.Execute strSQL - I am using a insert SQL statements. Fields in a DBF5 file are selected and inserted in the access table. When this syntax is executed however the following eror message appears "...
  2. Installable ISAM file not found.
    while connecting dbase IV file from VB 6.0 terror message was :could'nt find installable ISAM. Number :3170. How do I solve this.
  3. ISAM
    I use VB 6.0, create a report to link record of access(2000). When i run program(Ctrl_F5), occured a error "coudn't find installable ISAM"?
  4. ISAM ?
    I get this run-time error message "Could not find installable ISAM" when I ran the following codes to connect to my sample database. Please help. I have Office 2000 and Windows Xp pro. Dim myConnection As OleDbConnection Dim myCommand As OleDbCommand ... ...

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: peter57rPosted on 2006-03-10 at 01:07:11ID: 16152923

Hi IThema,
Importing directly into the destination table is a bad idea if you don't have complete control over the data.
And you cannot expect a fast import if you have to validate every field in every record programmatically.
Can you not validate the data in Excel before you start the import?  Is it not possible to use the Data Validation settings in XL to prevent duff data entering in the first place?

If not, then in my opinion you should import into a table of all text fields first, then run queries to check the validity of the data before posting to the final table.

Pete

 

by: boag2000Posted on 2006-03-10 at 02:35:42ID: 16153281

IThema,

Peter is correct.

You should be using the "Data Validation" utility in Excel to PREVENT incorrect values from being entered in the first place.
(Thanks Pete)

Column by column, select all the cells you wish to validate, typically A2:A65536 (A1 is reserved for the "text" field names)
Click: Data-->Validation
Under "Allow", select one of the "Number" types (Whole Number or Decimal)
Then specify the Minimum and Maximum values.
Click: OK

Now if a user types in text, they will get an error message.

That being said... if you ALREADY have unvalidated sheets ready to import, you could copy this formula down the entire column next to the column in question:
=IF(ISTEXT(A2),"Bad","Number")
Then do a Search for:    Bad
Remember to set "Look In" option to "Values"

On the other hand you could use:
=IF(ISNUMBER(C2),"Incorrect","OK")
To find all the Text Fields that might contain numbers.

Hope this helps as well

 

by: IThemaPosted on 2006-03-10 at 03:13:08ID: 16153448

Hi,

Thank you both for your responses,

Pete,
>Importing directly into the destination table is a bad idea if you don't have complete control over the data
You're right: we don't have complete control over the data, but having the data types right has his some nice benefits from this point on (the import needs to be fast and accurate, because the actual import will start AFTER the excel data is transferred to a temporary table).
> And you cannot expect a fast import if you have to validate every field in every record programmatically
You're right again: Validating every cell before continueing isn't an option. It's slow and requires the user to have Excel installed.
> Can you not validate the data in Excel before you start the import? Is it not possible to use the Data Validation settings in XL to prevent duff data entering in the first place?
No, we deal with a great number of customers who all have different backoffice/ERP-systems and different ways for transferring data. One uses txt, the other csv. There's also people using excel files or want to import their current database. We simply cannot validate the data using formula's etc, because we don't know what the data in their files represent.

Boag2000,
See my comment on the last quote I made to Pete

Let me explain how a client defines their import:
1 - User indicates what kind of media should be used to import their data. They can choose between TXT (txt, csv), Excel (xls), Database (Access or SQL) and XML.
2 - Users may use existing files as an example to quickly configure their import definition
3 - User fine tunes/creates the definition by entering field information for the import file(s). Later, he can create a mapping between his file, and our own database. A user can include formulas in this mapping. For example, their column "Unit" could be mapped 1 on 1 with our field "Price unit", but he can also use a formula for it, like: CASE [Unit] WHEN 'g' THEN 'GRM' WHEN 'kg' THEN 'KGM' END

Now, let me give you some more information about step 3:
Part of the field definitions, he can indicate what type of data is located in each field. He may choose between "Text", "Numeric", "Memo", "Date/Time" and "Yes/No". This could be importand, because this is the data type that is being used when they add a formula for the mapping; If the import field is numeric, the field is being treated as numeric in the mapping formula. This is one of the benefits for not just declaring them all as "Text".

Say that his import not only contains "Unit" (which represents price unit), but also has a field called "Price", which is indicated as representing a numeric field. Note that we can't control what the user enters in his own Excel file! Say that he made a typo and there's a row in Excel that has "q23" as a price in stead of "123". When this file gets imported (and it takes about half a second for 2000 lines, which is the speed I'm looking for), the "q23" gets impoted as NULL, because it is not a valid number. So, user says in his import definition: "Price" is a numeric field. He believes it, otherwise he wouldn't be saying it. I want to be able to say to him: "Look, 1 of your prices isn't what you expected it to be. It is not numeric and I wasn't able to import it correctly." This is very important, because otherwise the user will believe that everything went well and no price update/insertion will eventually take place for that particular product.

This functionality is already included within Access itself. It's in the "Import data" wizard. It shows exactly what went wrong and is still very, very fast (also half a second for 2000 records). If I were to scan every cell in the excel file, it will take a considerable amount of time. That's just not an option. Does one of you know how Access does this, or how I could use Access' functionality?

Cheers

 

by: boag2000Posted on 2006-03-10 at 03:52:49ID: 16153587

IThema,

Thanks for the Extra info, now I understand.

I don't think the Import Wizard is "exposed" in VBA?

<Is it Pete?>

Then I would go back to Peter original post and:
<import into a table of all text fields first, then run queries to check the validity of the data before posting to the final table>

This way at least the Import Wizard could list all the errors. Then you could possibly run a query or function to convert all the Nulls to whatever you want..

I am sure Peter can help you out with this better than I could.

Good luck!
:)

 

by: peter57rPosted on 2006-03-10 at 04:25:23ID: 16153749

IThema

Your aim is clear.  Without some more thought I would'nt like to comment on whether your performance goals are reasonable or not.

Can you clarify what happens at present during the 'import'.  You appear to be saying that Access somehow knows what datratype to expect for each field.  How do you do that on the fly?

Pete

 

by: peter57rPosted on 2006-03-10 at 05:00:12ID: 16153951

IThema,

You should be able to run an Access VBA procedure from your VB application via Automation.
Unfortunately where I am at the moment I don't have VB installed so I can't test anything.

If you create an Access Public sub such as

Public Sub Macro1()
On Error GoTo Macro1_Err

    DoCmd.TransferSpreadsheet acImport, 8, "tablename", "sourcefielpathandname", True, ""

Macro1_Exit:
    Exit Sub

Macro1_Err:
    MsgBox Error$
    Resume Macro1_Exit

End Sub

Then in VB you will need something like ....(but my VB is shaky so you will probably do better...)

Sub RunImport()
Dim objAcc As Access.Application
   Set objAcc = New Access.Application
   
      objAcc.OpenDatabase "datebasename"
      Call objAcc.Application.Run("macro1")
      objAcc.Quit
   End With
   Set objAcc = Nothing
End Sub

This should  run the Access import and create an import errors file if necessary.

However, I would still like your feedback on my previous question.
Pete

 

by: IThemaPosted on 2006-03-10 at 05:04:21ID: 16153972

Hi,

boag2000,
Thanks for all your input.

I'm considering to import everything as Text, but need to investigate what impact this will have on the entire import.

Peter,
A client can create and store multiple import definitions with our program. He predefines the Excel-import before he actually does the import. He may then use this import definition more than once, every week for example, to update the data in our program. In this import definition, he states the data type per column in the excel file. It look something like this:

Worksheet name: MyData
Decimal symbol: .
Date format: mm/dd/yyyy

Field         Data Type        Decimals
-----------------------------------------
ProdID      Text
Unit          Text                
PriceDate  Date/Time
Price         Numeric          2

Now, when the user imports a excel file using this import definition, a table is being made to represent this "MyData"-WorkSheet. It is being made, using the information given in the import definition, so it will correspond to my example given above and the Excel-data is imported into this table using the query from my original question. So, I do not determine the data types on the fly. They are already determined.

 

by: IThemaPosted on 2006-03-10 at 05:59:13ID: 16154305

Hi Pete,

You got me thinking. If I can call a VBA routine from within VB, I might also be able to do the DoCmd from VB as well:

Set objAccess = CreateObject("Access.Application")

objAccess.OpenCurrentDatabase strDBTemp
objAccess.DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel8, "MyProducts", "D:\MyImports\Products.xls", True
objAccess.CloseCurrentDatabase
objAccess.Quit

Set objAccess = Nothing

This works well!!

However, now I've come to this point, I ask myself wether this is the right choice, because this approach has it's drawbacks:
- User MUST have Access installed in order to make the "DoCmd"-call
- Table "MyProducts" must have column names matching the column names from Excel for 100%. If no column names are specified, it shoud use F1, F2, ..., Fx as column names. This isn't really a problem, because I can easily make this work.

When it is completed, it created a file for me (it's in Dutch):
Fout (=error)      Rij (=row)      Veld (=field)
Typeconversiefout (=type conversion error)      540      Nt#gew (=Nt.gew, which is a column name in the Excel file)
Typeconversiefout      541      Nt#gew
Typeconversiefout      562      Nt#gew
Typeconversiefout      563      Nt#gew
Typeconversiefout      564      Nt#gew
Typeconversiefout      565      Nt#gew
Typeconversiefout      566      Nt#gew
Typeconversiefout      567      Nt#gew
Typeconversiefout      568      Nt#gew
Typeconversiefout      569      Nt#gew
Typeconversiefout      570      EAN
Typeconversiefout      570      Nt#gew
Typeconversiefout      571      Nt#gew
Typeconversiefout      970      Nt#gew
Typeconversiefout      971      Nt#gew
Typeconversiefout      972      Nt#gew
Typeconversiefout      973      Nt#gew
Typeconversiefout      1046      Nt#gew
Typeconversiefout      1047      Nt#gew
Typeconversiefout      1239      EAN
Typeconversiefout      1239      Nt#gew
Typeconversiefout      1240      Nt#gew

The stupid thing is, that this file doesn't seem to be right. There's only good values listed in the EAN and Nt.gew fields, so I don't know where this is coming from :S

Anyway, this is what I asked for and I guess I will just make the call to Access if I detected the user to have Access on his system and use late binding to realise this. Otherwise, I guess I'll have to use the query and tell people that if they need more detailed import results, they should just install Access. My job now, is to find out why it gave those type conversion errors, while those values are actually OK in Excel.

I have thought some more about just declaring everything as Text, but this will cause memo-fields to truncate.

I'll leave this question open for another day to see if anyone has any other ideas about this, but as for now, I consider this question as answered. Maybe someone knows what MS Access does underneath the hood and how Access detects a value to be inconsistent with a data type. This would allow me to better understand the technology behind all this.


Cheers,

Luc Derckx

 

by: IThemaPosted on 2006-03-10 at 06:17:21ID: 16154445

Correction:
When it is completed, it created a file for me

I mean it's created a TABLE for me.

 

by: matthewspatrickPosted on 2006-03-10 at 07:11:49ID: 16154925

Just a note from a guy active in the Excel TA...

> You should be using the "Data Validation" utility in Excel to PREVENT incorrect values from being
> entered in the first place.
> (Thanks Pete)

This is very true.  Then again, Excel's Data Validation comes with significant limitations, first and foremost
that it *will not prevent invalid data from being pasted in*.  It will prevent invalid data from being manually
entered, but it offers no protection against a paste.

There are some ways to safeguard against a paste, but they are cumbersome.

Generally, I advise not to use Excel as a "UI" for an Access app unless you can really, really trust your users
not to misbehave :)

Patrick

 

by: leonstrykerPosted on 2006-03-10 at 07:39:59ID: 16155181

Just thinking outloud,

How about running a SUMPRODUCT formula on numeric fields before the import.  If there is a none numeric value it will error and the field can then be searched for the offending cell?

Leon

 

by: IThemaPosted on 2006-03-10 at 08:04:45ID: 16155621

Hi leonstryker,

Thank you for joining this question.

Both Peter and Boag mentioned something like that too. They said that I may first have to check the source itself for validation. Since you're the third one that tells me so, I think I should stop being stubborn and give it a try. I'm not sure if it's fast, but I think I'll just give it a try. Nothing to lose, have I?

I've never heard of the SumProduct formula before (actually it does ring a little bell to some maths class at collage, but that was a long time ago :), but I'm sure I can figure it out.

So I'll give it a try and check back on y'all after the weekend, cause this week is over for me :D

ps: Patrick, thanks for your input, but it's a little off topic after all that's been said. The Excel file is not at all an 'extension' or 'UI' for an Access application. We also agreed that the validation feature won't help me in this context. Do you have some good ideas about how to validate an Excel file while importing, without having to examine every cell at a time?

 

by: matthewspatrickPosted on 2006-03-10 at 08:29:21ID: 16155988

> ps: Patrick, thanks for your input, but it's a little off topic after all that's been said. The Excel file is
> not at all an 'extension' or 'UI' for an Access application. We also agreed that the validation feature
> won't help me in this context. Do you have some good ideas about how to validate an Excel file while
> importing, without having to examine every cell at a time?

A common technique is to import into a "holding table" with everything as text, and then use SQL to
validate the data there and move it to the permanent tables.

Patrick

 

by: leonstrykerPosted on 2006-03-10 at 08:32:54ID: 16156070

Hmm, I have had some problems with that technique, where numbers from Excel were not placed into text field.  You may need to play around with some setting in Access to get it right.

Leon

 

by: IThemaPosted on 2006-03-15 at 04:56:37ID: 16193681

Hi,

I've done some more thinking (it's a very complex feature; there's more formats to support and we'd like to have common code for that) and importing everything as text could cause problems with memo-fields. Also, Leon, I think that the problem you experienced can be solved by using the property IMEX=1 in the query; this forces the Excel ISAM to use the destination data types, in stead of scanning the excel file for a predefined number of rows. If, for example, the Excel ISAM treats column A as numeric and its destination is text, the numeric values are not being transferred (I think that's stupid, because numbers can implicitly be converted to text...).

Anyway, I don't want this question to turn into a discussion to what approach is best. Discussing it may be very usefull, but goes a little off topic, since my initial question was:
>Does someone know how to programmatically import an excel file into Access, using the speedy Jet Excel ISAM and displaying the individual cells that couldn't be >imported correctly?

This question has been answered by Peter:
Yes, it's possible to do that by calling an Access command. However, there seems to be a bug in the detection of wrong cells, but that seems to be something inevitable and is actually my fault, because I failed to detect that earlier.

For those who are interested in the approach that I will now use:
Data from txt, csv, xls, xml will be imported in tables that represent the data types from the source files, meaning that I will have the Excel worksheets imported into tables that also have numeric, date, memo and yes/no data types. This way, I will not be able to show the user what I'd actually like to show ("Look, 1 of your prices isn't what you expected it to be. It is not numeric and I wasn't able to import it correctly."), but enables me to have a structured and FAST code. I could explain to our clients why we can't show these errors; every file format has its benefits and drawbacks.

I'd like to thank everyone who helped me here.

 

by: IThemaPosted on 2006-03-15 at 04:59:12ID: 16193696

? I seem to have done something wrong. Peter, I accept your answer. Boag, thank you very much for assisting. I will post a comment in CS to correct this.

 

by: peter57rPosted on 2006-03-15 at 05:07:34ID: 16193737

All looks OK to me

Pete

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...