Hello I am writing a database which records property rooms.
I want to stop duplicate entries and to this end I want to prevent an entry when a record already exists which has both the same PROPERTY_ID and The Same ROOM_TITLE. I do not want two primary keys so would like to do it in code.
The table is called ROOM_TITLES
It contains the field PROPERTY_ID and ROOM_TITLES
I am still trying to learn code. My thoughts are that on clicking my add record button, I can use a Dlookup function to see if the record exists before adding the record.
I would very much appreciate if someone could show me how I would write this code. I think the following is doing a similar type of thing. Many thanks Mark
SAMPLE CODE I HAVE FOUND SO FAR:
I want to stop duplicate entries and to this end I want to prevent an entry when a record already exists which has both the same PROPERTY_ID and The Same ROOM_TITLE. I do not want two primary keys so would like to do it in code.
The table is called ROOM_TITLES
It contains the field PROPERTY_ID and ROOM_TITLES
I am still trying to learn code. My thoughts are that on clicking my add record button, I can use a Dlookup function to see if the record exists before adding the record.
I would very much appreciate if someone could show me how I would write this code. I think the following is doing a similar type of thing. Many thanks Mark
SAMPLE CODE I HAVE FOUND SO FAR:
Code:
Private Function IsDuplicateRecord() As Boolean
On Error Resume Next
Dim PreviousRecordID As Long
IsDuplicateRecord = False
PreviousRecordID = 0
PreviousRecordID = DLookup("MyID", "CustomerT", "MyID<>" & MyID & _
" AND CustomerID=" & CustomerID & " AND PersonID=" & PersonID)
If PreviousRecordID <> 0 Then
MsgBox "Customer Exists Already"
IsDuplicateRecord = True
End If
End Function
Private Sub Form_BeforeUpdate(Cancel As Integer)
If IsDuplicateRecord Then Cancel = 1
End Sub