Share via

Validation rule - Duplicates exception - Access 2016 table/form

Anonymous
2017-08-05T00:59:50+00:00

I need to set my table/form to not allow duplicates, except for three commonly strings.

something like this primitive logic:

if string is not "abc" or "def" or "ghi" then

   No duplicates for this field

end 

*******

is this possible? If so, HOW?

Sue

Microsoft 365 and Office | Access | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

2 answers

Sort by: Most helpful
  1. ScottGem 68,830 Reputation points Volunteer Moderator
    2017-08-05T12:46:32+00:00

    Can you explain the logic behind this? I'm wondering if there is not a better way.

    Was this answer helpful?

    1 person found this answer helpful.
    0 comments No comments
  2. Anonymous
    2017-08-05T11:35:27+00:00

    It can be done by applying a CHECK CONSTRAINT to the table.  In Access this cannot be done directly in SQL view, however, but must be executed in code, which can be entered as a single line in the debug (aka 'immediate') window, which can be opened by pressing Ctrl+G.  The statement would be as follows:

    CurrentProject.Connection.Execute "ALTER TABLE YourTableName ADD CONSTRAINT SelectiveDuplication CHECK((SELECT YourColumnName FROM YourTableName WHERE NOT (YourColumnName = 'abc' OR YourColumnName = 'def' OR YourColumnName = 'gh1') GROUP BY YourColumnName HAVING COUNT(*)>1)IS NULL)"

    To remove the CONSTRAINT you'd enter the following:

    CurrentProject.Connection.Execute "ALTER TABLE YourTableName DROP CONSTRAINT SelectiveDuplication"

    The table must be closed when either of the above are entered, and, if the database is split the statement must be executed in the back end, not the front end.

    Was this answer helpful?

    0 comments No comments