Solved Preventing duplicated record in datasheet form (1 Viewer)

Babycat

Member
Local time
Tomorrow, 03:59
Joined
Mar 31, 2020
Messages
275
Hi Everyone.

Hope you are doing well!

I have a datasheet form which is actually a subform of other mainform.
This datasheet form shows the district name and ID, so the user will be easy to know if a particular district is already in system or not. They can also correct the district name if any typo.
I want to handle the duplicated input of District name, for example "District 3" is keyed in at ID = 1 while this name is already existed in ID =5, when I move to other record or any elsewhere, it should pop up a msg "Duplicated" like attached photo. After that, the cursor should go back to the record ID = 1 for correction.

I check the duplicated record on even: On Lost Focus of control District_name. It works fine, but I dont know how to move cursor back to record ID =1.

May anyone willing to help me?

duplicated.JPG
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Tomorrow, 04:59
Joined
May 7, 2009
Messages
19,229
Use beforeUpdate event of District field:

Private Sub District_BeforeUpdate(Cancel As Integer)
If Dcount("1", "theTableName", "District = '" & Me!District & "' And ID <> " & Me!ID)
Cancel= True
Msgbox "District already exists."
End If
End Sub
 

Babycat

Member
Local time
Tomorrow, 03:59
Joined
Mar 31, 2020
Messages
275
Wow, it is so easy with expert !

Thank you very much. It is working well now.
 

Users who are viewing this thread

Top Bottom