Link to home
Start Free TrialLog in
Avatar of AbacusInfoTech
AbacusInfoTech

asked on

Check for duplicate records before saving with DLOOKUP or something else.

Hi Experts.

I have a form dedicated to entering new data into a table. On this form I have a 'CHECK AND SAVE' button that, once pressed, will check whether the value entered in my 'companyjobid' field is unique in my table 'tbljob'.

If the value IS unique then the record may be saved.

If the value IS NOT unique, then the record will not be saved and a message box will appear.

I just can't figure out the syntax of DLOOKUP, which is what I think I need to use.

I have seen this solution which is very similar to what I want, but I can't figure out the syntax.

https://www.experts-exchange.com/questions/20574943/Check-for-duplicates-before-save-record.html

I already have some code on this button that check all of my required fields for null values. This works a treat, but I need to get this extra code to stop duplicated entries in my 'companyjobid' field.

FYI this field is not the key.

Avatar of AbacusInfoTech
AbacusInfoTech

ASKER

Pending any answers i've got a bit of code i'vew tried that doesn't work....

If Not IsNull(Me!companyjobid) Then
      If Not IsNull (DLOOKUP ("[companyjobid]","tbljob","[companyjobid]=" & Me!companyjobid)) Then
      MsgBox "A duplicate exists.", vbInformation, "Note"
   Else
      MsgBox "No duplicates", vbInformation, "Note"
   End If
End If
Avatar of rockiroads
u need to use the Form_BeforeUpdate function

basically do this

IF NZ(DLOOKUP("field to check","tablename",""),"") <> "" then
    msgbox "Already exists"
    cancel = true  'abort save
END IF

ASKER CERTIFIED SOLUTION
Avatar of rockiroads
rockiroads
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Okay, this is better....

Private Sub test_Click()
If Not IsNull(Me!companyjobid) Then
    If Not IsNull(DLookup("[companyjobid]", "tbljob", "[companyjobid]" = Me!companyjobid)) Then
      MsgBox "A duplicate exists.", vbInformation, "Note"
   Else
      MsgBox "No duplicates", vbInformation, "Note"
   End If
End If
End Sub

...I got rid of the ampersand and at least I get a 'no duplicates' box now but it's not correctly looking at my table to find what I know to be a duplicate.

Thanks, i'll give these a whirl.
in your last post, the code

If Not IsNull(DLookup("[companyjobid]", "tbljob", "[companyjobid]" = Me!companyjobid))

u got the = sign outside the quotes, its got to be inside
i.e.
If Not IsNull(DLookup("[companyjobid]", "tbljob", "[companyjobid] =" & Me!companyjobid))

doing it outside quotes wont work anyway
Thanks rockiroads....!

This is my code that works fine in the beforeupdate of my field:

Dim sID As String

sID = Nz(companyjobid.Value, "")

If sID <> "" Then
    If Nz(DLookup("companyjobid", "tbljob", "companyjobid = '" & sID & "'"), "") <> "" Then
       MsgBox "Dups"
    Else
        MsgBox "No Dup"
    End If
End If
ok, cool
that means its a string

remember the difference in using strings/numbers

use of quotes!