Check for duplicates before post

Discussion of open issues, suggestions and bugs regarding SDAC (SQL Server Data Access Components) for Delphi, C++Builder, Lazarus (and FPC)
Post Reply
vanmeir
Posts: 13
Joined: Thu 19 May 2011 09:34
Location: The Netherlands
Contact:

Check for duplicates before post

Post by vanmeir » Sat 06 Jul 2013 12:30

Hello,

I would like to know if there is a strategy for checking (preventing) duplicate records (or specific fields within records) before the current record will be posted?
The purpose of this is to be able to notify the user for incorrect values and keep the option for the user to cancel the edit.
Important to say is that i'm using cachedupdates mode!

Regards,

Frans

AndreyZ

Re: Check for duplicates before post

Post by AndreyZ » Mon 08 Jul 2013 09:33

Hello,

There is no built-in mechanism that allows checking of duplicate records in the CachedUpdates mode. You can use the BeforePost event to check it yourself and abort the posting in case of existence of duplicate records. Please note that the only way to cancel posting in the BeforePost event is to call the Abort method. This functionality is inherited from the TDataSet standard class. For more information, please read the BeforePost event description in the Delphi help.

vanmeir
Posts: 13
Joined: Thu 19 May 2011 09:34
Location: The Netherlands
Contact:

Re: Check for duplicates before post

Post by vanmeir » Mon 08 Jul 2013 11:17

Hello Andrey,

the problem in this case is that calls like Locate and FindKey can lead to a jump to another record.
Maybe it's better to rephrase the question: is there a method availlable to search or check for values within a dataset without changing/disturbing (the state of) the current record?

AndreyZ

Re: Check for duplicates before post

Post by AndreyZ » Tue 09 Jul 2013 08:01

To avoid changing of the current record, you should use bookmarks. Here is a code example:

Code: Select all

var
  bm: TBookmark;
begin
  bm := MSQuery.Bookmark; // save the current position
  MSQuery.Locate(KeyFields, KeyValues, Options);
  MSQuery.GotoBookmark(bm); // return to the saved position
end;

Post Reply