Results 1 to 4 of 4
  1. #1
    Gina Maylone is offline Always learning
    Windows 7 64bit Access 2013
    Join Date
    Jun 2013
    Location
    Afton, MN
    Posts
    544

    Find duplicate record via data entry form ALWAYS fires


    Good morning all! Sorry to be so needy lately!

    I have a function that looks for an existing address in my database after "City" is entered. It fires on every record even when there are NO records in the table. Do I need an Else statement?
    Code:
    Private Sub City_AfterUpdate()
    On Error GoTo errHandler
    Dim fulladdress As String
    Dim strCriteria As String
    Dim rs As DAO.Recordset
    
    Me.fulladdress = Me.[Address] & " " & Me.[Street Name] & " " & Me.[City] & " " & Nz(Me.[State]) & " " & Nz(Me.[Zip])
    DoCmd.RunCommand acCmdSaveRecord
    
        Set rs = Me.RecordsetClone
    
        fulladdress = Me.fulladdress.Value
        
          strCriteria = "[fulladdress]=" & "'" & fulladdress & "'"
        Debug.Print strCriteria
        If DCount("fulladdress", "contacts", _
                  strCriteria) > 0 Then
     
                
        Debug.Print strCriteria
         
        End If
    'Message box warning of duplication
    Select Case MsgBox("This address already exists." & vbNewLine & vbNewLine & _
    "Click 'Yes' to view the existing contact record," & vbNewLine & vbNewLine & _
    "Click 'No' to add a duplicate contact record," & vbNewLine & vbNewLine & _
    "Click 'Cancel' to discard all changes.", vbYesNoCancel + vbExclamation)
    Case vbYes
    'Go to record of original record'
    DoCmd.OpenForm "contacts", acNormal, , strCriteria
    
    Case vbNo
    'Allow save
    'cancel = False
    Forms!CONTACTS.State.SetFocus
    
    Case vbCancel
    'Stop the save and undo the form
    Me.Undo
    
    End Select
    
    
        Set rs = Nothing
    
    Exit Sub
    errHandler:
      MsgBox "Error " & Err.Number & ": " & Err.Description & " in " & _
       VBE.ActiveCodePane.CodeModule, vbOKOnly, "Error"
    
    
    End Sub
    Thanks in advance!

  2. #2
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    I would probably do something like

    Set rs = Me.RecordsetClone
    If rs.recordcount < 1 then
    set rs = nothing
    exit sub
    end if

  3. #3
    John_G is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2011
    Location
    Ottawa, ON (area)
    Posts
    2,615
    Follow the logic in your code - there is no way to avoid executing the Select Case.... statement.

    It's an easy fix -

    First, change

    if DCount("fulladdress", "contacts", strCriteria) > 0 Then

    to

    if DCount("fulladdress", "contacts", strCriteria) = 0 Then


    and put Exit Sub right after the Debug.Print strCriteria statement.

    What that does is exit the procedure if the address is NOT found, and you will not get the message.

  4. #4
    Gina Maylone is offline Always learning
    Windows 7 64bit Access 2013
    Join Date
    Jun 2013
    Location
    Afton, MN
    Posts
    544
    This did it! Thank you John_G!!

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 5
    Last Post: 03-09-2014, 07:16 PM
  2. Duplicate data message for a data entry form
    By JulieMarie in forum Access
    Replies: 5
    Last Post: 07-30-2013, 08:18 AM
  3. Replies: 3
    Last Post: 03-09-2013, 10:39 AM
  4. Customer Entry/Find Duplicate/Similar names
    By pkstormy in forum Code Repository
    Replies: 0
    Last Post: 08-27-2010, 09:20 PM
  5. Replies: 3
    Last Post: 06-04-2010, 12:47 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums