If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > PC based Database Applications > Microsoft Access > Duplicate Value error

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-14-12, 02:20
anujkmehrotra anujkmehrotra is offline
Registered User
 
Join Date: Jan 2012
Posts: 19
Question Duplicate Value error

Hi,

I am facing a problem while preventing duplicate value in field "CID". As per below code, it response good when any duplicate value enters in CID field, but it also responses the same msg while entering new CID value.

Please help me to find the problem. thanks in advance.
I'm using Access2003.
================================================== ========
Private Sub CID_BeforeUpdate(Cancel As Integer)

Dim SID As String
Dim stLinkCriteria As String
SID = Nz(Me.Cid.Value, "")

stLinkCriteria = "Cid = " & "'" & SID & "'"

If DCount("Cid", "tblCustomerSale", stLinkCriteria) > 0 Then
MsgBox "Duplicate Customer ID.", , "Error"
Cancel = True
End If
End Sub
================================================== =======
Reply With Quote
  #2 (permalink)  
Old 01-14-12, 10:44
Missinglinq Missinglinq is offline
Registered User
 
Join Date: Jun 2005
Location: Richmond, Virginia USA
Posts: 1,702
Are you absolutely sure that you're entering a new CID number? Have you checked the Table to varify that the CID you're going to enter is, in fact, new?

I ask because I created a db with your Field Name, copied and pasted your code in the Code Module of the Form, and, for me, also using 2003, it works as it should, only popping the Messagebox when I attempt to enter an existing CID.

Linq ;0)>
__________________
Hope this helps!

The Devil's in the Details!!

All posts/responses based on Access 2000/2003
Reply With Quote
  #3 (permalink)  
Old 01-14-12, 11:07
anujkmehrotra anujkmehrotra is offline
Registered User
 
Join Date: Jan 2012
Posts: 19
Exclamation Same Error

Thanks for fast reply dear, but I' facing the same problem. I'm getting msgbox in both conditions, whether I enter new CID value or existing Value.

Many Thanks,
Anuj
Reply With Quote
  #4 (permalink)  
Old 01-14-12, 18:24
Sinndho Sinndho is offline
Registered User
 
Join Date: Mar 2009
Posts: 3,446
Ditto. I tried too and, as expected, I got the same result as Missinglinq.
__________________
Have a nice day!
Reply With Quote
  #5 (permalink)  
Old 01-14-12, 19:38
myle myle is offline
(Making Your Life Easy)
 
Join Date: Feb 2004
Location: New Zealand
Posts: 1,143
What about changing the dcount >1

As you are checking if more than one
__________________
hope this help

See clear as mud


StePhan McKillen
the aim is store once, not store multiple times
Remember... Optimize 'til you die!
Progaming environment:
Access based on my own environment: DAO3.6/A97/A2000/A2003
VB based on my own environment: vb6 sp5
ASP based on my own environment: 5.6
VB-NET based on my own environment started 2007
SQL-2005 based on my own environment started 2008
MYLE
Reply With Quote
  #6 (permalink)  
Old 01-15-12, 03:01
anujkmehrotra anujkmehrotra is offline
Registered User
 
Join Date: Jan 2012
Posts: 19
Thumbs up Duplicate Error problem solved..

Finally, I got the solution. It is working in both ways. It is a below:
=================
Private Sub CID_BeforeUpdate(Cancel As Integer)
Dim stLinkCriteria As String

If Me.NewRecord Then
stLinkCriteria = "CID = """ & Me.CID & """"
Else
stLinkCriteria = "CID = """ & Me.CID & """"
End If

If DCount("*", "tblCustomerSale", stLinkCriteria) > 0 Then
MsgBox "Duplicate Customer ID.", vbExclamation, "Error"
Cancel = True
End If
End Sub
===================

Thanks a lot for your fast replies.
Reply With Quote
  #7 (permalink)  
Old 01-15-12, 05:02
Sinndho Sinndho is offline
Registered User
 
Join Date: Mar 2009
Posts: 3,446
This makes little sense. The way the criteria is assembled is the same whether Me.NewRecord is True or False:
Code:
If Me.NewRecord Then
    stLinkCriteria = "CID = """ & Me.CID & """"
Else
    stLinkCriteria = "CID = """ & Me.CID & """"
End If
is the same as:
Code:
stLinkCriteria = "CID = """ & Me.CID & """"
and the test is useless.
__________________
Have a nice day!
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On