1

I have an Access database which has a number of linked tables linking to views in a SQL Server database. When you refresh the links to the views, Access asks for the field(s) which contain unique values, in order to build a unique ID index.

My question is, once the link is established, is it possible to see which fields were specified as the unique index? I cannot see anything in the properties of each linked table, or anything in the indexes in design view.

Thanks as always for reading and for any help,

9
  • When you right click on the linked table on the object pane on the left, do you see a key symbol next to any of the fields? If so, then those would be what Access considers as the primary key. Commented Nov 3, 2023 at 15:32
  • Hi Morty, I am not sure what you mean. In Access you cannot see the column fields in the Nav Pane. If I open the linked table in design view, none of the fields have a PK icon. Commented Nov 3, 2023 at 15:43
  • Yes, sorry it was a typo. I meant to open the linked table in design view, which you did. The next thing I would try is closing/reopening Access, then checking the design view again for the PK symbol. If that doesn't work, go to the design view of one of the tables and click on one of the fields that you remember that you designated as unique, then check the grid below to see if that field has a property "Indexed" set to "Yes (No Duplicates)". Commented Nov 3, 2023 at 16:15
  • Assuming CurrentDb.TableDefs("LinkName").Indexes.Count > 0, examine the properties of its first index: CurrentDb.TableDefs("LinkName").Indexes(0) and its Fields collection. Anything useful there? Commented Nov 3, 2023 at 16:20
  • 1
    When you refresh the links to the views, Access asks for the field(s) which contain unique values - perhaps this doesn't save this info to the tabledef. Try removing the linked view, and re-linking it from the Access GUI. Normally the PK field(s) are always visible in design view of linked Views. Commented Nov 3, 2023 at 19:06

1 Answer 1

2

Not only can you get this information, but in theory, if you write your own VBA re-link code, then WHEN you re-link to a different back end (SQL server), then you lose the settings for the PK (primary key value) column chosen at runtime.

In other words, while you can use code (or the Access UI) to re-fresh/relink the table(s) to SQL server "views" in this example? Access WILL remember the settings for PK values, but NOT WHEN you change or point to a different server.

The above information can thus be quite significant, since as a developer, we often will take a SQL backup of the production database, and restore to our local copy of SQL express, and thus use that to "test" and develop our software.

When all is happy and fine, then right before deployment, we will:

Link our front end to the live production database. We then compile the accDB to an accDE, and then the new great version of our software is distributed (installed) to each desktop computer.

However, as I stated, WHEN you relink to a DIFFERENT server and/or database, then you LOSE the PK settings, and thus after a VBA re-link, then those view's in question will become read only.

So, to get/see/find the columns "chosen" at link time?

You can use this code:

    Function ViewPK(strTable As String) As String
    
       Dim db       As DAO.Database
       Dim ix       As DAO.Index
       
       Set db = CurrentDb
       
       For Each ix In db.TableDefs(strTable).Indexes
          If ix.Primary = True Then
             ViewPK = ix.Fields
             Exit For
          End If
       Next
       
    End Function
    

Note that the columns come back as field names (with a "+" prefix), and then each column separated by a ";".

So, if you picked id, and then FirstName, then above would return

 +ID;+FirstName

So, in theory, to get/see, list out the above, then

Sub Test1()

    Dim sResult     As String
    Dim vFields     As Variant
    Dim vF          As Variant
    
    sResult = ViewPK("dbo_FightersV")
    
    vFields = Split(sResult, ";")
    
    For Each vF In vFields
    
        Debug.Print Replace(vF, "+", "")
        
    Next
            
    
End Sub
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.