I know the question of preventing duplicates has been asked many times before and I've found tons of messages here and elsewhere all giving the same answers, but I've not been able to find anyone else asking this specific variant of the question and I've found no answers.
I had figured out how to use the DLookup or DCount functions to check for duplicates in the BeforeUpdate event of a form and display a custom error message to the user, but I have an issue with it.
It works fine when adding a new record, but it has a pitfall when editing an existing record. For example, my table definition is this:
StatementCode1, StatementCode2, Name, Address
I have 2 existing records like this:
11111, 22222, Pete, 1234 Something St.
33333, 44444, Jack, 9876 Someother St.
I need to prevent duplicates with a combination of the StatementCode1 and StatementCode2 fields.
The user edits Pete's record to have the same combination of SatementCode1 and StatementCode2 as Jack's record. I do still want to trigger the duplicate warning, which works just fine.
The problem comes in if the user edits OTHER fields in an existing record but NOT the field that I want to prevent duplicates of. So say the user edits Pete's record to change his address. The issue is that DLookup and Dcount using the StatementCode1 and StatementCode2 fields as criteria will just find the same record currently being edited and trigger the duplicate warning, which I do NOT want in this case.
I need a way to append a way to say "and the found record is not the record currently being edited" to the search criteria for DLookup or DCount.
I did think of one way. If the table has a unique field for every record that's never modified (like an auto-number field, or a time stamp that's added when the record is first created and never changes), then you can include that in the search critera to compare the value of that field in the found record to the value of that field for the record currently being edited.
But this table doesn't have such a field and I hate to add a whole extra field just for this purpose.
I was wondering if there was a way to maybe check against the record position in the table, but I don't know how to retrieve that either from the form's data source or within the DLookup or DCount criteria. And is the record position even consistent? Can it change based on sort order and filtering?
Thank you.
I had figured out how to use the DLookup or DCount functions to check for duplicates in the BeforeUpdate event of a form and display a custom error message to the user, but I have an issue with it.
It works fine when adding a new record, but it has a pitfall when editing an existing record. For example, my table definition is this:
StatementCode1, StatementCode2, Name, Address
I have 2 existing records like this:
11111, 22222, Pete, 1234 Something St.
33333, 44444, Jack, 9876 Someother St.
I need to prevent duplicates with a combination of the StatementCode1 and StatementCode2 fields.
The user edits Pete's record to have the same combination of SatementCode1 and StatementCode2 as Jack's record. I do still want to trigger the duplicate warning, which works just fine.
The problem comes in if the user edits OTHER fields in an existing record but NOT the field that I want to prevent duplicates of. So say the user edits Pete's record to change his address. The issue is that DLookup and Dcount using the StatementCode1 and StatementCode2 fields as criteria will just find the same record currently being edited and trigger the duplicate warning, which I do NOT want in this case.
I need a way to append a way to say "and the found record is not the record currently being edited" to the search criteria for DLookup or DCount.
I did think of one way. If the table has a unique field for every record that's never modified (like an auto-number field, or a time stamp that's added when the record is first created and never changes), then you can include that in the search critera to compare the value of that field in the found record to the value of that field for the record currently being edited.
But this table doesn't have such a field and I hate to add a whole extra field just for this purpose.
I was wondering if there was a way to maybe check against the record position in the table, but I don't know how to retrieve that either from the form's data source or within the DLookup or DCount criteria. And is the record position even consistent? Can it change based on sort order and filtering?
Thank you.