【发布时间】:2013-11-29 00:40:54
【问题描述】:
我在要插入的表中的 2 列上有一个复合主键。我来自使用 SQL Server,我知道如果我尝试将重复的键值插入到 PK 表中,它会引发错误。
我的问题是,我的代码没有抛出这种错误。你能看看它,看看它是否是代码的问题?还是 Access 不会针对这种违规抛出错误?
[编辑]
我想我正在寻找一种方法来确认尝试插入重复记录。我希望保留当前功能(丢弃重复;插入有效记录)。我不希望整个 INSERT 回滚。
我的代码如下。
Function InsertData(Ignore As String)
' define file path of CSV to be imported
Dim CurrentDate As String
Dim CurrentYear As String
CurrentDate = Format(Date, "yyyymmdd")
CurrentYear = Format(Date, "yyyy")
Dim Exfile As String
Exfile = iPath + "\" + CurrentYear + "\" + "FileName" + CurrentDate + ".txt"
'this calls a saved import routine
DoCmd.RunSavedImportExport "tbl_TEMP"
'merge data with that already existing in tbl_Perm.
'the clustered PK on product_ID and As_of_Date prevents dup insertion
Dim dbs As Database
Dim errLoop As Error
Set dbs = OpenDatabase(iPath + "\ExDatabase.mdb")
dbs.Execute " INSERT INTO tbl_Perm (Col1,Col2,Date_Created) " _
& "SELECT ColA + ColB, ColC, Format$(Now(),'Short Date')" _
& "FROM tbl_TEMP;"
' Trap for errors, checking the Errors collection if necessary.
On Error GoTo Err_Execute
'delete temp table
dbs.Execute "DROP TABLE tbl_TEMP;"
dbs.Close
Err_Execute:
' Notify user of any errors that result from
' executing the query.
If DBEngine.Errors.Count > 0 Then
For Each errLoop In DBEngine.Errors
MsgBox "Error number: " & errLoop.Number & vbCr & _
errLoop.Description
Next errLoop
End If
Resume Next
End Function
【问题讨论】:
标签: sql ms-access error-handling vba