Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsFormsForms ProgrammingQueriesModules / DAO / VBAReports / PrintingMacrosDatabase DesignSecurityConversionImporting / LinkingSQL Server / ADPMultiuser / NetworkingReplicationSetup / ConfigurationDeveloper ToolkitsActiveX ControlsNew UsersGeneral 1General 2
Access DirectoryToolsTutorialsUser Groups
Related Topics
SQL ServerOther DB ProductsMS OfficeMore Topics ...

MS Access Forum / Importing / Linking / August 2004

Tip: Looking for answers? Try searching our database.

Importing Excel spreadsheet into Access - data type problem

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Andrew Good - 26 Aug 2004 11:51 GMT
A number of users here at the University of Plymouth have
reported to me a problem when they import Excel
spreadsheets into Access.  It does not allow them to
specify the data types of the fields to be imported.  I
have tried this myself and have encountered the same
problem.

Versions are:
Windows XP Professional SP 1
MS Office XP Professional SP 2.  (The problem is not
resolved by applying Office XP Pro SP 3.)

Here's the recipe:  Create an Excel spreadsheet, with
column headings in its first row, and save it as
test.xls.  Exit from Excel.

Start up Access and create a new, empty database.
Choose File -> Get External Data -> Import...
and select test.xls.

The Import Spreadsheet Wizard starts up.  Select Sheet1,
check "First Row Contains Column Headings", and
select "Store Data in a New Table."

It now asks for information about each of the fields to
be imported.  But the Data Type is grayed out and cannot
be changed from the Data Type that Access displays.

Can anyone tell me why this is happening, and how to put
it right?

Andrew Good
Applications Team
University of Plymouth
John Nurick - 26 Aug 2004 18:17 GMT
Hi Andrew,

This is normal. The Access routine that imports Excel data doesn't allow
direct control over the types of the fields it creates, and often runs
into trouble with Excel columns that contain a mix of numeric and text
values.

You can work round this in any of the following ways:

1) create the table yourself with the field types you need, then import
the spreadsheet data. The field names in the table must exactly match
the column headings in Excel.

2) make sure that at least one row near the top of the Excel table
contains values that can only be interpreted as the data types you need
(e.g. text that cannot be interpreted as a number if you want the column
to become a text field).  Sometimes the simplest way to do this is to
insert a first row of "dummy" data into Excel just for this, and then
delete it from the Access table once the data has been imported.

3) Access assigns field types on the basis of the data it finds in the
first dozen or so rows of the spreadsheet table. It pays no attention to
cell formats. Sometimes a useful trick is to put an apostrophe ' in
front of numeric values in thecells (e.g. '999): this forces Excel and
Access to treat them as text, but the apostrophe is not displayed in
Excel or imported into Access.

(Just to make things more confusing,  Access applies different rules
when you're linking Excel data rather than importing it. Simplifying
somewhat: when importing, any text value in the first few rows will
cause a field to be imported as text. When linking, any *numeric* value
in the first few rows will cause a field to be linked as numeric even if
all the other values are non-numeric.)

3) Write your own import code using Automation to get the values direct
from the worksheet cells and recordset operations or queries to append
them into your table.

Sub AddApostrophes()
 Dim C As Excel.Range
 For Each C In Application.Selection.Cells
   If IsNumeric(C.Formula) Then
     C.Formula = "'" & C.Formula
   End If
 Next
End Sub

Sub RemoveApostrophes()
 Dim C As Excel.Range
 For Each C In Application.Selection.Cells
   C.Formula = C.Formula
 Next
End Sub

>A number of users here at the University of Plymouth have
>reported to me a problem when they import Excel
[quoted text clipped - 30 lines]
>Applications Team
>University of Plymouth

--
John Nurick [Microsoft Access MVP]

Please respond in the newgroup and not by email.
Jamie Collins - 27 Aug 2004 10:21 GMT
> Access assigns field types...

No, this is a Jet process.

> ...on the basis of the data it finds in the
> first dozen or so rows of the spreadsheet table.

This is determined by a Jet registry key which, if set to zero, scans
all rows. For more details see:

http://www.dicks-blog.com/excel/2004/06/external_data_m.html

> It pays no attention to
> cell formats.

That is incorrect. As proof, create an Excel workbook containing a
single cell formula

 =38000

Change the cell format to (custom) dd mmm yyyy. Include the column in
a query e.g.

 SELECT F1
 FROM [Excel 8.0;HDR=No;C:\Tempo\db.xls;].[Sheet1$]
 ;

The value appears as 14 JAN 2004 (in local format) and, using ADO's
OpenSchema method, the column is show to have been determined as
adDate ('a date value').

Jamie.

--
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.