?
Solved

Trap Duplicate Entries On First Field

Posted on 2008-10-01
7
Medium Priority
?
435 Views
Last Modified: 2013-11-28
There is a table in my database that contains information on clients (tblClients). Two fields in the table - ClientName and ClientID - are indexed, no dups allowed. The client name is the first field in the form and I would like to trap it if it is a duplicate of an exisiting name, before any more fields are entered. I've tried a macro for On Lost Focus, On Dirty, etc., but nothing has worked. Please advise!
0
Comment
Question by:Gazaway
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
7 Comments
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 22618825
what code are you using in those events you mentioned above?

have you tried using dcount or dlookup function

if dcount("ClientName","tblClients","[clientName]='" & me.clientName &"'")>0 then
msgbox "Duplicate client name found"
end if
0
 
LVL 2

Expert Comment

by:rene119
ID: 22619042
Hello Gazaway,

Here is one way to do it.
On your form in the on exit event for the Client Name field add the code below.
The only thing you will have to check is that you have Microsoft DAO  enabled in your references.
To check go to modules and select new then click on Tools at the top of the screen and select references look in the list and make sure there is a check mark in front of the Microsoft DAO. If you have multiples select the hightst version I use 3.6. Then exit the module.
Let me know if you have any questions.

Rene
Private Sub ClientName_Exit(Cancel As Integer)
    'variables
    Dim db As Database
    Dim rec As DAO.Recordset
    
    'run the procedure only if you are on a new record
    If Me.NewRecord = True Then
    
    'set our objects
    Set db = CurrentDb()
    Set rec = db.OpenRecordset("tblClients")
    
    'move to the first record in the table
    rec.MoveFirst
    
    'go through each record in the table
    While Not rec.EOF
        'If the client name in the table matches the client name in the form field
        'display a message, move the focus back to the client name field and exit this code
        If rec![ClientName] = Me.ClientName Then
            MsgBox Me.ClientName & " is a duplicate name!"
            Cancel = 1
            'Me.ClientName.SetFocus
            rec.Close
            Set db = Nothing
            Exit Sub
        End If
       
       'move to the next record in the table
        rec.MoveNext
    Wend
    
    'clean up
    rec.Close
    Set db = Nothing
    
    End If
 
End Sub

Open in new window

0
 

Author Comment

by:Gazaway
ID: 22619286
capricorn1,

I had read in another question that "requery" of the field in question would work. So my macro is a simple requery with msgbox on error.

Where would I put the code you suggest?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Gazaway
ID: 22619391
I put capricorn1's suggested code in the "On Exit" event and it works! However, I'd like the focus to return to the client name field so it can be corrected, instead of moving to the next field. I tried adding this command: DoCmd.GoToControl "txtClientName", but it still goes to the next field in the form instead of back to txtClientName.

rene119, I'm going to stick with capricorn1's method for now, since it works and is less complex.
0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 22619402
place it in the before update event of the control clientname


private sub clientname_beforeupdate(cancel as integer)

if dcount("ClientName","tblClients","[clientName]='" & me.clientName &"'")>0 then
msgbox "Duplicate client name found"
cancel=true
end if

end sub
0
 
LVL 120

Accepted Solution

by:
Rey Obrero (Capricorn1) earned 2000 total points
ID: 22619423
private sub clientname_beforeupdate(cancel as integer)

if dcount("ClientName","tblClients","[clientName]='" & me.clientName &"'")>0 then
msgbox "Duplicate client name found"
cancel=true
me.clientname.set focus
end if

end sub


if you want to use the exit event

private sub clientname_exit(cancel as integer)

if dcount("ClientName","tblClients","[clientName]='" & me.clientName &"'")>0 then
msgbox "Duplicate client name found"
cancel=true

end if

end sub
0
 

Author Comment

by:Gazaway
ID: 22619476
Perfect! I used the Exit event and your code and it works great! Thanks!
0

Featured Post

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Did you know that more than 4 billion data records have been recorded as lost or stolen since 2013? It was a staggering number brought to our attention during last week’s ManageEngine webinar, where attendees received a comprehensive look at the ma…
If you need a simple but flexible process for maintaining an audit trail of who created, edited, or deleted data from a table, or multiple tables, and you can do all of your work from within a form, this simple Audit Log will work for you.
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question