Checking for Duplicate Records

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • JWS0823
    New Member
    • Jul 2024
    • 2

    #1

    Checking for Duplicate Records

    I am trying to check for duplicate address records in a table before running an append query to add the user-entered address on a form. I am defining a string to capture the street number, name, suffix and city and then using DLookup to search the table (tblLocation). The problem is that the code keeps returning a data type mismatch in criteria expression error. I know it has to do with the street number field because the code works fine when it is omitted. The street number field type is Number with a field size of Double. is there a way to format or convert the string so it will be accepted?
    Code:
    Private Function DupLocation() As Boolean
      Dim strWhere As String
     
      DupLocation = False
      If IsNull(Me!txtStreetNumber) _
      Or IsNull(Me!txtStreetName) _
      Or IsNull(Me!txtSuffix) _
      Or IsNull(Me!txtCity) Then Exit Function
      strWhere = "(([Street]='" & Me!txtStreetName & "') AND " & _
                 "([Suffix]='" & Me!txtSuffix & "') AND " & _
                 "([City]='" & Me!txtCity & "') AND " & _
                 "([Number]='" & Me!txtStreetNumber & "'))"
      If Not IsNull(DLookup("[Street]", _
                            "[tblLocation]", _
                            strWhere)) Then
        DupLocation = True
        Call MsgBox("This location already exists in the database." & vbCrLf & "Please select it from the 'Trespass Location' dropdown", vbExclamation, "Duplicate Location")
       Else
        Call MsgBox("This is a new location", vbInformation, "New Location")
    Last edited by NeoPa; Jul 10 '24, 05:13 PM. Reason: Fixed the [CODE] tags for you.
  • NeoPa
    Recognized Expert Moderator MVP
    • Oct 2006
    • 32669

    #2
    Just caught this on the way out for the evening. Immediately I see a problem. Your number value is presented in the SQL as a string by adding quotes (') around it. Get rid of those & see if that's all there is wrong with it. I'll check back in later when I get back home this evening.

    For further tips on working with SQL in VBA see How to Debug SQL String

    Comment

    • JWS0823
      New Member
      • Jul 2024
      • 2

      #3
      Well, I feel pretty dumb right now. That fixed the issue! Thanks.

      Comment

      • NeoPa
        Recognized Expert Moderator MVP
        • Oct 2006
        • 32669

        #4
        Why feel dumb when you're simply on a journey as we all are?

        If you were repeating the same mistake over & over again then you'd have a right to feel dumb. So far, no cause.

        Best of luck & I hope I don't need to say it but please come back & post if you get into any other difficulties down the road.

        PS. I just reread the last part of my linked article where I sound a little impatient of people who make mistakes. I was less experienced when I wrote that. It's targetted at those who need very strong hints in order to understand why it makes better sense to post the result as well as the code that creates it. I'm developing patience but you understood straight off so no need to feel it pertains to you.
        Last edited by NeoPa; Jul 10 '24, 05:49 PM.

        Comment

        Working...