使用 vba 动态选择唯一记录标识符?

Selecting a Unique Record Identifier dynamically with vba?

我正在尝试自动执行在 MS Access 中添加链接 table 的过程。

就添加链接 table、

而言,我已经让它工作了
' RUN ME
Sub testItOut()

    Dim loginInfo As New AuthInfoz

    loginInfo.workgroup = "E:\xxxdatas\SEC\Secured.mdw"
    loginInfo.username = "XXXXX"
    loginInfo.password = "XXXX"
    loginInfo.dbs = "E:\xxxdatas\username\Report.mdb"

    Call DeleteRelinkToViewAndRename(loginInfo, "sometable", "ServiceProvision_Schema.hrpersnl")
End Sub

' Deletes the old table and relinks it
' to the new schema

Sub DeleteRelinkToViewAndRename(loginInfo As AuthInfoz, tblName, tblTarget)
    Dim objAccess As Access.application

    Dim view_ucp_consumers2_qf As DAO.QueryDef

    ' Open the thing manually first...
    'Shell """C:\Program Files (x86)\Microsoft Office\Office12\MSACCESS.EXE""" & " " & loginInfo.dbs
    Set objAccess = GetObject(loginInfo.dbs).application

    objAccess.CurrentDb

    ' Delete the Linked Table...THIS WORKS UNCOMMENT LATER...
    objAccess.DoCmd.DeleteObject acTable, tblName

    ' Relink the old table to the new schema
    ' THIS IS WHERE THE DIALOG APPEARS
    objAccess.DoCmd.TransferDatabase _
        acLink, _
        "ODBC Database", _
        "ODBC;DSN=MEDSN;Database=MEDATABASE;Trusted_Connection=Yes", _
        acTable, _
        tblTarget, _
        tblName
    ' Close out...
    objAccess.Quit acQuitSaveAll

End Sub

...但是随后会弹出一个对话框,要求提供主键或称其为唯一记录标识符。反正有这个吗?

好的,在一个旧的 MS-Access 论坛上找到了答案;使用此方法只是将 table 链接起来,没有问题...也没有主键...

' Deletes the old table and relinks it
' to the new schema
Sub DeleteRelinkToViewAndRename(loginInfo As AuthInfoz, tblName, tblTarget)

    Dim objAccess As Access.application
    Dim db As DAO.database

    Dim tdf As DAO.TableDef

    ' Open the thing manually first...
    'Shell """C:\Program Files (x86)\Microsoft Office\Office12\MSACCESS.EXE""" & " " & loginInfo.dbs
    Set objAccess = GetObject(loginInfo.dbs).application
    Set db = objAccess.CurrentDb

    ' Delete the Linked Table...THIS WORKS UNCOMMENT LATER...
    objAccess.DoCmd.DeleteObject acTable, tblName

    ' Relink the old table to the new schema
    Set tdf = db.CreateTableDef(tblName, 0, tblTarget, "ODBC;DSN=MEDSN;Database=MEDATABASE;Trusted_Connection=Yes")

    ' Add the new table def
    db.TableDefs.Append tdf

End Sub

您可以设置 .