Steve Sanford checking for duplicate code!!

R

riccifs

Hi to everyone,
I know how to check for duplicates entries for a single field or for a
pair of fields, but now I am realizing to have a different problem.

Image that I registered in a filed of my form a person named John
McDonald and a couple of mouths later I do another registration but
this time, for a my true typing mistake, I write John McDonold with
the "o" instead the "a" in McDonald. For the db, of course, They are
two different persons and leave my insert both of them.

The question is: How do I prevent something like that to happen? I
mean is it possible to make a string compare and for example, if at
least the 80% of the words in the strings are the same I will receive
a Msgbox that alert me to a possible duplicate record, and leave to me
to decide what to do.

At moment the code I am using is the one that came from Steve Sanford.
It works great but I'd like to implement it in the way I described
above.
You can get the entire discussion at this link:
http://groups.google.it/group/micro...a744c767?lnk=gst&q=duplicate#df71c244a744c767

'-------------------------------
Private Sub Form_BeforeUpdate(Cancel As Integer)
Cancel = DuplicateCheck
End Sub

Private Sub tbDOB_Exit(Cancel As Integer)
Cancel = DuplicateCheck
End Sub
'-------------------------------

Here is the function to check for dups:

'------beg code-------------------------
Function DuplicateCheck() As Boolean

Dim SID As String
Dim stLinkCriteria As String
Dim rsc As DAO.Recordset

Dim mPrompt As String
Dim mButtons, mTitle, Response

'check if change to form data
If Not Me.Dirty Then
Exit Function
End If

Set rsc = Me.RecordsetClone

'for message box
mPrompt = "Warning: Possible Duplicate Record" & vbCr & vbCr
mPrompt = mPrompt & "Do you want to continue to add this record?" &
vbCr
& vbCr
mPrompt = mPrompt & "Select YES to add the record" & vbCr & vbCr
mPrompt = mPrompt & "Select NO to be taken to the record."
mButtons = vbYesNo + vbCritical + vbDefaultButton2
mTitle = "Duplicate Information"

'check that all criteria are entered
If IsNull(Me.tbLastName) Then
MsgBox "Last name is required!!", vbOKOnly + vbExclamation
DuplicateCheck = True
Me.tbLastName.SetFocus
Exit Function
End If
If IsNull(Me.tbFirstName) Then
MsgBox "First name is required!!", vbOKOnly + vbExclamation
DuplicateCheck = True
Me.tbFirstName.SetFocus
Exit Function
End If
If IsNull(Me.tbDOB) Then
MsgBox "Date of Birth is required!!", vbOKOnly + vbExclamation
'
DuplicateCheck = True
Me.tbDOB.SetFocus
Exit Function
End If

stLinkCriteria = "[strLastName]= '" & Me.tbLastName & "'"
stLinkCriteria = stLinkCriteria & " And [strFirstName]= '" &
Me.tbFirstName & "'"
stLinkCriteria = stLinkCriteria & " And [dtmDOB] = #" & Me.tbDOB &
"# "

rsc.FindFirst stLinkCriteria

'next 3 lines are for testing
' Debug.Print rsc!strLastName
' Debug.Print rsc!strFirstName
' Debug.Print rsc!dtmDOB

'always check the NoMatch property after a find
' rsc.NoMatch = TRUE means no duplicates
' rsc.NoMatch = FALSE means there are duplicates

' if rsc.NoMatch = FALSE, then NOT rsc.NoMatch is TRUE
' I hate negative logic!!!

'********changes here*************
If Not rsc.NoMatch Then
'possible duplicate record found
'ask if they want to goto the record or continue to add record
Response = MsgBox(mPrompt, mButtons, mTitle)
If Response = vbNo Then ' User chose No
'undo entries and goto record
Me.Undo
Me.Bookmark = rsc.Bookmark
Me.tbLastName.SetFocus
End If
End If
'*********************************
DuplicateCheck = False

'clean up
rsc.Close
Set rsc = Nothing

End Function
'------end code-------------------------

Hope someone will help me, may be Steve Sanford....
Many thanks to everyone,
Stefano.
 
D

Douglas J. Steele

You might be able to use SoundEx to see whether the values are "close
enough" to be of concern.

I wrote about how to implement SoundEx (and another related technique, the
Levenshtein Distance algorithm) in my April, 2005 "Access Answers" column in
Pinnacle Publication's "Smart Access". You can download the column (and
sample database) for free from
http://www.accessmvp.com/DJSteele/SmartAccess.html

