Solved

Check to see if part number already exists in database

Posted on 2014-11-23
4
241 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
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 47

Assisted Solution

by:Dale Fye (Access MVP)
Dale Fye (Access MVP) 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 49

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

828 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