I have a database that i am trying to eliminate duplicate entries the fields
are FirstName,LastName, WorkPhone, and HomePhone. I realize i can have more
than 1 client with the same name that is why i am using phone as well. I
started at the table level and made a unique index of the 4. That doesnt work
because i might only have 1 phone number or the other. I do not know vb at
all is there a way to make this work
Well... it's a bad idea, I fear.
Some people don't have phones.
I know a Fred Brown, Sr. and his son Fred Brown, Jr. Fred Jr. is out
of the house now but he once lived with his parents. That's a very
legitimate duplicate (unless you include the Suffix as part of the
name).
A better solution might be to CHECK for duplicates, say in the Form's
BeforeUpdate event; open a recordset or use DLookUp to see if the name
already exists and WARN the user that it's an apparent duplicate, and
let them choose whether to cancel or not. This might be something
like:
Private Sub Form_BeforeUpdate(Cancel as Integer)
Dim rs As DAO.Recordset
Dim strSQL As String
Dim strMsg As String
Dim iAns As Integer
Set rs = Me.RecordsetClone ' get the form's records
' set up a query string resembling
' [LastName] = "O'Brian" AND [FirstName] = "Ryan"
strSQL = "[LastName] = " & Chr(34) & Me.LastName & Chr(34)_
& " AND [FirstName] = " & Chr(34) & Me.FirstName & Chr(34)
' See if there is such a person in the table
rs.FindFirst strSQL
If Not rs.NoMatch Then
strMsg = Me.[FirstName] & " " & Me.[LastName] _
& " already found. Add anyway?" & vbCrLf _
& "Yes to add, No to jump to that person, " _
& "Cancel to try another name:"
iAns = MsgBox(strMsg, vbYesNoCancel)
Select Case iAns
Case vbYes ' add anyway
' do nothing
Case vbNo ' Cancel edits, jump to record
Me.Undo
Cancel = True
Me.Bookmark = rs.Bookmark
Case vbCancel ' just cancel the add
Cancel = True
End Select
End If
End Sub
John W. Vinson [MVP]