Solved

How prevent more than one record as the default

Posted on 2016-10-21
8
30 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
8 Comments
 
LVL 20
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 50

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
Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

 
LVL 50

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 20
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 50

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
Familiarize people with the process of utilizing SQL Server functions 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 Microsoft Ac…
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

739 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