Export/Import

D

DS

I'm trying to export a table to a txt file, the export works fine.

'Export
Private Sub Command0_Click()
DoCmd.TransferText acExportDelim, , "Table1", "c:\Access\export.txt"
End Sub


The problem is when I try to import back into the table, it keeps asking
for a field that does't exist. Any help appreciated.


'Import
Private Sub Command1_Click()
DoCmd.TransferText acImportDelim, , "Table1", "C:\Access\Export.txt"
End Sub

Thanks
DS
 
J

John Nurick

When you're importing to an existing table, the field names in the
source must match the field names in the destination.

Your export statement creates a text file without field names; and when
the import routine encounters this it assigns default field names F1,
F2... - and then panics when these don't match the names in the table
you're importing to.

The simplest solution is to use the HasFieldNames argument of
TransferText to ensure that the field names are used when exporting or
importing.
 
D

DS

John said:
When you're importing to an existing table, the field names in the
source must match the field names in the destination.

Your export statement creates a text file without field names; and when
the import routine encounters this it assigns default field names F1,
F2... - and then panics when these don't match the names in the table
you're importing to.

The simplest solution is to use the HasFieldNames argument of
TransferText to ensure that the field names are used when exporting or
importing.
Thanks, I'm getting closer.
So if I had fields F1, F2 and F3 then it would look like this?

DoCmd.TransferText acImportDelim, , "Table1", "C:\Access\Export.Txt",
F1, F2, F3


Thanks again,
DS
 
J

John Nurick

No. TransferText doesn't care what the field names are.

When you export the data, use something like this, passing -1 or True as
the HasFieldNames argument:

DoCmd.TransferText acExportDelim, , "Table1", "c:\Access\export.txt", -1

That means that the first row of the text file will contain the field
names.

Then, when you import, use something like this:

DoCmd.TransferText acImportDelim, , "Table1", "C:\Access\Export.txt", -1

and Access will use use the field names in the text file to match its
fields with those in the existing table.
 
D

DS

John said:
No. TransferText doesn't care what the field names are.

When you export the data, use something like this, passing -1 or True as
the HasFieldNames argument:

DoCmd.TransferText acExportDelim, , "Table1", "c:\Access\export.txt", -1

That means that the first row of the text file will contain the field
names.

Then, when you import, use something like this:

DoCmd.TransferText acImportDelim, , "Table1", "C:\Access\Export.txt", -1

and Access will use use the field names in the text file to match its
fields with those in the existing table.
Thnaks, I've got it now! Finally!
Thanks
DS
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top