Access 2007-2019
Access 2000-2003
Access Seminars
TechHelp Support
Tips & Tricks
Access Forum
Course Index
Topic Glossary
Insider Circle
 
Home   News   Tips   Glossary   Templates   Forums   Help   Logon   Order   Contact Us  
 
Tips & Tricks

New Tips Added Weekly!
Click here to get on our Mailing List

   
 

Microsoft Access Duplicate Values on Multiple Fields

Preventing Duplicate Values Across Multiple Fields with Two Indices

Q: How can I prevent the user from entering a record with duplicate values across MULTIPLE fields. For example, if I have CustomerID and PersonID in a table, the user could duplicate CustomerID or PersonID many times, but the combination of the TWO fields has to always be unique.

A: If you have a multi-field index and you set the fields individually to NO DUPLICATES then you won't be able to have a duplicate value in EITHER field.

In order to make it so that the COMBINATION of indexes is unique, then you can do that by setting both fields to be the PRIMARY KEY (with the little key button) but changing their index type to YES, DUPLICATE VALUES OK. Access will make sure that the combination of both records is unique or it will give you an error message.

If you want to be able to control what happens when the user violates the rules, then you need to do that with some VBA code. I would put it in the BeforeUpdate event of a form (before the record is actually saved) so that you can display a warning message and cancel the event.

Here is a sample database file that shows you the exact code: SAMPLE FILE - ZIP file, 13k, Access XP (2002) format

 


By Richard Rost   Richard Rost on Facebook Richard Rost on Twitter Richard Rost on Google Plus Richard Rost on LinkedIn Email Richard Rost

Click here to sign up for more FREE tips

 

You may want to read these articles from the 599CD News:

 

Learn
 
Accessindex
Excelindex
Wordindex
Windowsindex
PowerPointindex
Photoshopindex
Visual Basicindex
ASPindex
Seminars
More...
Customers
 
Account Login
Online Theater
Downloads
Lost Password
Free Upgrades
Insider Circle
Student Databases
Change Email
Info
 
Latest News
New Releases
User Forums
Topic Glossary
Tips & Tricks
Articles
Search Our Site
Waiting List
Production Schedule
Collapse Menus
Help
 
Live Chat
Customer Support
WalkThru Tutorials
Troubleshooting
FAQs
TechHelp
Consulting Services
About Us
 
Background
Testimonials
Jobs
Affiliate Program
Richard Rost
Free Lessons
Mailing List
Order
 
Video Tutorials
Handbooks
MYOLP Memberships
Idiot's Guide to Excel
Volume Discounts
Payment Info
Shipping
Terms of Sale
Gift CDs
Contact
 
Live Chat
General Info
Support Policy
Contact Form
Email Us
Mailing Address
Phone Number
Fax Number
Course Survey
Facebook    Twitter

Google Plus    LinkedIn

Blog RSS Feed    YouTube Channel
Richard Rost Microsoft MVP