Free Lessons
Courses
Seminars
TechHelp
Fast Tips
Templates
Topic Index
Forum
ABCD
 
Home   Courses   TechHelp   Help   Contact   Merch   Join   Order   Logon   Forums   
 
Back to Access Developers    Comments List
Upload Images | @Reply | Bookmark | Link | Email | Next Unseen |
Duplicate Values in Continuous Form
Michael Duncan 
     
17 days ago
Hi,

I am trying to prevent the same color from being added to a sub form. Using a combo box of the colors. I tried some VBA code from Richards "Employee Training 11" video. Here is a picture of the code I used. What happens is when I try to add a different color to the sub form the code still shows to message that it is a duplicate.
Michael Duncan OP  @Reply  
     
17 days ago

Michael Duncan OP  @Reply  
     
17 days ago
Sorry guys I told you that I got the code from the wrong video. The right video is "Prevent Duplicates"
Donald Blackwell  @Reply  
       
17 days ago
First, I would try to msgbox Combo22 to see what value is getting sent and ensure it is not in the table already.

Then I would look at where you are using it - in the "before update" event. In Richard's Prevent Duplicates video, he used in the click event, after access had already processed the change. if you move the code to the after update event, it should work but you can't cancel it per se. But after the msgbox you could add something like:

Combo22 = Combo22.OldValue
Combo22.SetFocus
Combo22.Dropdown
Richard Rost  @Reply  
           
17 days ago
Michael, your screenshot shows DCount checking ColorT for the selected ColorID. If ColorT is your master list of colors, every valid selection already exists there, so it will be flagged even when it hasn't been added to the subform.

The check needs to count records in the table that stores the colors assigned to the current item, matching both that item's ID and the selected ColorID. When editing an existing row, exclude that row from the count. Also, in your BeforeUpdate event, the message alone doesn't stop the selection. Use Cancel = True when a duplicate is found.

What table supplies the subform, and what are its primary key and the field linking it to the main form? That will tell us which table and criteria belong in your DCount.
Michael Duncan OP  @Reply  
     
16 days ago
ColorSubFormSortQ is the query that supplies the sub form. The primary key is ID and the linked field is PartID.
Richard Rost  @Reply  
           
16 days ago
You need to DCount the query's underlying assignment table, not ColorT. The criteria should match the current PartID and selected ColorID, and exclude the current record's ID so editing an existing row doesn't find itself.

Something along these lines:


If DCount("*", "YourAssignmentTable", _
    "PartID=" & PartID & " AND ColorID=" & Combo22 & " AND ID<>" & Nz(ID, 0)) > 0 Then
    MsgBox "That color has already been added for this part."
    Cancel = True
End If


Replace YourAssignmentTable with the actual table behind ColorSubFormSortQ. Also make sure Combo22's bound column is ColorID, not the color description.
Michael Duncan OP  @Reply  
     
16 days ago
Richard

Thank you very much for your help with is code. It works perfectly. This way I can't add two of the same color for one part.

One last thing when I click the OK button on the message box the focus goes back to the color combo box. Where should I put the code to open the combo box again to select another color. I tried above the message box and below the message box line. And I tried afer the cancel = True line. It did not work.
Richard Rost  @Reply  
           
16 days ago
Put it after the MsgBox, before Cancel = True:


MsgBox "That color has already been added for this part."
Combo22.SetFocus
Combo22.Dropdown
Cancel = True


The reason it didn't work in the other spots is that Access is still processing the control's update. You may need to use the form's Timer event instead to delay the dropdown until after the update is canceled. But try that first.
Michael Duncan OP  @Reply  
     
16 days ago
I just tried the above code and the VBA editor says that I can't put the focus on the combo box until the record is saved. Looks like the timer event is the way to go.
Richard Rost  @Reply  
           
15 days ago
Don't use a Timer for this. Keep the duplicate check and Cancel = True in Combo22's BeforeUpdate event.

Then put the focus and dropdown code in Combo22's AfterUpdate event:


Private Sub Combo22_AfterUpdate()
    Combo22.SetFocus
    Combo22.Dropdown
End Sub


That way Access first validates the selection in BeforeUpdate, and then, after the update is finished, it can safely set the focus and open the combo box.
Michael Duncan OP  @Reply  
     
15 days ago
Thanks again for your help Richard that worked. Thank You.
Add a Reply Upload an Image
Next Unseen

 
 
What's This?

 

The following is a paid advertisement
Computer Learning Zone is not responsible for any content shown or offers made by these ads.
 

Learn
 
Access - index
Excel - index
Word - index
Windows - index
PowerPoint - index
Photoshop - index
Visual Basic - index
ASP - index
Seminars
More...
Customers
 
Login
My Account
My Courses
Lost Password
Memberships
Student Databases
Change Email
Info
 
Latest News
New Releases
User Forums
Topic Glossary
Tips & Tricks
Search The Site
Code Vault
Collapse Menus
Help
 
Customer Support
Web Site Tour
FAQs
TechHelp
Consulting Services
About
 
Background
Testimonials
Jobs
Affiliate Program
Richard Rost
Free Lessons
Mailing List
PCResale.NET
Order
 
Video Tutorials
Handbooks
Memberships
Learning Connection
Idiot's Guide to Excel
Volume Discounts
Payment Info
Shipping
Terms of Sale
Contact
 
Contact Info
Support Policy
Mailing Address
Phone Number
Fax Number
Course Survey
Email Richard
[email protected]
Blog RSS Feed    YouTube Channel

LinkedIn
Copyright 2026 by Computer Learning Zone, Amicron, and Richard Rost. All Rights Reserved. Current Time: 11/17/2026 2:12:48 AM. PLT: 1s