Free Lessons
Courses
Seminars
TechHelp
Fast Tips
Templates
Topic Index
Forum
ABCD
 
Home   Courses   TechHelp   Forums   Help   Contact   Merch   Join   Order   Logon  
 
Back to Yes/No Fields    Comments List
Upload Images   @Reply   Bookmark    Link   Email   Next Unseen 
Indexed YesNo field
Daniel Schuster 
     
3 years ago
Can I set a yes/no field to be indexed so that only 1 'yes' can be applied? For example, a many-to-many relationship between two tables (People and Pets) and the junction table records which Pet is the 'Favorite' for a Person. Johnny has 3 pets but only one of them can be his favorite. I have a check box to mark the 'favorite' but I want to programmatically prevent a second pet getting the 'favorite' checkmark.
Kevin Robertson  @Reply  
          
3 years ago
You would have to loop through all the records and set them all to false and the one you click on is true.

For Each Loop
Daniel Schuster OP  @Reply  
     
3 years ago
Thanks for the reply. I'm breaking new ground here, so I'm trying to keep up.
This is what I've managed so far and all it will do is make it so that once I mark a pet as favorite, I can't mark it as not favorite. I tried doing the S as String thing from the Status Box video thinking that was a prerequisite for this process. I obviously don't understand the basics of trying to accomplish this loop thingy. Let me know if I'm getting close or if I need to do more research.

Private Sub FavoritePet_Click()
  Dim C As Control
        For Each C In Me.Controls
              If C. ControlType = acCheckBox Then
                     FavoritePet.Value = False
              End If
        Next
End Sub
Kevin Yip  @Reply  
     
3 years ago
You basically need to handle two situations:  (1) If an unchecked checkbox is clicked, check it, and uncheck the other checkboxes (pet owner favoriting one pet and unfavoriting the others).  (2) If the user clicks on a checkbox that is already checked, uncheck it (pet owner unfavoriting a previously favorited pet, leaving him with no favorite pet).

The second condition above may not be what you want, so you have to decide what to do if the user unchecks a previously checked checkbox, which will leave you with all boxes unchecked - no favorite.  If you must have at least one Yes, you need to find a way to force the user to check at least one box in that situation.  Or you can allow the user to have no favorite pet.
Kevin Robertson  @Reply  
          
3 years ago
Use an UPDATE statement in the After Update event for the Check Box.

Private Sub IsFavourite_AfterUpdate()

    CurrentDb.Execute "UPDATE PetT SET IsFavourite=False"
    
End Sub


Add an appropriate WHERE condition, if needed. For example:
    CurrentDb.Execute "UPDATE PetT SET IsFavourite=False WHERE CustomerID=" & cboCustomers

That will ensure only one favourite pet is selected - per customer, if applicable

.Access SQL Seminars - Part 2 covers Action Queries.

This thread is now CLOSED. If you wish to comment, start a NEW discussion in Yes/No Fields.
 

Next Unseen

 
New Feature: Comment Live View
 
 

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: 4/30/2026 11:48:33 AM. PLT: 1s