DCount Duplicates reading record as its own duplicate (1 Viewer)

eee333

Registered User.
Local time
Today, 08:44
Joined
Jan 11, 2016
Messages
36
I've been having some trouble with this piece of code with the straightforward task of preventing a user from entering a duplicate ID into the field SurveyID. However, as I've been testing the various ways a user can "break" my database design, I've noticed an issue. If the user is in an old record, and they accidentally started editing the field, but then re-type the correct survey ID (so clicked on it and started typing without noticing they're in the wrong field, then noticed their error and re-typed in their correct ID 10002), it still reads it as a duplicate (even though it's...it's own duplicate) and gives them the error message. I've already figured out a slight workaround so that when this happens the value of the field is reset, but since receiving the "this is a duplicate" message could confuse users, I'm trying to find a solution that just prevents it from happening in the first place.

It may be useful to note that SurveyID is NOT the primary key in this case. I'm wondering whether that may be the key to my solution, somethign saying that if the primary key of this record and the primary key of its "duplicate" are the same, to not proceed--but I'm not sure how to do it.

Here is my code
Code:
 Private Sub SurveyID_BeforeUpdate(Cancel As Integer)
'checks for duplicates'
If DCount("SurveyID", "test", "SurveyID=" & Nz(Me.SurveyID, 0)) > 0 Then
    Beep
    MsgBox "The Survey ID number you have entered is a duplicate. Please double check that the number you entered is correct. If it is correct, please X."
    Me.SurveyID.Undo
    Cancel = True
End If
End Sub
 

Ranman256

Well-known member
Local time
Today, 08:44
Joined
Apr 9, 2015
Messages
4,337
can you not KEY the field and prevent ANY duplicates?
 

eee333

Registered User.
Local time
Today, 08:44
Joined
Jan 11, 2016
Messages
36
I believe not (unless I'm misunderstanding you) because there are two different fields people may use as IDs, so I can't do that. And I already have it set to just prevent duplicates, but I want it to tell them it's a duplicate right away, not when they try to proceed to a new record.
 

Alc

Registered User.
Local time
Today, 08:44
Joined
Mar 23, 2007
Messages
2,407
If it definitely counts the new record in amongst the check, would it not work to change this
Code:
 If DCount("SurveyID", "test", "SurveyID=" & Nz(Me.SurveyID, 0)) > 0 Then
to this?
Code:
 If DCount("SurveyID", "test", "SurveyID=" & Nz(Me.SurveyID, 0)) > 1 Then
 

Ranman256

Well-known member
Local time
Today, 08:44
Joined
Apr 9, 2015
Messages
4,337
then I am confused.
if you are preventing duplicates, how are they entering duplicates?
 

Ranman256

Well-known member
Local time
Today, 08:44
Joined
Apr 9, 2015
Messages
4,337
how about an empty screen except for the ID#
they enter, then its either duplicate and open it
or new and start new.
 

eee333

Registered User.
Local time
Today, 08:44
Joined
Jan 11, 2016
Messages
36
then I am confused.
if you are preventing duplicates, how are they entering duplicates?

because it gives you that system notification when you try to navigate away from the record, not when you first enter it into the field
 

eee333

Registered User.
Local time
Today, 08:44
Joined
Jan 11, 2016
Messages
36
If it definitely counts the new record in amongst the check, would it not work to change this
Code:
 If DCount("SurveyID", "test", "SurveyID=" & Nz(Me.SurveyID, 0)) > 0 Then
to this?
Code:
 If DCount("SurveyID", "test", "SurveyID=" & Nz(Me.SurveyID, 0)) > 1 Then

Because then it allows you to enter an actual duplicate.
 

eee333

Registered User.
Local time
Today, 08:44
Joined
Jan 11, 2016
Messages
36
how about an empty screen except for the ID#
they enter, then its either duplicate and open it
or new and start new.

that's really not the user interface I want to have. It won't work for my purposes.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 20:44
Joined
May 7, 2009
Messages
19,246
you should test if the SurveID old value is the same as the SurveID new value, also if it is a New record:

Code:
Private Sub SurveyID_BeforeUpdate(Cancel As Integer)
dim lngOldID as long
Dim lngNewID As Long
lngOldID = nz(Me.SurveyID.OldValue,0)
lngNewID = NZ(Me.SurveyID.Value, 0)
'checks for duplicates'
If lngOldID <> lngNewID Or Me.NewRecord Then
If DCount("SurveyID", "test", "SurveyID=" & lngNewID) > 0 Then
    Beep
    MsgBox "The Survey ID number you have entered is a duplicate. Please double check that the number you entered is correct. If it is correct, please X."
    Me.SurveyID.Undo
    Cancel = True
End If
End If
End Sub
 

Cronk

Registered User.
Local time
Today, 22:44
Joined
Jul 4, 2013
Messages
2,774
The simplest way of preventing duplicate SurveyIDs in the table, is to add a unique index on that field.
 

eee333

Registered User.
Local time
Today, 08:44
Joined
Jan 11, 2016
Messages
36
you should test if the SurveID old value is the same as the SurveID new value, also if it is a New record:

Code:
Private Sub SurveyID_BeforeUpdate(Cancel As Integer)
dim lngOldID as long
Dim lngNewID As Long
lngOldID = nz(Me.SurveyID.OldValue,0)
lngNewID = NZ(Me.SurveyID.Value, 0)
'checks for duplicates'
If lngOldID <> lngNewID Or Me.NewRecord Then
If DCount("SurveyID", "test", "SurveyID=" & lngNewID) > 0 Then
    Beep
    MsgBox "The Survey ID number you have entered is a duplicate. Please double check that the number you entered is correct. If it is correct, please X."
    Me.SurveyID.Undo
    Cancel = True
End If
End If
End Sub

Yes, this worked perfectly, just what I needed! Thanks!
 

Users who are viewing this thread

Top Bottom