Remove primary key after import

User avatar
Michael Abrams
4StarLounger
Posts: 574
Joined: 10 Feb 2010, 17:32

Remove primary key after import

Post by Michael Abrams »

Acc2013

Is there a line (or 2) of code that I can add to my command button - after a line that imports a table from another database that will remove the Primary Key from a field named RECNUM?

Code: Select all

DoCmd.TransferDatabase acImport, "Microsoft Access", _
   "R:\XYZ HMOs 2017\HMONAME 2017\" & strMonth & "\XYZ\" & strMonth & ".ACCDB", acTable, _
   "CURRENT MONTH MEMBER COUNT", "CURRENT MONTH MEMBER COUNT XYZ", False
Michael

User avatar
HansV
Administrator
Posts: 78549
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Remove primary key after import

Post by HansV »

Let's say the primary key is imaginatively named PrimaryKey.
You can use

Code: Select all

CurrentDb.Execute "ALTER TABLE [CURRENT MONTH MEMBER COUNT XYZ] DROP CONSTRAINT PrimaryKey", dbFailOnError
Best wishes,
Hans

User avatar
Michael Abrams
4StarLounger
Posts: 574
Joined: 10 Feb 2010, 17:32

Re: Remove primary key after import

Post by Michael Abrams »

My code (RECNUM is the field name for the Primary Key):

Code: Select all

CurrentDb.Execute "ALTER TABLE [CURRENT MONTH MEMBER COUNT XYZ] DROP CONSTRAINT RECNUM", dbFailOnError
Receive error 3755 - CHECK constraint RECNUM does not exist.

CURRENT MONTH MEMBER COUNT XYZ table has a Primary Key = RECNUM

User avatar
HansV
Administrator
Posts: 78549
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Remove primary key after import

Post by HansV »

The primary key is on the field RECNUM, but RECNUM is not necessarily the index name of the primary key. You can view the index name in the Indexes window:
S1565.png
In this screenshot, the primary key is on the field HolidayDate, but its index name is PrimaryKey. The latter is the name you should use in the code.
You do not have the required permissions to view the files attached to this post.
Best wishes,
Hans

User avatar
Michael Abrams
4StarLounger
Posts: 574
Joined: 10 Feb 2010, 17:32

Re: Remove primary key after import

Post by Michael Abrams »

Ouch - I have been working with Access for xx years and forgot all about that. It works fine.

I need to append this into another table, so I will work on that today.

Thank you HansV.

Michael