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