You are here:

Delphi/Delphi index search

Advertisement


Question
QUESTION: To look up a multi key in Delphi (index is a multi key) I would code
Findkey[(A,B]). This works if fields a and b are full and correspond to the database fields.
If say field A is only 50% full and field B is 100% full then the search fails.
Is there a way of padding out field A with blanks or a way of recognizing that field A is only partially full. Delphi seems to truncate field A if it is not blank resulting in a no find.

ANSWER: Searching for NULL values is an issue in not only Findkey, but most of the other searching methods such as Locate.  I normally detect the user's search intent and build the query based on the need.  SQL would require 'MyField IS NULL' syntax to find the record and a parameterized query such as 'MyField=:FieldVal'  ParamByName['FieldVal'].AsVariant := NULL will not find the record.  The query will need to be adjusted based on the searching need.

Hope that helps.

---------- FOLLOW-UP ----------

QUESTION: I figured there would be a problem.
Field A would normally say contain '123456' and field B '7890' and using table1.findkey([A,B]) finds the record.
If field A is '1234  ' and field B '7890' and the database field contents are also '1234  ' and '7890' because Delphi truncates field A to '1234' there is no match because the second key B is tagged onto the end of field A and there is no record in the index with '12347890'. Some other languages don't truncate (clipper) and I was wondering if there was a way to stop the truncation.
One way around it is to just look for the A field and set a filter for the B field.

Answer
It probably has more to do with either the field data type or the database than Delphi.  I remember having a similar problem with an early version of either MySQL or Interbase (Firebird) with character fields that had no concept of string length.  Have you considered using a TQuery instead of a TTable to perform your search?  You could build a simple query to pull those values and they would be treated separately.  

var
 MyQuery: TQuery;

MyQuery.SQL.Text := 'SELECT * FROM MyTable WHERE FieldA=:A AND FieldB=:B';
MyQuery.ParamByName('A').AsString := '1234  ';
MyQuery.ParamByName('B').AsString := '7890';
MyQuery.Open;
if not MyQuery.IsEmpty then
 ShowMessage('Found it!');

Delphi

All Answers


Answers by Expert:


Ask Experts

Volunteer


Randy Sill

Expertise

Turbo Pascal and Delphi developer since 1986. Strengths - UI, Windows API, Database, SQL, Internet, Threading, NT Services.

Experience

Turbo Pascal and Delphi developer since 1986.

Education/Credentials
Delphi 5 Certification, Borland

Awards and Honors
2005 Indiana IT/Software Million Dollar Award, Key Technical Contributor

©2016 About.com. All rights reserved.