Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Checking for duplicate data

Posted on 2012-04-03
7
Medium Priority
?
202 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 400 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
Office 365 Training for IT Pros

Learn how to provision tenants, synchronize on-premise Active Directory, implement Single Sign-On, customize Office deployment, and protect your organization with eDiscovery and DLP policies.  Only from Platform Scholar.

 

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

Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

Question has a verified solution.

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

Traditionally, the method to display pictures in Access forms and reports is to first download them from URLs to a folder, record the path in a table and then let the form or report pull the pictures from that folder. But why not let Windows retr…
Outlook for dependable use in a very small business   This article is about using the Outlook application (part of Microsoft Office) in a very small business, or for homeowners where dependability and reliability are critical requirements. This …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

715 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