Solved

Checking for duplicate data

Posted on 2012-04-03
7
197 Views
Last Modified: 2012-04-03
I have a very simple form called form2 based on a table called final_tbl. The field on my form is called ser_serialnumber (based on final_tbl). Basically the operator will scan a barcode tag into the field on the form. I have the following code that checks to see if the serial number is a duplicate.

Private Sub SER_SerialNumber_BeforeUpdate(Cancel As Integer)
 Dim Answer As Variant
 Answer = DLookup("[ser_serialnumber]", "final_tbl", "[ser_serialnumber] = '" & Me.SER_SerialNumber & "'")
 If Not IsNull(Answer) Then
 MsgBox "Duplicate Serial Number" & vbCrLf & "Please enter again.", vbCritical + vbOKOnly + vbDefaultButton1, "Duplicate"
  Cancel = True
 Me.SER_SerialNumber.Undo
  Else
 End If
End Sub

The issue that I am having is that I have the form set to open to a new record and if I scan in a serial number that I know is a duplicate it will take it. If I scan it a second time the code will work. This seems to happen for any known duplicate the first time any record is scanned. After the first scan is taken care of the code seems to work. I am not a programmer so I don't know what I could be missing.
0
Comment
Question by:tmaususer
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 61

Expert Comment

by:mbizup
ID: 37802305
>> Me.SER_SerialNumber


There is an important distinction between the name of the control, and the name of the field underneath it that may be coming into play here.

try renaming the textbox or whatever holds your serial number to txtSerialNumber (so that it is distinct from the name of the underlying field)

In this code, you wanto make sure to reference the name of the control, not the underlying field.

 Answer = DLookup("[ser_serialnumber]", "final_tbl", "[ser_serialnumber] = '" & Me.ThisNeedsToBeTheNameOfTheTextbox & "'")
0
 
LVL 39

Accepted Solution

by:
als315 earned 100 total points
ID: 37802340
You should test your fields. May be there are additional spaces. Open VBEditor, set breakpoints and analyze all parameters, transferred to dlookup.
Sometimes better to use dcount:
Answer = DCount("*", "final_tbl", "[ser_serialnumber] = '" & Me.SER_SerialNumber & "'")
I always prefer to use indexes in tables (in your case it could be complex index) for preventing duplicates.
0
 

Author Comment

by:tmaususer
ID: 37802428
The name of the text box is correct.
0
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 

Author Closing Comment

by:tmaususer
ID: 37802454
Dcount has appeared to solve the issue
0
 
LVL 61

Expert Comment

by:mbizup
ID: 37802460
Understood.  My point is that the field and a text box should not be given the same name.

 This is likely to be causing the problem you are describing.

.

 Try renaming your text box as I described earlier.

TxtSerialNumber
0
 
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 37802518
This is why I hate trying to do validation "On-the-fly"
I typically try to do this on the Before update event of the *form*

Validating data "while editing" always seems like a cool thing to do, but it always has problems like what you have here.
(there are countless questions here on this subject)
Even if you find a solution here, a user will always stumble on a scenario where it will have another issue (cancelling a record, filtering, ...etc)

Remember, ...when you order an item from a website, you enter *ALL* of the fields then click "Next/Continue".
Only after this step, are any errors flagged.

If you enter a duplicate cust ID, or invalid Address, ...etc  you are not stopped as soon as you enter it, only after the entire record is about to be processed.

Again, validating on the form event is not as "Cool" as validating on the combobox, but the form event offers much more control and consistency, IMHO...


JeffCoachman
0
 
LVL 39

Expert Comment

by:als315
ID: 37802567
I hope you've also changed code from:
If Not IsNull(Answer) Then
to
If Answer>0 Then
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Overview: This article:       (a) explains one principle method to cross-reference invoice items in Quickbooks®       (b) explores the reasons one might need to cross-reference invoice items       (c) provides a sample process for creating a M…
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

912 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now