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.
--