Delphi Examples: DbiAddFilter

<< Click to Display Table of Contents >>

Navigation:  »No topics above this level«

Delphi Examples: DbiAddFilter

Return to chapter overview

Add a filter that only shows records that the specified field is not NULL. This example uses the following input:
         fDbiAddFilter(Table1, Table1.FieldByName('Addr1'), hFilter);

Note:        When done with filter, call DbiDeactivateFilter and DbiDropFilter. If table is connected to data aware cnotrols, call TTable.Refresh.

procedure fDbiAddFilter(Table: TTable; Field: TField; var hF: hDBIFilter);

type

 // Setup the node structure

 TNodes = record

   UNode: CANUnary;

   FNode: CANField;

end;

 

var

 Nodes: TNodes;

 Expression: CANExpr;

 pCan: pByte;

 

begin

 // Uninary Node - set the operator to NOT BLANK or (NOT NULL)

with Nodes.UNode do begin

   nodeClass := nodeUNARY;

   canOp := canNOTBLANK;

   iOperand1 := 12;

end;

 // Field Node - set the field number and literal pool offset

with Nodes.FNode do begin

   nodeClass := nodeFIELD;

   canOp := canFIELD2;

   iFieldNum := Field.Index + 1;

   iNameOffset := 0;

end;

 // Expression - set the expression size, nodes and start positions

with Expression do begin

   iVer := 1;

   iTotalSize := sizeof(CANExpr) + sizeof(Nodes) + Length(Field.FieldName) + 1;

   iNodes := 2;

   iNodeStart := sizeof(CANExpr);

   iLiteralStart := sizeof(CANExpr) + sizeof(Nodes);

end;

 

 GetMem(pCan, Expression.iTotalSize * sizeof(BYTE));

try

   // Move expression, nodes and literal pool into a contiguous memory space

   Move(Expression, pCan^, sizeof(CANExpr));

   Inc(pCan, sizeof(CANExpr));

   Move(Nodes, pCan^, sizeof(Nodes));

   Inc(pCan, sizeof(Nodes));

   Move(Field.FieldName, pCan^, Length(Field.FieldName) + 1);

   Dec(pCan, sizeof(Nodes) + sizeof(CANExpr));

   // Add and activate the filter

   Check(DbiAddFilter(Table.Handle, 0, 1, False, pCANExpr(pCan), nil, hF));

   Check(DbiActivateFilter(Table.Handle, hF));

finally

   FreeMem(pCan, Expression.iTotalSize * sizeof(BYTE));

end;

end;