How to view the name of the primary key in acccess

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.