T
Tim
Access 2003
I have a Contacts form the with the Contacts table as the record source.
The field Initials is entered by the user and is the primary key for the
table.
The Before Update for this field has code to check for duplicates, fire a
message box if a duplicate is found, and go to the record with those initials.
The problem I'm having is that the code works sometimes but not always (when
it does not work, the generic Access message about not allowing duplicate
primary key is shown before thee record is saved- not helpful to the user) -
see below for code.
As an example, records with the Initials AK and AB exist. If I try to enter
a new record with the initials AK, the code fires and I get the message and
taken to the existing record. If I enter AB into a new record, I can leave
the Initials field without the code firing - I cannot create a new record and
get the Access message. Of the 17 records already present, 7 of the initials
cause the code to execute, but the other 10 do not.
Is any one able to see what I am missing?
Thank you
*Code*
Private Sub Initials_BeforeUpdate(Cancel As Integer)
Dim SID As String
Dim stLinkCriteria As String
Dim rsc As DAO.Recordset
Set rsc = Me.RecordsetClone
SID = Me.Initials.Value
stLinkCriteria = "[Initials]=" & "'" & SID & "'"
'Check Contacts table for duplicate Initials
If DCount("Initials", "tbl_Survey_Contacts", stLinkCriteria) > 0 Then
'Undo duplicate entry
Me.Undo
'Message box warning of duplication
MsgBox "Warning these Initials " _
& "'" & SID & "'" & " have already been entered." _
& vbCr & vbCr & "You will now been taken to the record." _
& vbCr & vbCr & "Please check to see that you are not entering
a duplicate Contact." _
& vbCr & vbCr & "Return if neccessary to enter a unique set of
Initials.", _
vbInformation, "Duplicate Information"
'Go to record of original Initial
rsc.FindFirst stLinkCriteria
Me.Bookmark = rsc.Bookmark
End If
Set rsc = Nothing
End Sub
I have a Contacts form the with the Contacts table as the record source.
The field Initials is entered by the user and is the primary key for the
table.
The Before Update for this field has code to check for duplicates, fire a
message box if a duplicate is found, and go to the record with those initials.
The problem I'm having is that the code works sometimes but not always (when
it does not work, the generic Access message about not allowing duplicate
primary key is shown before thee record is saved- not helpful to the user) -
see below for code.
As an example, records with the Initials AK and AB exist. If I try to enter
a new record with the initials AK, the code fires and I get the message and
taken to the existing record. If I enter AB into a new record, I can leave
the Initials field without the code firing - I cannot create a new record and
get the Access message. Of the 17 records already present, 7 of the initials
cause the code to execute, but the other 10 do not.
Is any one able to see what I am missing?
Thank you
*Code*
Private Sub Initials_BeforeUpdate(Cancel As Integer)
Dim SID As String
Dim stLinkCriteria As String
Dim rsc As DAO.Recordset
Set rsc = Me.RecordsetClone
SID = Me.Initials.Value
stLinkCriteria = "[Initials]=" & "'" & SID & "'"
'Check Contacts table for duplicate Initials
If DCount("Initials", "tbl_Survey_Contacts", stLinkCriteria) > 0 Then
'Undo duplicate entry
Me.Undo
'Message box warning of duplication
MsgBox "Warning these Initials " _
& "'" & SID & "'" & " have already been entered." _
& vbCr & vbCr & "You will now been taken to the record." _
& vbCr & vbCr & "Please check to see that you are not entering
a duplicate Contact." _
& vbCr & vbCr & "Return if neccessary to enter a unique set of
Initials.", _
vbInformation, "Duplicate Information"
'Go to record of original Initial
rsc.FindFirst stLinkCriteria
Me.Bookmark = rsc.Bookmark
End If
Set rsc = Nothing
End Sub