function Tform1.VariantToList(VarOle:OleVariant):TStringList;
//先把olevariant转换成为TStringList类型
var
List:TStringList;
StrRecord:OleVariant;
i:integer;
High:integer;
Low:integer;
begin
if not VarIsArray(VarOle) then
Exit;
List:=TstringList.Create;
High:=VarArrayHighBound(VarOle,1);
Low:=varArrayLowBound(VarOle,1);
for i:=Low to High do
List.Add(VarOle[i]);
Result:=List;
end;
function Tform1.SearchBill(BillNumber:OleVariant):OleVariant;
var
Query : TQuery;
StrRecord:OleVariant; //存入查寻所得结果
BillList:TStringList; //存放单据号列表
strBillNo:String; //存放单据号
strBillPerfix:String; //存放单存类型
StrBillName:string; //单据名称
StrBillMName:string; //表名
i,index:integer;
begin
BillList:=TstringList.Create;
BillList:=VariantToList(BillNumber);//调用上面定义的函数
Query := TQuery.Create(nil);
Query.DatabaseName:='materiel_mgr';
i:=BillList.Count;
StrRecord:=VarArrayCreate([1,'i',1,6],OleVariant);
for i:=0 to BillList.Count-1 do
begin
strBillNo:=BillList.Strings[i];
index:=pos('-',strBillNo);
strBillPerfix:=copy(strBillNo,1,index-1);
try
Query.SQL.Clear;
Query.SQL.Add('select BillName,BillMName from TableType_Tab where BillPerfix='#39+strBillPerfix+#39);
Query.Open;
StrBillMName:=Query.FieldValues['BillMName'];
StrBillName:=Query.FieldValues['BillName'];
Query.SQL.Clear;
Query.SQL.Add('select makeDate,FDate,OutMan,State,LogInsertDate from '+StrBillMName+' where BillNo='+#39+strBillPerfix+#39);
Query.Open;
StrRecord[i][1]:=StrBillName;
StrRecord[i][2]:=Query.FieldValues['makeDate'];
StrRecord[i][3]:=Query.FieldValues['FDate'];
StrRecord[i][4]:=Query.FieldValues['OutMan'];
StrRecord[i][5]:=Query.FieldValues['State'];
StrRecord[i][6]:=Query.FieldValues['LogInsertDate'];
except
Result:=-1;
end;
end;
Result:=StrRecord;
end;
use comobj;
.............................................
var varole:olevariant;
vararray:array of .......
begin
varole:=createoleobjete( );
vararray:=varole;
-================
Olevariant的高级用法是构造可变数组;调用vararraycreate()
function vararraycreate(const bounds:array of integer;vartype:integer):variant;
var a,b:olevariant;
i:integer;
begin
a:=vararraycreate(0,9],varinteger);
for i:=0 to 9 do a[i]:=i+1;
b:=vararraycreate([1,4,0,9],varvariant);
............
可变数组的基类型不能是varstring,要创建一个字符串类型的可变数组,应当用varolestr来代替varstring;