Selecting a Unique Record Identifier dynamically with vba?

﹥>﹥吖頭↗ 提交于 2019-12-12 03:44:00

问题


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:


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.



来源:https://stackoverflow.com/questions/38254776/selecting-a-unique-record-identifier-dynamically-with-vba

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!