Solved

Checking for duplicate data

Posted on 2012-04-03
7
196 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
Comment Utility
>> 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
Comment Utility
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
Comment Utility
The name of the text box is correct.
0
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 

Author Closing Comment

by:tmaususer
Comment Utility
Dcount has appeared to solve the issue
0
 
LVL 61

Expert Comment

by:mbizup
Comment Utility
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
Comment Utility
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
Comment Utility
I hope you've also changed code from:
If Not IsNull(Answer) Then
to
If Answer>0 Then
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Experts-Exchange is a great place to come for help with solutions for your database issues, and many problems are resolved within minutes of being posted.  Others take a little more time and effort and often providing a sample database is very helpf…
I see at least one EE question a week that pertains to using temporary tables in MS Access.  But surprisingly, I was unable to find a single article devoted solely to this topic. I don’t intend to describe all of the uses of temporary tables in t…
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…
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 …

743 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

19 Experts available now in Live!

Get 1:1 Help Now