Solved

How prevent more than one record as the default

Posted on 2016-10-21
8
29 Views
Last Modified: 2016-10-21
I have a form with a checkbox.  There can only be one record in the record source table marked as the default record.  If the user attempts to make a checkbox true for a record when another record is already marked as true I want the user to get a warning letting them know that there is already a default record.

Here is what I have but it is not working:

Private Sub chkbxDefaultSig_BeforeUpdate(Cancel As Integer)

    Dim strCriteria As String
    
    strCriteria = "Initials = " & Me.txtInitials

    If DCount("DefaultSig", "tblSignatures", strCriteria) > 0 Then
        Me.Undo

        MsgBox "Warning: A default signature has already been selected."
        
        Cancel = True

    End If

End Sub

Open in new window

0
Comment
Question by:SteveL13
  • 3
  • 3
  • 2
8 Comments
 
LVL 19
ID: 41853701
strCriteria needs to contain a condition that the primary key is <> to the record you are on. For example, assuming PK is numeric
strCriteria  = "Initials = " & Me.txtInitials & " AND MyPkFieldname <> " & me.MyPk

Open in new window

Are you using 'Initials' to indicate the active one or is there a yes/no checkbox (or something else) also?
0
 
LVL 49

Expert Comment

by:Gustav Brock
ID: 41853711
Try this:
Private Sub chkbxDefaultSig_BeforeUpdate(Cancel As Integer)

    Dim strCriteria As String
    
    strCriteria = "Initials = " & Me.txtInitials

    Cancel = Not IsNull(DLookup("DefaultSig", "tblSignatures", strCriteria))

    If Cancel = True Then
        MsgBox "Warning: A default signature has already been selected.", vbExclamation + vbOkOnly
        Me.Undo
    End If

End Sub

Open in new window

/gustav
0
 

Author Comment

by:SteveL13
ID: 41853743
Crystal:  the PK is text.  And the Yes/No checkbox is where the beforeupdate code is going.

Gustav:  I believe my original post contained improper field references.  I have changed it now to be:

    Dim strCriteria As String
    
    strCriteria = "DefaultSig = " & Me.chkbxDefaultSig

    Cancel = Not IsNull(DLookup("DefaultSig", "tblSignatures", strCriteria))

    If Cancel = True Then
        MsgBox "Warning: A default signature has already been selected.", vbExclamation + vbOkOnly
        Me.Undo
    End If

Open in new window


Which works fine if I try to make a record the default via the checkbox and there already is a default record.  But if I try to UNcheck the default record I get the message and it won't let me UNcheck it.
0
The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

 
LVL 49

Accepted Solution

by:
Gustav Brock earned 500 total points
ID: 41853751
Yes, that's right. Try this with a check of the value:
Private Sub chkbxDefaultSig_BeforeUpdate(Cancel As Integer)

    Dim strCriteria As String
    
    If Me!chkbxDefaultSig.Value = True Then
        strCriteria = "Initials = " & Me.txtInitials
        Cancel = Not IsNull(DLookup("DefaultSig", "tblSignatures", strCriteria))
    End If

    If Cancel = True Then
        MsgBox "Warning: A default signature has already been selected.", vbExclamation + vbOkOnly
        Me.Undo
    End If

End Sub

Open in new window

/gustav
0
 
LVL 19
ID: 41853757
to clarify:
you have a table called tblSignatures
with:
 a Yes/No field called DefaultSig
 a field called Initials to indicate who the signature is for (if more than one person is in here)

What is the PK fieldname?
0
 

Author Closing Comment

by:SteveL13
ID: 41853763
Worked! with one minor correction:

    Dim strCriteria As String
   
    If Me!chkbxDefaultSig.Value = True Then
        strCriteria = "DefaultSig = " & Me.chkbxDefaultSig
        Cancel = Not IsNull(DLookup("DefaultSig", "tblSignatures", strCriteria))
    End If

    If Cancel = True Then
        MsgBox "Warning: A default signature has already been selected.", vbExclamation + vbOKOnly
        Me.Undo
    End If
0
 
LVL 49

Expert Comment

by:Gustav Brock
ID: 41853767
Great!
Have a nice weekend.

/gustav
0
 

Author Comment

by:SteveL13
ID: 41853770
You too!  Thanks again.
0

Featured Post

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

QuickBooks® has a great invoice interface that we were happy with for a while but that changed in 2001 through no fault of Intuit®. Our industry's unit names are dictated by RUS: the Rural Utilities Services division of USDA. Contracts contain un…
I see at least one EE question a week that pertains to using temporary tables in MS Access.  But surprisingly, I was unable to find a single article devoted solely to this topic. I don’t intend to describe all of the uses of temporary tables in t…
Familiarize people with the process of utilizing SQL Server stored procedures from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Micr…
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question