Smart questions
Smart answers
Smart people
Join Tek-Tips Forums
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login




Remember Me
Forgot Password?
Join Us!

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

Join Tek-Tips
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.
Jobs from Indeed

Link To This Forum!

Partner Button
Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

bcooler (Programmer) (OP)
25 Jan 12 22:57
I have a form that is being filled out.  I need to prevent duplicate entries (a duplicate is defined as ALL field values from one record match ALL the field values on any other record).

Now, I've used Dcount quite a bit, but I'm having a little problem here.  I can get no value for Dcount except zero.

CODE

    If DCount("[txtMultiplier_ID]", "tblMultiplier", "[Manufacturer] = """ & Nz(Me.cboManuf) & """ AND [PartType] = """ & Nz(Me.cboPartType) & """ AND [DiscountCode] = """ & _
        Nz(Me.cboDiscount) & """ AND [VolDiscount-Low] = " & Nz(Me.txtVolLow) & " AND [VolDiscount-High] = " & Nz(Me.txtVolHigh) & " AND [Multiplier_ID] <> " & _
            Nz(Me.txtMultiplier_ID)) > 0 Then
        Cancel = True
        If MsgBox("This would create duplicate conditions.  Press 'OK' to return and complete form.  Press 'Cancel' to undo changes.", vbOKCancel, "Please Complete Entry") = vbCancel Then
            Me.Undo
        End If
        Exit Sub

    End If

In some cases, some records under review will have blank fields.  After some troubleshooting, it appears that blank fields (such as [DiscountCode]) are considered Null, which, I think, messes with the Dcount function.  I have used Nz to make sure the form's text boxes are not null, but I don't think I can prevent it on the Field side of things.  

Am I correct about Null, or is there something else going on? I only get 0 for an answer when I know there is at least 1 matching record.

Thanks!
Helpful Member!  PHV (MIS)
26 Jan 12 3:33
What about this ?

CODE

If DCount("txtMultiplier_ID", "tblMultiplier", "Nz(Manufacturer)='" & Nz(Me!cboManuf) & _
    "' AND Nz(PartType)='" & Nz(Me!cboPartType) & _
    "' AND Nz(DiscountCode)='" & Nz(Me!cboDiscount) & _
    "' AND Nz([VolDiscount-Low])=" & Nz(Me!txtVolLow) & _
    " AND Nz([VolDiscount-High])=" & Nz(Me!txtVolHigh) & _
    " AND Nz(Multiplier_ID)<>" & Nz(Me!txtMultiplier_ID)) > 0 Then

Hope This Helps, PH.
FAQ219-2884: How Do I Get Great Answers To my Tek-Tips Questions?
FAQ181-2886: How can I maximize my chances of getting an answer?

bcooler (Programmer) (OP)
27 Jan 12 8:03
Excellent!  I previously attempted to do this, but I must have gotten the syntax wrong.  Works perfectly!

The only thing I did differently was to use two "" in place of ', which prevent apostrophe's in my values from confusing the whole string.  (at least, that's what Allen Browne says).

Thanks so much!
bcooler (Programmer) (OP)
27 Jan 12 17:32
Spoke too early.....

The solution worked on a couple of examples, but later gave 'Syntax' errors.  I'm confident it still had to do with missing info in text/combo boxes, so I got around it by building the filter as a variable (like Allen Browne does)...

CODE

    DupMult = "Nz(Manufacturer)= """ & Nz(Me!cboManuf) & """" 'setting up Dcount Filter based on non-empty fields
    If Nz(Me.cboPartType) <> Empty Then DupMult = DupMult & " AND Nz(PartType)=""" & Nz(Me!cboPartType) & """"
    If Nz(Me.cboDiscount) <> Empty Then DupMult = DupMult & " AND Nz(DiscountCode)=""" & Nz(Me!cboDiscount) & """"
    If Nz([VolDiscount-Low]) <> Empty Then DupMult = DupMult & " AND Nz([VolDiscount-Low])=" & Nz(Me!txtVolLow)
    If Nz([VolDiscount-High]) <> Empty Then DupMult = DupMult & " AND Nz([VolDiscount-High])=" & Nz(Me!txtVolHigh)
    
    DupMult = DupMult & " AND Nz(Multiplier_ID)<> " & Nz(Me!txtMultiplier_ID)

    If DCount("txtMultiplier_ID", "tblMultiplier", DupMult) > 0 Then
        Cancel = True
        If MsgBox("This would create duplicate conditions.  Press 'OK' to return and complete form.  Press 'Cancel' to undo changes.", vbOKCancel, "Please Complete Entry") = vbCancel Then
            Me.Undo
        End If
        Exit Sub
    End If

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Back To Forum

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close