Solved

Checking for duplicate data

Posted on 2012-04-03
7
201 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
[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
  • 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 40

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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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 40

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

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

Did you know that more than 4 billion data records have been recorded as lost or stolen since 2013? It was a staggering number brought to our attention during last week’s ManageEngine webinar, where attendees received a comprehensive look at the ma…
The Windows Phone Theme Colours is a tight, powerful, and well balanced palette. This tiny Access application makes it a snap to select and pick a value. And it doubles as an intro to implementing WithEvents, one of Access' hidden gems.
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …

617 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