Results 1 to 2 of 2
  1. #1
    Join Date
    May 2008
    Posts
    48

    Unhappy Unanswered: Prevent Duplicate Records through VBA

    Can anyone suggest me how to prevent duplicate records through VBA in MS Access?

    Thanks in advance.

  2. #2
    Join Date
    Mar 2009
    Posts
    5,442
    Provided Answers: 14
    For a simple condition of unicity, using a primary key or unique index:
    Code:
    If IsNull(Dlookup("<Field>", "<Table or Query>", "<Condition (ex. Primary_Key = 1234)>")) Then
        ' Record does not exist
    End If
    For multiple conditions of unicity:
    Code:
    Dim rst As DAO.Recordset
    Dim strSQL as String
    
    strSQL = "SELECT COUNT(<Field1>) AS Exist FROM <Table or Query> WHERE <Field1> = <something> AND <Field2> = <something> AND ... AND <Fieldn> = <something>"
    Set rst = Currentdb.OpenRecordset(strSQL, doOpenSnapshot)
    If rst!Exist = 0 Then
        ' Record does not exist
    End If
    rst.Close
    Set rst = Nothing
    Have a nice day!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •