What is the name of the violating unique index constraint in dao / ms-access

余生颓废 提交于 2019-12-13 02:16:01

问题


I am trying to insert a record into a table with DAO (within MS-Access) and doing so, I receive an Error 3022 (which indicates that a unique index is violated). The error is correct since in fact the tried-to-insert record has a value which is already found in the table.

Now, I'd like to find out the name of the violated unique index. Does someone have a clue how I'd get this?

Thanks for any pointer René


回答1:


Here are some notes:

Sub WithADO()
''Reference: Microsoft ADO Ext x.x For DLL and Security
Dim catTables As ADOX.Catalog
Dim cn As ADODB.Connection
Dim ndx As Object

Set catTables = CreateObject("ADOX.Catalog")

Set cn = CreateObject("ADODB.Connection")
dbfile = "C:\Docs\LTD.mdb"

    cn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
              "Data Source=" & dbfile & ";"

Set catTables.ActiveConnection = cn

For Each ndx In catTables.Tables("Table1").Indexes
    strlist = ndx.Name & " " & ndx.Properties("Primary Key") & vbCrLf & strlist
Next
MsgBox strlist

Set catTables = Nothing

End Sub

Sub WithDAO()
''Reference: Microsoft DAO x.x Object Library
Dim db As DAO.Database
Dim tdf As TableDef
Dim ndx As Object

Set db = CurrentDb
Set tdf = db.TableDefs("Table1")

For Each ndx In tdf.Indexes

    If ndx.Primary = True Then
        MsgBox ndx.Name
    End If
Next
End Sub

You could also use schemas.



来源:https://stackoverflow.com/questions/1649049/what-is-the-name-of-the-violating-unique-index-constraint-in-dao-ms-access

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