Solved

Check to see if part number already exists in database

Posted on 2014-11-23
4
243 Views
Last Modified: 2014-11-28
I have a form which the user uses to enter a new part in the database.  The following code is supposed to halt the entry if the same part already exists.  Note that PART_NO is a text field.

But the code isn't working, the alert pops up even if the part does not exist already.

    'Code to check for existing part...
    Dim Answer As Variant
    Answer = DLookup("[PART_NO]", "tblParts", "[PART_NO] = '" & Me.txtPART_NO & "'")
    If Not IsNull(Answer) Then
        MsgBox "This part already exists in the database.  Please re-enter the details."
    Me.Undo
    End If

What am I doing wrong?
0
Comment
Question by:SteveL13
[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
4 Comments
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 40460662
try this codes instead


     If dcount("*", "tblParts", "[PART_NO] = '" & Me.txtPART_NO & "'")>0  Then
         MsgBox "This part already exists in the database.  Please re-enter the details."
     Me.Undo
     End If
0
 

Author Comment

by:SteveL13
ID: 40460678
Didn't work.  Sorry.

I am running the code from a command button on a form.  Here is the code with your suggestion in place...

Private Sub cmdCommitPartN_Click()
    'Code to check for existing part...
    If DCount("*", "tblParts", "[PART_NO] = '" & Me.txtPART_NO & "'") > 0 Then
        MsgBox "This part already exists in the database.  Please re-enter the details."
    Me.Undo
    End If
End Sub

Open in new window

0
 
LVL 48

Assisted Solution

by:Dale Fye
Dale Fye earned 250 total points
ID: 40460810
Both your DLOOKUP and Rey's DCOUNT syntax are correct, for what you have described.  You might try:
Dim strCriteria as string

strCriteria = "[PART_NO] = '" & Me.txtPART_NO & "'"
debug.print strCriteria
If dcount("*", "tblParts", strCriteria)>0  Then
    MsgBox "This part already exists in the database.  Please re-enter the details."
    Me.txtPart_No.Undo
End If 

Open in new window

At least this way, you can actually see what is being tested, then you can open tblParts and confirm that the value is actually there.
0
 
LVL 51

Accepted Solution

by:
Gustav Brock earned 250 total points
ID: 40460934
You may have better luck using the BeforeUpdate event of the form and its parameter Cancel:

     ' Code to check for existing part...
     Dim Answer As Variant
     Answer = DLookup("[PART_NO]", "tblParts", "[PART_NO] = '" & Me.txtPART_NO & "'")
     Cancel = Not IsNull(Answer)

     If Cancel = True Then
         MsgBox "This part already exists in the database.  Please re-enter the details."
     End If

/gustav
0

Featured Post

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

Phishing attempts can come in all forms, shapes and sizes. No matter how familiar you think you are with them, always remember to take extra precaution when opening an email with attachments or links.
Code that checks the QuickBooks schema table for non-updateable fields and then disables those controls on a form so users don't try to update them.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
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: …

623 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