我正在尝试使用 DAO(在 MS-Access 中)将记录插入到表中,并且这样做,我收到错误 3022(这表明违反了唯一索引)。该错误是正确的,因为实际上尝试插入的记录具有已在表中找到的值。
现在,我想找出违反的唯一索引的名称。有人知道我怎么得到这个吗?
感谢任何指针勒内
以下是一些注意事项:
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
您也可以使用模式。