Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Checking for duplicate data

Posted on 2012-04-03
7
Medium Priority
?
203 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 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

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

Microsoft's Excel has many features that most people will never need nor take advantage of.  Conditional formatting is one feature that you may find a necessity once you start using it.
Lost Word File? Eagerly, need it back? Read ahead; this File Recovery guide is for you.
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…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

916 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