Solved

How prevent more than one record as the default

Posted on 2016-10-21
8
28 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
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 
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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
hit enter key to run macro 13 23
How to run a VBS file in a MS access macro? 2 28
Auto-generated ID's in MS Access 9 45
Ms Access VBA Variables 6 26
When you are entering numbers in a speadsheet, and don't remember what 6×7 is, you just type “=6*7" instead. It works in every cell! This is not so in Access. To enter the elusive 42 in a text box, you have to find a calculator, and then copy the re…
As tax season makes its return, so does the increase in cyber crime and tax refund phishing that comes with it
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…

786 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