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

时间:2016-07-07 20:33:42

标签: vba ms-access ms-access-2013 linked-tables

我正在尝试自动化在MS Access中添加链接表的过程。

到目前为止,我已经开始使用链接表了

' 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

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

1 个答案:

答案 0 :(得分:2)

好的,在旧的MS-Access论坛上找到答案;使用这种方法只需链接表格,没有问题......也没有主键......

//h2[.='Name' and not(ancestor::div/@class = 'abc')]

您可以设置primary key programmatically as well