Check for duplicate before new record saved

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I would like to check for a duplicate entry and cancel the operation if the
user starts a new record with a duplicate ID. But I can't figure out how to
get the code to run. The field is the very first one for the record, so the
record doesn't exist until after the field has been exited. This seems to
mean that none of the usual events like AfterUpdate, Change, Exit, etc. get
triggered. The field is bound to a table. Should I make it unbound?
 
Assuming the user enters the ID in a control, use the control's Before Update
event.

If Not IsNull(DLook("[ID]", "MyTable", "[ID] = " & Me.txtID) Then
MsgBox "This ID is already in the table"
Cancel = True
End If

If the ID is found in the table, the DLookup will return its value. The
user will get a warning, the Update will be canceled, and the focus will stay
in the control.
 
The BeforeUpdate event will only fire if the value entered in the field by
the user. If it is done programmatically, it will not fire.
How are you assigning the new ID value?

LongWayFromHome said:
My problem is not how to check for the duplicate but how to trigger the code.
The BeforeUpdate event doesn't occur. I am not sure why that is the case.
--
Dave

Klatuu said:
Assuming the user enters the ID in a control, use the control's Before Update
event.

If Not IsNull(DLook("[ID]", "MyTable", "[ID] = " & Me.txtID) Then
MsgBox "This ID is already in the table"
Cancel = True
End If

If the ID is found in the table, the DLookup will return its value. The
user will get a warning, the Update will be canceled, and the focus will stay
in the control.

LongWayFromHome said:
I would like to check for a duplicate entry and cancel the operation if the
user starts a new record with a duplicate ID. But I can't figure out how to
get the code to run. The field is the very first one for the record, so the
record doesn't exist until after the field has been exited. This seems to
mean that none of the usual events like AfterUpdate, Change, Exit, etc. get
triggered. The field is bound to a table. Should I make it unbound?
 
My problem is not how to check for the duplicate but how to trigger the code.
The BeforeUpdate event doesn't occur. I am not sure why that is the case.
--
Dave

Klatuu said:
Assuming the user enters the ID in a control, use the control's Before Update
event.

If Not IsNull(DLook("[ID]", "MyTable", "[ID] = " & Me.txtID) Then
MsgBox "This ID is already in the table"
Cancel = True
End If

If the ID is found in the table, the DLookup will return its value. The
user will get a warning, the Update will be canceled, and the focus will stay
in the control.

LongWayFromHome said:
I would like to check for a duplicate entry and cancel the operation if the
user starts a new record with a duplicate ID. But I can't figure out how to
get the code to run. The field is the very first one for the record, so the
record doesn't exist until after the field has been exited. This seems to
mean that none of the usual events like AfterUpdate, Change, Exit, etc. get
triggered. The field is bound to a table. Should I make it unbound?
 
Never mind...I failed to set the AfterUpdate link in the Properties box!
--
Dave


LongWayFromHome said:
My problem is not how to check for the duplicate but how to trigger the code.
The BeforeUpdate event doesn't occur. I am not sure why that is the case.
--
Dave

Klatuu said:
Assuming the user enters the ID in a control, use the control's Before Update
event.

If Not IsNull(DLook("[ID]", "MyTable", "[ID] = " & Me.txtID) Then
MsgBox "This ID is already in the table"
Cancel = True
End If

If the ID is found in the table, the DLookup will return its value. The
user will get a warning, the Update will be canceled, and the focus will stay
in the control.

LongWayFromHome said:
I would like to check for a duplicate entry and cancel the operation if the
user starts a new record with a duplicate ID. But I can't figure out how to
get the code to run. The field is the very first one for the record, so the
record doesn't exist until after the field has been exited. This seems to
mean that none of the usual events like AfterUpdate, Change, Exit, etc. get
triggered. The field is bound to a table. Should I make it unbound?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top