How to use a RecordSet.Find with TADOQuery?

3.9k views Asked by At

In this question:

Delphi ADO : Locate with dataset filter on bug

an ADO bug was described where the Filter string is ignored during .Locates.

This is causing problems for us migrating from the BDE because we have a lot of code that changes the filter according to user input.

We had expected TADOQuery to provide a working migration path. We were wrong.

We can of course change our current filters to WHERE statements, but that's a lot of work and risk concatenating the filter strings to filter-less WHERE statements, etc.

The accepted answer to the question linked to above suggests the possibility of using TCustomADODataSet.Recordset.Find

Can we safely use RecordSet.Find in a TADOQuery just to implement .Locates? i.e. Does RecordSet.Find update whatever wrapper TADOQuery puts around TADOQuery?

If so, can someone show a sample call from Delphi XE5 to RecordSet find? I'm having trouble figuring out the arguments.

1

There are 1 answers

0
kobik On

Your best option is to look at the source code of ADODB.pas TCustomADODataSet.LocateRecord. You will see how Locate is implemented.

For a single condition it uses RecordSet Find method. for multiple conditions it uses the Filter property.

Using Find is fairly easy:

uses ..., ADODB, ADOInt;

procedure TForm1.Button1Click(Sender: TObject);
var
  bm: TBookmarkStr;
begin
  bm := ADODataSet1.Bookmark;
  // for partial condition use e.g. ItemName LIKE 'He*'
  ADODataSet1.Recordset.Find('ItemName = ''Hello''', 0, adSearchForward, adBookmarkFirst);
  if ADODataSet1.Recordset.EOF then // not found
    ADODataSet1.Bookmark := bm // restore bookmark
  else
    ADODataSet1.Resync([rmExact, rmCenter]); // set active record and trigger dataset change event
end;

Since TADOQuery is a TCustomADODataSet descendant there is no problem using RecordSet same as TADODataSet