--
Doug Steele, Microsoft Access MVP

(no e-mails, please!)


Hi to everyone,
I know how to check for duplicates entries for a single field or for a
pair of fields, but now I am realizing to have a different problem.

Image that I registered in a filed of my form a person named John
McDonald and a couple of mouths later I do another registration but
this time, for a my true typing mistake, I write John McDonold with
the "o" instead the "a" in McDonald. For the db, of course, They are
two different persons and leave my insert both of them.

The question is: How do I prevent something like that to happen? I
mean is it possible to make a string compare and for example, if at
least the 80% of the words in the strings are the same I will receive
a Msgbox that alert me to a possible duplicate record, and leave to me
to decide what to do.

At moment the code I am using is the one that came from Steve Sanford.
It works great but I'd like to implement it in the way I described
above.
You can get the entire discussion at this link:
http://groups.google.it/group/micro...a744c767?lnk=gst&q=duplicate#df71c244a744c767

'-------------------------------
Private Sub Form_BeforeUpdate(Cancel As Integer)
Cancel = DuplicateCheck
End Sub

Private Sub tbDOB_Exit(Cancel As Integer)
Cancel = DuplicateCheck
End Sub
'-------------------------------

Here is the function to check for dups:

'------beg code-------------------------
Function DuplicateCheck() As Boolean

Dim SID As String
Dim stLinkCriteria As String
Dim rsc As DAO.Recordset

Dim mPrompt As String
Dim mButtons, mTitle, Response

'check if change to form data
If Not Me.Dirty Then
Exit Function
End If

Set rsc = Me.RecordsetClone

'for message box
mPrompt = "Warning: Possible Duplicate Record" & vbCr & vbCr
mPrompt = mPrompt & "Do you want to continue to add this record?" &
vbCr
& vbCr
mPrompt = mPrompt & "Select YES to add the record" & vbCr & vbCr
mPrompt = mPrompt & "Select NO to be taken to the record."
mButtons = vbYesNo + vbCritical + vbDefaultButton2
mTitle = "Duplicate Information"

'check that all criteria are entered
If IsNull(Me.tbLastName) Then
MsgBox "Last name is required!!", vbOKOnly + vbExclamation
DuplicateCheck = True
Me.tbLastName.SetFocus
Exit Function
End If
If IsNull(Me.tbFirstName) Then
MsgBox "First name is required!!", vbOKOnly + vbExclamation
DuplicateCheck = True
Me.tbFirstName.SetFocus
Exit Function
End If
If IsNull(Me.tbDOB) Then
MsgBox "Date of Birth is required!!", vbOKOnly + vbExclamation
'
DuplicateCheck = True
Me.tbDOB.SetFocus
Exit Function
End If

stLinkCriteria = "[strLastName]= '" & Me.tbLastName & "'"
stLinkCriteria = stLinkCriteria & " And [strFirstName]= '" &
Me.tbFirstName & "'"
stLinkCriteria = stLinkCriteria & " And [dtmDOB] = #" & Me.tbDOB &
"# "

rsc.FindFirst stLinkCriteria

'next 3 lines are for testing
' Debug.Print rsc!strLastName
' Debug.Print rsc!strFirstName
' Debug.Print rsc!dtmDOB

'always check the NoMatch property after a find
' rsc.NoMatch = TRUE means no duplicates
' rsc.NoMatch = FALSE means there are duplicates

' if rsc.NoMatch = FALSE, then NOT rsc.NoMatch is TRUE
' I hate negative logic!!!

'********changes here*************
If Not rsc.NoMatch Then
'possible duplicate record found
'ask if they want to goto the record or continue to add record
Response = MsgBox(mPrompt, mButtons, mTitle)
If Response = vbNo Then ' User chose No
'undo entries and goto record
Me.Undo
Me.Bookmark = rsc.Bookmark
Me.tbLastName.SetFocus
End If
End If
'*********************************
DuplicateCheck = False

'clean up
rsc.Close
Set rsc = Nothing

End Function
'------end code-------------------------

Hope someone will help me, may be Steve Sanford....
Many thanks to everyone,
Stefano.
 
R

riccifs

You might be able to use SoundEx to see whether the values are "close
enough" to be of concern.

