Board index » delphi » Problem with Locate

Problem with Locate

I am having a problem with the Locate command.  Following is code that
does not work and what I am trying to do.

I have a DBEdit11 asigned to the field PER_ID which is an Integer
field.  When a value is entered or changed.  I need to check and see if
that value already exist in another record, same dataset , as the same
ID number is not valid for two records.  I simply need to see if it
already exist.  Not make the results the current record, or at lease
return to the current record.  I have used Locate many times and haven't
had this problem before.  While I have the procedure in the "Before Post
Event" there are any number of other places it can be put.

********************Start Code

procedure TBurialForm.HCemeteryBeforePost(DataSet: TDataSet);
var
  IDLookUp: Boolean;
begin
  // The following line causes a stack overflow.
  IDLookUp:=HCemetery.Locate('PER_ID',
HCemetery.FieldByName('PER_ID').AsInteger,[]);

  if IDLookUp Then
  begin
    PlaySound(PChar('ERROR'), hInstance, snd_ASync or snd_Resource);
    ShowMessage('WARNING!!!   -->   The Personal ID You Entered ia
already in use.'+#13+#10+'Two Persons cannot have the same ID Number.
Enter another ID Number.');
    DBEdit11.SetFocus;
  end;
end;

******************End Code

Using D4 (patch 3), Halcyon, Clipper Function for Delphi, DBF/NTX

Thanks for any help you can give.

Tom Waldie
Design Software

Comprehensive Software for Genealogy Data Management

 

Re:Problem with Locate


If you are locating on the same table as the one calling the BeforePost
event, you can not do this properly without first either posting the record
or canceling the changes. This could create an infinite loop. It is better
to use a second TTable for the same database table and do the locate with
it, not the TTable you are using for the editing.

--

Woody

Quote
Tom Waldie <des...@dhc.net> wrote in message

news:388AEE07.F24D677A@dhc.net...
Quote
> I am having a problem with the Locate command.  Following is code that
> does not work and what I am trying to do.

> I have a DBEdit11 asigned to the field PER_ID which is an Integer
> field.  When a value is entered or changed.  I need to check and see if
> that value already exist in another record, same dataset , as the same
> ID number is not valid for two records.  I simply need to see if it
> already exist.  Not make the results the current record, or at lease
> return to the current record.  I have used Locate many times and haven't
> had this problem before.  While I have the procedure in the "Before Post
> Event" there are any number of other places it can be put.

> ********************Start Code

> procedure TBurialForm.HCemeteryBeforePost(DataSet: TDataSet);
> var
>   IDLookUp: Boolean;
> begin
>   // The following line causes a stack overflow.
>   IDLookUp:=HCemetery.Locate('PER_ID',
> HCemetery.FieldByName('PER_ID').AsInteger,[]);

>   if IDLookUp Then
>   begin
>     PlaySound(PChar('ERROR'), hInstance, snd_ASync or snd_Resource);
>     ShowMessage('WARNING!!!   -->   The Personal ID You Entered ia
> already in use.'+#13+#10+'Two Persons cannot have the same ID Number.
> Enter another ID Number.');
>     DBEdit11.SetFocus;
>   end;
> end;

> ******************End Code

> Using D4 (patch 3), Halcyon, Clipper Function for Delphi, DBF/NTX

> Thanks for any help you can give.

> Tom Waldie
> Design Software

> Comprehensive Software for Genealogy Data Management

Re:Problem with Locate


You have created an infinitely recursive loop which is overflowing the
stack. Your locate is on the same TTable whose BeforePost event handler it
is caused from. Calling locate causes the TTable's cursor to move to a new
record. Moving to a new record forces the current record to be posted which
triggers a Before Post event which calls locate which forces a post which
calls locate ad infinitem. If you need to call locate from the BeforePost
event handler add a second TTable and call its Locate method so you will not
force a post on the first TTable.

--
Bill

Bill Todd (TeamB)
(TeamB cannot respond to questions received via email)

Other Threads