There are two ways to create an access primary key: one is manually created in access, the other is created using code (whether in access or in external programs ).
Different primary key names are also different. The primary key names created in the first method are primarykey. Note that there is no space in the middle. The names of the second primary key must be identified using VBA code:
Public Sub fnGetPrimaryKeyName ()
Dim v_DB As New ADOX. Catalog
Dim v_Table As ADOX. Table
Dim v_Key As ADOX. Key
Dim objTable As New ADOX. Table
Dim strKey As String
Dim strKeyName As String
V_DB.ActiveConnection = CurrentProject. Connection
ObjTable. ParentCatalog = v_DB
Set objTable = v_DB.Tables ("your table name ")
For Each v_Key In objTable. Keys
Select Case v_Key.Type
Case adKeyPrimary
StrKey = "Primary KEY"
StrKeyName = v_Key.Name
End select
Next
Debug. Print "primary key constraint name:" & strkeyname
End sub
The above code must reference ADOX.
1 --> open the VBA code window
2 --> tool menu, reference menu item
3 --> select "Microsoft ADO Ext. 2.x for DLL and security"
Because I don't know VBA, and I don't know where to view the execution result during running, I have to use a stupid method to debug it in one step and execute it in one step, in the last step, place the cursor over the strkeyname variable to display the current value. (Please let us know how to view the execution result. Thank you very much ).
Note that the names of the primary keys created by using code in different places of the same database file are different. Remember this.