I wrote about how to implement SoundEx (and another related technique, the
Levenshtein Distance algorithm) in my April, 2005 "Access Answers" column in
Pinnacle Publication's "Smart Access". You can download the column (and
sample database) for free fromhttp://www.accessmvp.com/DJSteele/SmartAccess.html

--
Doug Steele, Microsoft Access MVPhttp://I.Am/DougSteele
(no e-mails, please!)


Hi to everyone,
I know how to check for duplicates entries for a single field or for a
pair of fields, but now I am realizing to have a different problem.
Image that I registered in a filed of my form a person named John
McDonald and a couple of mouths later I do another registration but
this time, for a my true typing mistake, I write John McDonold with
the "o" instead the "a" in McDonald. For the db, of course, They are
two different persons and leave my insert both of them.
The question is: How do I prevent something like that to happen? I
mean is it possible to make a string compare and for example, if at
least the 80% of the words in the strings are the same I will receive
a Msgbox that alert me to a possible duplicate record, and leave to me
to decide what to do.
At moment the code I am using is the one that came from Steve Sanford.
It works great but I'd like to implement it in the way I described
above.
You can get the entire discussion at this link:
http://groups.google.it/group/microsoft.public.access.formscoding/bro...
'-------------------------------
Private Sub Form_BeforeUpdate(Cancel As Integer)
Cancel = DuplicateCheck
End Sub
Private Sub tbDOB_Exit(Cancel As Integer)
Cancel = DuplicateCheck
End Sub
'-------------------------------
Here is the function to check for dups:
'------beg code-------------------------
Function DuplicateCheck() As Boolean
Dim SID As String
Dim stLinkCriteria As String
Dim rsc As DAO.Recordset
Dim mPrompt As String
Dim mButtons, mTitle, Response
'check if change to form data
If Not Me.Dirty Then
Exit Function
End If
Set rsc = Me.RecordsetClone
'for message box
mPrompt = "Warning: Possible Duplicate Record" & vbCr & vbCr
mPrompt = mPrompt & "Do you want to continue to add this record?" &
vbCr
& vbCr
mPrompt = mPrompt & "Select YES to add the record" & vbCr & vbCr
mPrompt = mPrompt & "Select NO to be taken to the record."
mButtons = vbYesNo + vbCritical + vbDefaultButton2
mTitle = "Duplicate Information"
'check that all criteria are entered
If IsNull(Me.tbLastName) Then
MsgBox "Last name is required!!", vbOKOnly + vbExclamation
DuplicateCheck = True
Me.tbLastName.SetFocus
Exit Function
End If
If IsNull(Me.tbFirstName) Then
MsgBox "First name is required!!", vbOKOnly + vbExclamation
DuplicateCheck = True
Me.tbFirstName.SetFocus
Exit Function
End If
If IsNull(Me.tbDOB) Then
MsgBox "Date of Birth is required!!", vbOKOnly + vbExclamation
'
DuplicateCheck = True
Me.tbDOB.SetFocus
Exit Function
End If
stLinkCriteria = "[strLastName]= '" & Me.tbLastName & "'"
stLinkCriteria = stLinkCriteria & " And [strFirstName]= '" &
Me.tbFirstName & "'"
stLinkCriteria = stLinkCriteria & " And [dtmDOB] = #" & Me.tbDOB &
"# "
rsc.FindFirst stLinkCriteria
'next 3 lines are for testing
' Debug.Print rsc!strLastName
' Debug.Print rsc!strFirstName
' Debug.Print rsc!dtmDOB
'always check the NoMatch property after a find
' rsc.NoMatch = TRUE means no duplicates
' rsc.NoMatch = FALSE means there are duplicates
' if rsc.NoMatch = FALSE, then NOT rsc.NoMatch is TRUE
' I hate negative logic!!!
'********changes here*************
If Not rsc.NoMatch Then
'possible duplicate record found
'ask if they want to goto the record or continue to add record
Response = MsgBox(mPrompt, mButtons, mTitle)
If Response = vbNo Then ' User chose No
'undo entries and goto record
Me.Undo
Me.Bookmark = rsc.Bookmark
Me.tbLastName.SetFocus
End If
End If
'*********************************
DuplicateCheck = False
'clean up
rsc.Close
Set rsc = Nothing
End Function
'------end code-------------------------
Hope someone will help me, may be Steve Sanford....
Many thanks to everyone,
Stefano.

