Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Auto Import Table with Primary Key Field

Status
Not open for further replies.

tristap

Technical User
Jun 24, 2005
16
0
0
AU
I have set up an automatic import with specification for a fixed width test file using the TransferText method, however I am having problems with a primary key field.

When I manually import, Access assigns a Primary Key field giving each record a unique ID number, however when this process is automated using TransferText method, the Primary Key field is not established.

Is there any way of including the Primary Key column using the Transferautomatically without having to manually add the column to the text file?
 
I think you must create the primary key after you import the table. Here is one possibility:
Code:
Dim db As DAO.Database
Dim td As DAO.TableDef
Dim f As DAO.Field
Dim strTableName
Set db = CurrentDb
strTableName = "tblTable"
DoCmd.TransferText acImportDelim, , strTableName, "C:\Import.txt"

Set td = db.TableDefs(strTableName)
Set f = td.CreateField("CustID")
f.Type = dbLong
f.Attributes = dbAutoIncrField
td.Fields.Append f
(Mainly taken from:
You could also use Alter table, for example:
Code:
strSQL = "Alter TABLE tblTable Add Column CustID COUNTER ;"
DoCmd.RunSQL strSQL
 
Remou,

Used code provided and this has fixed my issue.

Thank you so much for the assist!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top