Microsoft KB Archive/153812

= ACC: TransferSpreadsheet May Not Allow Table Append =

Article ID: 153812

Article Last Modified on 1/19/2007

-

APPLIES TO


 * Microsoft Access 2.0 Standard Edition
 * Microsoft Access 95 Standard Edition
 * Microsoft Access 97 Standard Edition

-



This article was previously published under Q153812



Moderate: Requires basic macro, coding, and interoperability skills.



SYMPTOMS
When you append data from a spreadsheet to an existing table by using a TransferSpreadsheet macro action or a TransferSpreadsheet method in Visual Basic for Applications, you may receive the following error message:

Field 'F1' doesn't exist in destination table ' '.

Microsoft Access was unable to complete the append operation. The destination table must contain the same fields as the table you are pasting from.



CAUSE
The first row of the spreadsheet file does not contain field names; therefore, Microsoft Access assumes the fields are named "F1," "F2," and so on.



RESOLUTION
You can use one of the following two methods to work around this behavior. You can import the spreadsheet into a new table, and then append that data in Microsoft Access; or you can change the spreadsheet to include field names in the first row.

Method 1

 * 1) Import the spreadsheet into a new table named tblTemp.
 * 2) Create an append query to append the data in the tblTemp table to your existing table.
 * 3) Run the append query.
 * 4) Delete the tblTemp table.

Method 2
 In Microsoft Excel, insert a new first row in the spreadsheet. In this row, add field names that match the field names in the existing Microsoft Access table.  In the macro, change the Has Field Names argument to Yes, for example:

     Transfer Type: Import Spreadsheet Type: Table Name: File Name:  Has Field Names: Yes   If you are using code, the HasFieldNames argument should be set to True, for example:

NOTE: In the following sample code, an underscore (_) at the end of a line is used as a line-continuation character. Remove the underscore from the end of the line when re-creating this code in Access Basic.

     DoCmd.TransferSpreadsheet acImport, _ , " ", _ "", True </li></ol>

<div class="moreinformation_section">

Steps to Reproduce Behavior
NOTE: This example uses the Shippers table from the Northwind sample database (or NWIND.MDB in version 2.0).

<ol>  Open Microsoft Excel version 7.0, and create a spreadsheet with the following data:

<pre class="fixed_text">     9     Express      800-123-4567 10    US Mail      800-569-4568 </li> Save spreadsheet as c:\test1.xls.</li> Open the sample database Northwind.mdb (or NWIND.MDB in version 2.0).</li>  Create a macro with the TransferSpreadsheet action, and the following values:

<pre class="fixed_text">     Transfer Type: Import Spreadsheet Type: Microsoft Excel 5-7 Table Name: Shippers File Name: c:\test1.xls Has Field Names: No                       </li> Run the macro. Note that you receive the error mentioned in the "Symptoms" section.</li>  Create a new module, and enter the following function.

NOTE: In the following sample code, an underscore (_) at the end of a line is used as a line-continuation character. Remove the underscore from the end of the line when re-creating this code in Access Basic.

Function ImportTest DoCmd.TransferSpreadsheet acImport, 5, "Shippers", " c:\test1.xls", _ False End Function </li> Open the Debug window (or the Immediate window in version 2.0), type the following line, and then press ENTER:

?ImportTest

Note that you receive the error mentioned in the "Symptoms" section.</li></ol>

<div class="references_section">