Board index » delphi » Preventing duplicates - the right way?

Preventing duplicates - the right way?

Hi all,
    Using Delphi 5, dBaseIV tables and a DBGrid.  In an effort to prevent
duplicate records in a simple table, I am using the following code.  It
appears to work as intended, but I am wondering if this is the "right" way
to do it.  Table1 is the main table that will actually accept the changes
from the grid.  Table2 is really the same table but not connected to any
components and is used solely for lookup purposes.
procedure TForm1.Table1BeforePost(DataSet: TDataSet);
begin
  if Table2.Locate ('TYPE',dbgrid1.SelectedField.AsString,[])=true then
    begin
      ShowMessage('Duplicate record.');
      SysUtils.Abort ;
    end;
end;

Any comments or suggestions would be very welcome.

Thanks,
-Dave

 

Re:Preventing duplicates - the right way?


Quote
>    Using Delphi 5, dBaseIV tables and a DBGrid.  In an effort to prevent
>duplicate records in a simple table, I am using the following code.  It
>appears to work as intended, but I am wondering if this is the "right" way
>to do it.  Table1 is the main table that will actually accept the changes
>from the grid.  Table2 is really the same table but not connected to any
>components and is used solely for lookup purposes.

Yes that is the way to do it.   At least with dbase that doesn't support a true
Unique index.  However you will have a problem if you edit an existing record
because the Locate will find the existing record.  So you need to add some  code
to check if the status is DsInsert and only run the locate code then.

--
Brian Bushay (TeamB)
Bbus...@NMPLS.com

Re:Preventing duplicates - the right way?


Quote
> Yes that is the way to do it.   At least with dbase that doesn't support a
true
> Unique index.  However you will have a problem if you edit an existing
record
> because the Locate will find the existing record.  So you need to add some
code
> to check if the status is DsInsert and only run the locate code then.

Thanks Brian!  It always feels good for a relative novice to know they're on
the right track.  I've been "spoiled" by Access databases that do this work
for me.  Oh well.  The weather here in CT is going to be cold and wet
Sunday,  so I may as well put on a pot (or two) of black coffee and work on
the remainder of the code then.  Thanks again.

-Dave

Re:Preventing duplicates - the right way?


Hi Dave:

I use the same code for a similar reason but I placed it in the
OnValidate event of the field, 'TYPE' in your case, so that the user
cannot get beyond the 'TYPE' field while entering data without
correcting the duplicated problem.

I my case, there are many fields to enter following my equivalent to
your 'TYPE' field and I don't want the user to waste any time, i.e.
waiting for the BeforePost event to fire to find out that they are
posting duplicate information, before resolving the problem.

I am now going to modify my code to take advantage of Brian's advice
as well!

Rob.
--
Robert Thompson
Montreal, QC, Canada

Re:Preventing duplicates - the right way?


Quote
"Robert Thompson" <robert.thomps...@sympatico.ca> wrote in message

news:3A2140CE.31A3A6C9@sympatico.ca...

Quote
> Hi Dave:

> I use the same code for a similar reason but I placed it in the
> OnValidate event of the field, 'TYPE' in your case, so that the user
> cannot get beyond the 'TYPE' field while entering data without
> correcting the duplicated problem.

Robert,
    Great idea.  I'll give it a try.  By the way, I'll be celebrating the
New Year in Montreal.  Great city!  It's like a little bit of Europe in
North America and it's only a 6 hour drive away.  See you on Rue St.
Catherine! <g>
-Dave

Re:Preventing duplicates - the right way?


Quote
> I've been "spoiled" by Access databases that do this work
>for me.  

Most databases support true Unique indexes which is what you need to have the
database handle this.   Dbase is the exception.

--
Brian Bushay (TeamB)
Bbus...@NMPLS.com

Other Threads