Lookup和Locate方法使用

5.LookUp方法
搜索符合一定条件的记录,如果找到返回指定的字段数值
function Lookup(const KeyFields:String;const KeyValues:Variant;const ResultFields:String):Variant;

KeyFileds 参数是个字符串,它指定了搜索的字段.可以是一个或多个字段名,中间要用分号隔开
KeyValues参数是Variant类型,可以包含任何数据类型,如果KeyFields参数指定搜索多个字段,那么KeyValues 参数需要用VarArrayOf函数
生成Variant类型数组;
如果找到符哈记录,返回字段名由ResultFields参数指定.可以是一个或多个字段名.
如果没有找到返回一个空值

6.Locate方法
Locate方法是用来搜索指定条件的记录,并将记录指针指向该记录
type
TLocateOption = (loCaseInsensitive,loPartialKey);
TLocateOptions = set of TLocateOption;
function Locate(const KeyFields:String;Const KeyValues:Variant;Options:TLocateOptions):Boolean;

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Grids, DBGrids, DB, DBTables, StdCtrls, DBCtrls;

type
TForm1 = class(TForm)
Table1: TTable;
DataSource1: TDataSource;
DBGrid1: TDBGrid;
Button1: TButton;
Memo1: TMemo;
Button2: TButton;
DBComboBox1: TDBComboBox;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}
//-----------Locate方法使用-------------------
procedure TForm1.Button1Click(Sender: TObject);
var
KeyFields:string;
KeyValues:Variant;


begin
keyFields := 'Contact;Phone';
KeyValues := VarArrayOf(['Joe Bailey','011-5-697044']);

if Table1.Locate(KeyFields,KeyValues,[loPartialKey]) then
begin
TAble1.MoveBy(Table1.RecNo);
end;


end;
//LookUp方法
procedure TForm1.Button2Click(Sender: TObject);
var
KeyFields:string;
KeyValues:Variant;
ResultString:String;
ReturnValue:Variant;
IndexOfArray,BoundLow,BoundHigh:Integer;

begin
KeyFields:='Company;City';
KeyValues := VarArrayOf(['Unisco','Freeport']);
REsultString := 'CustNo;Addr1';
ReturnValue := Table1.Lookup(KeyFields,KeyValues,ResultString);

if Not VarIsNull(ReturnValue) then
begin
BoundLow := VarArrayLowBound(ReturnValue,1);
BoundHigh := varArrayLowBound(ReturnValue,1);

for IndexOfArray := BoundLow to BoundHigh do
begin
Memo1.Lines.Add(ReturnValue[IndexOfArray]);
end;
end;

end;

LookUp方法中要注意ResultString中使用

ResultString主要返回指定字段值的内容

返回的值是保存在ReturnValues可变数组中

主要是KeyValues使用

每天早上敲醒自己的不是闹钟,是夢想!
原文地址:https://www.cnblogs.com/yplong/p/2351153.html