being somewhat new to Access and Vba I need the answer to a question. I want
to combine three fields into one. as FirstName+MI+LastName. I want to do
this after FirstName, MI, and LastName have entered. The reason for this it
to be able to check the created field for duplicates. Any suggestions
It is not NECESSARY to combine the three fields into one (you can check for
duplicates on three fields, or on ten fields).
It is not SUFFICIENT to check the three fields for duplicates; I once worked
with Dr. Lawrence David Wise and his colleague Dr. Lawrence David Wise. Names
are not unique.
And it is not APPROPRIATE to store the concatenated field redundantly in a
second table.
Instead, I'd use VBA code in a Form's BeforeUpdate event to *warn* the user
that they may be entering a duplicate - not prevent it, because it might be
legitimately a second person with the same name. You could use code like:
Private Sub Form_BeforeUpdate(Cancel as Integer)
Dim rs As DAO.Recordset ' define a Recordset object
Dim strSQL As String
Dim iAns as Integer
Set rs = Me.RecordsetClone ' get the form's Recordset
strSQL = "[FirstName] = """ & Me!txtFirstName & """
If Not IsNull(Me!txtMI) Then
strSQL = strSQL & " AND [MI] = """ & Me!txtMI & """"
End If
strSQL = strSQL & " AND [LastName] = """ & Me!txtLastName & """"
' strSQL will be something like
' [FirstName] = "Jane" AND [MI] = "A." AND [LastName] = "O'Neill"
rs.FindFirst strSQL ' find the first record with this name
If Not rs.NoMatch Then
iAns = MsgBox("Possible duplicate name. Go to it? Click Yes to do so," _
& " No to add new record, Cancel to erase this entry and start over", _
vbYesNoCancel)
Select Case iAns ' find out which button the user clicked
Case vbYes
Me.Undo ' erase the current record
Cancel = True ' cancel the update
Me.Bookmark = rs.Bookmark ' go to the found record
Case vbCancel
Me.Undo ' erase
Cancel = True
Case vbNo
' do nothing
End Select
End If
End Sub
John W. Vinson [MVP]