Hi Doug,
I downloaded the sample db and I think it's simply clever and it's
just what I was looking for!
But unfortunately I have no idea how to make it works like a duplicate
checker on my form, I'm not able to adapt it for my purpose!
What I was thinking by myself is to call the your function in place of
the "stLinkCriteria".... but I'm not sure it would be the right
approach!

Could you help me in same way, please?
Many thanks again,
Stefano.
 
D

Douglas J. Steele

Try replacing

stLinkCriteria = "[strLastName]= '" & Me.tbLastName & "'"
stLinkCriteria = stLinkCriteria & " And [strFirstName]= '" &
Me.tbFirstName & "'"

with

stLinkCriteria = "SoundEx([strLastName])= '" & _
SoundEx(Me.tbLastName) & "'"
stLinkCriteria = stLinkCriteria & _
" And SoundEx([strFirstName])= '" &
SoundEx(Me.tbFirstName) & "'"

--
Doug Steele, Microsoft Access MVP

(no e-mails, please!)


Hi Doug,
I downloaded the sample db and I think it's simply clever and it's
just what I was looking for!
But unfortunately I have no idea how to make it works like a duplicate
checker on my form, I'm not able to adapt it for my purpose!
What I was thinking by myself is to call the your function in place of
the "stLinkCriteria".... but I'm not sure it would be the right
approach!
 
R

riccifs

Try replacing

stLinkCriteria = "[strLastName]= '" & Me.tbLastName & "'"
stLinkCriteria = stLinkCriteria & " And [strFirstName]= '" &
Me.tbFirstName & "'"

with

stLinkCriteria = "SoundEx([strLastName])= '" & _
SoundEx(Me.tbLastName) & "'"
stLinkCriteria = stLinkCriteria & _
" And SoundEx([strFirstName])= '" &
SoundEx(Me.tbFirstName) & "'"

--
Doug Steele, Microsoft Access MVPhttp://I.Am/DougSteele
(no e-mails, please!)








Hi Doug,
I downloaded the sample db and I think it's simply clever and it's
just what I was looking for!
But unfortunately I have no idea how to make it works like a duplicate
checker on my form, I'm not able to adapt it for my purpose!
What I was thinking by myself is to call the your function in place of
the "stLinkCriteria".... but I'm not sure it would be the right
approach!

I imported the mdlSoundex into my db and I replaced what you said but
it's giving to me this kind of error:
"compile error Sub or Function not defined" and it's highlighted the
SoundEx string.

What I'm doing wrong?
 
D

Douglas J. Steele

My mistake. The function in the module is named GetSoundex, not just
SoundEx.

stLinkCriteria = "GetSoundex([strLastName])= '" & _
GetSoundex(Me.tbLastName) & "'"
stLinkCriteria = stLinkCriteria & _
" And GetSoundex([strFirstName])= '" &
GetSoundex(Me.tbFirstName) & "'"

Sorry about that.

--
Doug Steele, Microsoft Access MVP

(no e-mails, please!)


I imported the mdlSoundex into my db and I replaced what you said but
it's giving to me this kind of error:
"compile error Sub or Function not defined" and it's highlighted the
SoundEx string.

What I'm doing wrong?
Try replacing

stLinkCriteria = "[strLastName]= '" & Me.tbLastName & "'"
stLinkCriteria = stLinkCriteria & " And [strFirstName]= '" &
Me.tbFirstName & "'"

with

stLinkCriteria = "SoundEx([strLastName])= '" & _
SoundEx(Me.tbLastName) & "'"
stLinkCriteria = stLinkCriteria & _
" And SoundEx([strFirstName])= '" &
SoundEx(Me.tbFirstName) & "'"
 
R

riccifs

My mistake. The function in the module is named GetSoundex, not just
SoundEx.

stLinkCriteria = "GetSoundex([strLastName])= '" & _
GetSoundex(Me.tbLastName) & "'"
stLinkCriteria = stLinkCriteria & _
" And GetSoundex([strFirstName])= '" &
GetSoundex(Me.tbFirstName) & "'"

Sorry about that.

--
Doug Steele, Microsoft Access MVPhttp://I.Am/DougSteele
(no e-mails, please!)




