Board index » delphi » Checking for duplicates while entering data.

Checking for duplicates while entering data.

I have a grid (using Cached updates) that the user will use to enter product
numbers and I need a way to check for duplicates while they are entering the
numbers.  I have tried a few ways and just can't seem to figure it out.

Thank you!
Stacey

 

Re:Checking for duplicates while entering data.


 The only way I've found to do this is to use a separate query to do the
checking.  When you're using cached updates, it's best to check for this
BEFORE the user posts the update to the cache, not during the applyupdates!
For example, you might put the following code in the BeforePost  event
handler to do this:

 with qryValid do
 begin
  parambyname('KEYFIELD').AsSTring := edit1.text;
  open;
  try
    If not fieldbyname('KEYFIELD').IsNull then
    begin
      messagedlg('The Value ' + edit1.text + 'Already Exists', mtError,
[mbOK], 0);
      edit1.setfocus;
      sysutils.abort;
    end;
  finally
    close
  end;  // try..finally
end;  // with qryValid

The abort will cancel the post operation.

-Dell Stinnett

Quote
"Stacey" <suptegr...@midwayusa.com> wrote in message news:38eba74f@dnews...
> I have a grid (using Cached updates) that the user will use to enter
product
> numbers and I need a way to check for duplicates while they are entering
the
> numbers.  I have tried a few ways and just can't seem to figure it out.

> Thank you!
> Stacey

Re:Checking for duplicates while entering data.


I didn't explain it very well, I sorry.

I have a grid and when they type in a number and press enter I want to check
for duplicates then.  I don't do an apply updates until they press the save
button which is where they will get the error (I have the number set as a
primary key in the database).  What I want to do is have grid turn red or
something like that when they key in a duplicate so they know there is a
duplicate.  The user doesn't understand why they can't have duplicates
because "the computer let them type it in."

Quote
Dell Stinnett <dell.stinn...@zcsterling.com> wrote in message

news:8ci65d$jse6@bornews.borland.com...
Quote
> The only way I've found to do this is to use a separate query to do the
> checking.  When you're using cached updates, it's best to check for this
> BEFORE the user posts the update to the cache, not during the
applyupdates!
> For example, you might put the following code in the BeforePost  event
> handler to do this:

>  with qryValid do
>  begin
>   parambyname('KEYFIELD').AsSTring := edit1.text;
>   open;
>   try
>     If not fieldbyname('KEYFIELD').IsNull then
>     begin
>       messagedlg('The Value ' + edit1.text + 'Already Exists', mtError,
> [mbOK], 0);
>       edit1.setfocus;
>       sysutils.abort;
>     end;
>   finally
>     close
>   end;  // try..finally
> end;  // with qryValid

> The abort will cancel the post operation.

> -Dell Stinnett

> "Stacey" <suptegr...@midwayusa.com> wrote in message

news:38eba74f@dnews...

- Show quoted text -

Quote
> > I have a grid (using Cached updates) that the user will use to enter
> product
> > numbers and I need a way to check for duplicates while they are entering
> the
> > numbers.  I have tried a few ways and just can't seem to figure it out.

> > Thank you!
> > Stacey

Re:Checking for duplicates while entering data.


What you want to do is to be able to compare the new key to the keys of
records 1. already in the table and 2.  those in  the cache.

I don't know of any way to do 2 (although someone else may) but I can
suggest a workaround.  Create a StringList. Write a BeforePost event
handler in which you
1.  do a select against the physical table to determine whether the key
is already there;
2. check the StringList to see if the key is already there, because you
will have
   done 3 previously.
3. Add the key to the stringlist.
If you find the key, throw an exception.

JP

Re:Checking for duplicates while entering data.


You could clone the dataset after opening the query, and do a select against
the cloned curser which will also include new records in the cache. If
success, the key already exists.

Ole Willy Tuv

Quote
> I have a grid and when they type in a number and press enter I want to
>check  for duplicates then.

Re:Checking for duplicates while entering data.


I had thought about creating a StringList, but thought it would maek the app
to slow.  With you suggestion I went on and did it and it works great.
Thanks a lot.
Quote
John Pierce <jpie...@healthplanning.com> wrote in message

news:38ECD0C2.B2F511A2@healthplanning.com...
Quote
> What you want to do is to be able to compare the new key to the keys of
> records 1. already in the table and 2.  those in  the cache.

> I don't know of any way to do 2 (although someone else may) but I can
> suggest a workaround.  Create a StringList. Write a BeforePost event
> handler in which you
> 1.  do a select against the physical table to determine whether the key
> is already there;
> 2. check the StringList to see if the key is already there, because you
> will have
>    done 3 previously.
> 3. Add the key to the stringlist.
> If you find the key, throw an exception.

> JP

Other Threads