Board index » delphi » Locating a record

Locating a record

My DBGrid is correctly showing the result of my Query. I know that I can
position my mouse on a specific row on the DBGrid and correspondingly access
that record in the Query set with statements like MyQuery.FieldByName( )...
What if I need (programmatically) to get to a specific record of that Query
set ? Is there something corresponding to Setkey/Gototkey of a TTable ?
 

Re:Locating a record


"Franco Jommi" <franco.jom...@tin.it> schrieb im Newsbeitrag
news:P6Fj9.130951$ub2.2843643@news1.tin.it...

Quote
> My DBGrid is correctly showing the result of my Query. I know that I can
> position my mouse on a specific row on the DBGrid and correspondingly
access
> that record in the Query set with statements like

MyQuery.FieldByName( )...

Quote
> What if I need (programmatically) to get to a specific record of that
Query
> set ? Is there something corresponding to Setkey/Gototkey of a TTable ?

Yes. You should use "Locate" instead.

Example:
if Query1.Locate('CustomerId', EditCustomer.Text, []) then
begin
   // found
end
else
begin
  // not found
end;

Andreas Schmidt

PS: do not use Locate on Queries with a large record count.

Re:Locating a record


Thanks for the lightening answer. But what if the record count is large and
I need to access that specific record ? Is there some other way ?

"Andreas Schmidt" <a_j_schm...@rocketmail.com> ha scritto nel messaggio
news:amn8ur$79jm1$1@ID-13084.news.dfncis.de...

Quote

> "Franco Jommi" <franco.jom...@tin.it> schrieb im Newsbeitrag
> news:P6Fj9.130951$ub2.2843643@news1.tin.it...
> > My DBGrid is correctly showing the result of my Query. I know that I can
> > position my mouse on a specific row on the DBGrid and correspondingly
> access
> > that record in the Query set with statements like
> MyQuery.FieldByName( )...
> > What if I need (programmatically) to get to a specific record of that
> Query
> > set ? Is there something corresponding to Setkey/Gototkey of a TTable ?

> Yes. You should use "Locate" instead.

> Example:
> if Query1.Locate('CustomerId', EditCustomer.Text, []) then
> begin
>    // found
> end
> else
> begin
>   // not found
> end;

> Andreas Schmidt

> PS: do not use Locate on Queries with a large record count.

Re:Locating a record


Regardless of the record count, locate is fine.
There's also moveby, for example, if you want to go n records forward:
moveby(n);

Cheers,

John Bleau

"Franco Jommi" <franco.jom...@tin.it> a crit dans le message news:
jXLj9.155125$pX1.5582...@news2.tin.it...

Quote
> Thanks for the lightening answer. But what if the record count is large
and
> I need to access that specific record ? Is there some other way ?

> "Andreas Schmidt" <a_j_schm...@rocketmail.com> ha scritto nel messaggio
> news:amn8ur$79jm1$1@ID-13084.news.dfncis.de...

> > "Franco Jommi" <franco.jom...@tin.it> schrieb im Newsbeitrag
> > news:P6Fj9.130951$ub2.2843643@news1.tin.it...
> > > My DBGrid is correctly showing the result of my Query. I know that I
can
> > > position my mouse on a specific row on the DBGrid and correspondingly
> > access
> > > that record in the Query set with statements like
> > MyQuery.FieldByName( )...
> > > What if I need (programmatically) to get to a specific record of that
> > Query
> > > set ? Is there something corresponding to Setkey/Gototkey of a TTable
?

> > Yes. You should use "Locate" instead.

> > Example:
> > if Query1.Locate('CustomerId', EditCustomer.Text, []) then
> > begin
> >    // found
> > end
> > else
> > begin
> >   // not found
> > end;

> > Andreas Schmidt

> > PS: do not use Locate on Queries with a large record count.

Re:Locating a record


Quote
"Franco Jommi" <franco.jom...@tin.it> wrote in message

news:jXLj9.155125$pX1.5582385@news2.tin.it...

Quote
> Thanks for the lightening answer. But what if the record count is large
and
> I need to access that specific record ? Is there some other way ?

Reduce the number of retrieved records with the SQL WHERE clause.

example:

SELECT * FROM tablename
WHERE OrderDate > '2002.01.01'

Andreas Schmidt

Other Threads