Selecting a Unique Record Identifier dynamically w

2019-09-17 06:54发布

I'm trying to automate the process of adding a linked table in MS Access.

I've gotten it to work in so far as adding the linked 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

...but then a dialog box pops up requesting a primary key or as it calls it a Unique Record Identifier. Is there anyway around this?

1条回答
对你真心纯属浪费
2楼-- · 2019-09-17 07:20

Okay found the answer on an old MS-Access forum; using this method just links the table up, no questions asked...and no primary keys...

' 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

You can set the primary key programmatically as well.

查看更多
登录 后发表回答