Solved

Check to see if part number already exists in database

Posted on 2014-11-23
4
234 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 119

Expert Comment

by:Rey Obrero
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

762 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

6 Experts available now in Live!

Get 1:1 Help Now