I imported the mdlSoundex into my db and I replaced what you said but
it's giving to me this kind of error:
"compile error Sub or Function not defined" and it's highlighted the
SoundEx string.
What I'm doing wrong?
Try replacing
stLinkCriteria = "[strLastName]= '" & Me.tbLastName & "'"
stLinkCriteria = stLinkCriteria & " And [strFirstName]= '" &
Me.tbFirstName & "'"
with
stLinkCriteria = "SoundEx([strLastName])= '" & _
SoundEx(Me.tbLastName) & "'"
stLinkCriteria = stLinkCriteria & _
" And SoundEx([strFirstName])= '" &
SoundEx(Me.tbFirstName) & "'"

Sound good...!! I believe it's the case to said that now!
It's working very well now.
If I would like to use the Levenshtein module instead how the code
will look like?
I'm asking that because I'd like to insert a distance box in the form
as well. In that way I will be able to control the less or the more
precision to be closed of the insert word.

Many many thanks for the help you give to me,
Stefano.
 
R

riccifs

My mistake. The function in the module is named GetSoundex, not just
SoundEx.
stLinkCriteria = "GetSoundex([strLastName])= '" & _
GetSoundex(Me.tbLastName) & "'"
stLinkCriteria = stLinkCriteria & _
" And GetSoundex([strFirstName])= '" &
GetSoundex(Me.tbFirstName) & "'"
Sorry about that.
I imported the mdlSoundex into my db and I replaced what you said but
it's giving to me this kind of error:
"compile error Sub or Function not defined" and it's highlighted the
SoundEx string.
What I'm doing wrong?
On 9 Nov, 18:34, "Douglas J. Steele"
Try replacing
stLinkCriteria = "[strLastName]= '" & Me.tbLastName & "'"
stLinkCriteria = stLinkCriteria & " And [strFirstName]= '" &
Me.tbFirstName & "'"
with
stLinkCriteria = "SoundEx([strLastName])= '" & _
SoundEx(Me.tbLastName) & "'"
stLinkCriteria = stLinkCriteria & _
" And SoundEx([strFirstName])= '" &
SoundEx(Me.tbFirstName) & "'"

Sound good...!! I believe it's the case to said that now!
It's working very well now.
If I would like to use the Levenshtein module instead how the code
will look like?
I'm asking that because I'd like to insert a distance box in the form
as well. In that way I will be able to control the less or the more
precision to be closed of the insert word.

Many many thanks for the help you give to me,
Stefano.

Can I use it to check Italian word? or it makes no sense to use the
function for Italian language?

Stefano.
 
D

Douglas J. Steele

Take a look in the sample database!

You need to define a threshold distance value (let's assume it's in text box
txtDistance on your form), and then use something like:

stLinkCriteria = "LevenshteinDistance([strLastName], """ & _
Me.tbLastName & """) <= " & Me.txtDistance & " "
stLinkCriteria = stLinkCriteria & _
" And LevenshteinDistance([strFirstName]), """ &
Me.tbFirstName & """) <= " & Me.txtDistance & " "
 
D

Douglas J. Steele

I don't know. Try it and see.

Do some searching, too, to see whether there might be a different version of
SoundEx for Italian.
 
R

riccifs

My mistake. The function in the module is named GetSoundex, not just
SoundEx.
stLinkCriteria = "GetSoundex([strLastName])= '" & _
GetSoundex(Me.tbLastName) & "'"
stLinkCriteria = stLinkCriteria & _
" And GetSoundex([strFirstName])= '" &
GetSoundex(Me.tbFirstName) & "'"
Sorry about that.
I imported the mdlSoundex into my db and I replaced what you said but
it's giving to me this kind of error:
"compile error Sub or Function not defined" and it's highlighted the
SoundEx string.
What I'm doing wrong?
On 9 Nov, 18:34, "Douglas J. Steele"
Try replacing
stLinkCriteria = "[strLastName]= '" & Me.tbLastName & "'"
stLinkCriteria = stLinkCriteria & " And [strFirstName]= '" &
Me.tbFirstName & "'"
with
stLinkCriteria = "SoundEx([strLastName])= '" & _
SoundEx(Me.tbLastName) & "'"
stLinkCriteria = stLinkCriteria & _
" And SoundEx([strFirstName])= '" &
SoundEx(Me.tbFirstName) & "'"

Sound good...!! I believe it's the case to said that now!
It's working very well now.
If I would like to use the Levenshtein module instead how the code
will look like?
I'm asking that because I'd like to insert a distance box in the form
as well. In that way I will be able to control the less or the more
precision to be closed of the insert word.

Many many thanks for the help you give to me,
Stefano.

Many thanks for your help, I really do appreciate that!
Bye Stefano.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top