Code to validate no duplication during data entry

I am trying to determine on entry of anew record, that the data being entered doesn't already exist in the data.  I have tried several approaches without success.  My current process is just using a recordcount to determine if the record being entered doesn't exists.  getting type mismatch on strsql string.  Also interested if Validation rules can be used if your criteria is multiple sources?

Private Sub cmdAdd_Click()
Dim strSQL As String
Dim curDB As Database
Dim rs As Recordset
Dim nCountryName As String
Dim nStateName As String
Dim gCityName As String
Dim n As Long
gCountryID = Nz([Forms]![frmAddLocation]![CountryRecID])
gStateID = Nz([Forms]![frmAddLocation]![StateRecID])
gCityName = Nz([Forms]![frmAddLocation]![CityName])
'nCountryName = DLookup("Countryname", "tblCountry", "CountryRecID =" & gCountryID)
'nStateName = DLookup("StateName", "tblState", "stateRecID =" & gStateID)

'    Set curDB = CurrentDb()
'   Dim strWhere As String
'strWhere = "((tblCity.CityName) Like   & QUOTE & " * " &  nCityName &  " * " & QUOTE, " & _
              " AND [CountryRecID] = " & gCountryID & _
              " AND [StateRecID] = " & gStateID
'n = DCount("*", "tblCity", strWhere)
   ' n = DCount("*", "tblCity", "((tblCity.CityName) Like " & QUOTE & " * " & nCityName & " * " & QUOTE, "" & _
              " AND [CountryRecID] = '" & gCountryID & "' " & _
              " AND [StateRecID] = '" & gStateID & "' ")
              
'Debug.Print n


   strSQL = "SELECT tblCountry.CountryName, tblCity.CityName, tblState.StateName" & _
                " FROM (tblCity INNER JOIN tblCountry ON tblCity.CountryRecID = tblCountry.CountryRecID) INNER JOIN tblState ON tblCity.StateRecID = tblState.StateRecID" & _
                " WHERE tblCity.CountryRecID =" & gCountryID & "" & _
                        " And tblCity.stateRecID =" & gStateID & "" & _
                " GROUP BY tblCountry.CountryName, tblCity.CityName, tblState.StateName" & _
                " HAVING (((tblCity.CityName) Like " * " & [Forms]![frmAddLocation]![CityName] & " * "))"
'strSQL = "Select * from qryAddCityValidation"
Debug.Print strSQL
        Set rs = curDB.OpenRecordset(strSQL)
        If rs.RecordCount = 0 Then
'        If n = 0 Then
            DoCmd.RunSQL _
                ("INSERT INTO tblCity ( CountryRecID, StateRecID, CityName, TaxInclusive, Top5000, DateCreated, Modifiedby ) " _
                & "VALUES ( '" & gCountryID & "', '" & gStateID & "', '" & _
                gCityName & "', " & gTaxIncl & ", " & gTop & ", Date(), '" & _
                GetgUserID() & "' )") & _
               " where Not exists(strSQL)"
           ' DoCmd.RunSQL ("Delete * from tblCityAddTemp")
'        Else
'            Select Case MsgBox("Records Found", vbYesNo Or vbExclamation Or vbDefaultButton1, "found")
'
'                Case vbYes
'
'                Case vbNo
'
'            End Select
'
        End If

   On Error GoTo 0
   Exit Sub

End Sub

Open in new window

Looking for the most efficient and simple way to determine posssible duplicates on entry of new record.

k
Karen SchaeferBI ANALYSTAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

als315Commented:
Best approach - add complex index to your table and make it unique (look at Index1 in sample).
In this case you will get error for repeating values and can make some error processing routine if you don't like standard error message
DB28684279.accdb

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Gustav BrockCIOCommented:
Yes, that's the best method.

If you are looking for a simple method to use in the BeforeInsert and BeforeUpdate events of the form, you can use DLookup:

Criteria = ' List of fields and values to check.
Cancel = Not IsNull(DLookup("[ID]", "YourQuery", Criteria))
If Cancel = True Then
    MsgBox "Cannot duplicate existing data."
End If

/gustav
Jeffrey CoachmanMIS LiasonCommented:
<No Points wanted>

Open als315's sample table in Design view.
Then click the "Indexes" button to view "Index1"

See the steps here as well:
http://www.geeksengine.com/article/composite-unique-index-access.html

JeffCoachman
Karen SchaeferBI ANALYSTAuthor Commented:
Thanks that seems to have worked -

K
Karen SchaeferBI ANALYSTAuthor Commented:
thanks
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.