Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using Recordest Clone to find duplicate primary key

Status
Not open for further replies.

Harr777

Programmer
Sep 25, 2003
71
US
Howdy,

I'm trying to write some code that would search through the table (that is the record source for my form) that would look to see if a new PrimaryKey entered into a text bok already exits in the table (before Access does so upon close and present an un-user friendly message).
I know that I need to use the recordset clone because I don't want to change my new record to one that is found. The code that I used so far didn't work.
P.S., can you tell that I'm new to working with record sets.

Please help!!!
Thank you kindly
 
Harr,

Look up the DLookup function in Help. It is really easy
and you don't need a recordset. Just use the BeforeUpdate
event of your field:

Code:
If IsNull(DLookup("[YourKey]", "YourTable", "[YourKey] = '" & Me.YourFormKey & "'") Then
   MsgBox("Record already exists.")
End If

Wayne
 
Thanks Wayne, I'm going to learn about DLookup. I was trying to understand your code. It seems like if the return value is Null, you wouldn't have a match and, therefore, wouldn't display that message box. Maybe I just simply don't understand how it works yet.
Thanks again.
 
Har,

No, that is my mistake. It should be If Not IsNull().
Just a typo.

Wayne
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top