M
mscertified
I have some code that adds records to a table. The table has 2 columns which
together form a unique key. When inserting values I need to check for a
duplicate error. Currently, I check for error -2147467259. However, I found
this also is raised when the referential integrity checks fail (this table is
linked to another with referential integrity checks). Is there any way to
isolate which failure occured (without doing a READ first)?
Here is my code now:
Private Sub butAdd_Click()
' Add keyword selections, Allow for multi-selections, Handle no selection
On Error GoTo ER
Dim strSQL As String
Dim varItem As Variant
If Me!lstKW.ItemsSelected.Count = 0 Then
MsgBox "Please select a Keyword to Add", vbOKOnly, "Keyword"
Me!lstKW.SetFocus
Exit Sub
End If
' Need to check for duplicates
With Me!lstKW
For Each varItem In .ItemsSelected
strSQL = "INSERT INTO tblKWXref (TicketID,KWID) VALUES (" & _
lngTicket & "," & .Column(0, varItem) & ")"
On Error Resume Next
CurrentProject.Connection.Execute strSQL
If Err.Number = -2147467259 Then
MsgBox "Keyword already selected", , "Duplicate"
Err.Clear
End If
On Error GoTo ER
.Selected(varItem) = False
Next
End With
Me!lstKWSel.Requery
Exit Sub
ER:
MsgBox Err.Description, , "butAdd_Click (" & Err.Number & ")"
End Sub
together form a unique key. When inserting values I need to check for a
duplicate error. Currently, I check for error -2147467259. However, I found
this also is raised when the referential integrity checks fail (this table is
linked to another with referential integrity checks). Is there any way to
isolate which failure occured (without doing a READ first)?
Here is my code now:
Private Sub butAdd_Click()
' Add keyword selections, Allow for multi-selections, Handle no selection
On Error GoTo ER
Dim strSQL As String
Dim varItem As Variant
If Me!lstKW.ItemsSelected.Count = 0 Then
MsgBox "Please select a Keyword to Add", vbOKOnly, "Keyword"
Me!lstKW.SetFocus
Exit Sub
End If
' Need to check for duplicates
With Me!lstKW
For Each varItem In .ItemsSelected
strSQL = "INSERT INTO tblKWXref (TicketID,KWID) VALUES (" & _
lngTicket & "," & .Column(0, varItem) & ")"
On Error Resume Next
CurrentProject.Connection.Execute strSQL
If Err.Number = -2147467259 Then
MsgBox "Keyword already selected", , "Duplicate"
Err.Clear
End If
On Error GoTo ER
.Selected(varItem) = False
Next
End With
Me!lstKWSel.Requery
Exit Sub
ER:
MsgBox Err.Description, , "butAdd_Click (" & Err.Number & ")"
End Sub