Title: Refreshing a TQuery without losing current record
Question: How can I refresh a query without losing the current record?
Answer:
Here is a procedure you can use to refresh a query without losing CurrentRecord. You just have to indicate, as a string separeted by semicolons, which fields the procedure have to use to locate the current record.
procedure RefreshQuery(AQuery : TQuery; const FieldsForSearch: String);
var
AList : TList;
AVarArray : Variant;
i : Byte;
begin
AList := TList.Create;
try
AQuery.GetFieldList(AList, FieldsForSearch);
AVarArray := VarArrayCreate([0, AList.Count - 1], varVariant);
for i := 0 to Pred(AList.Count) do
AVarArray[i] := TField(AList.Items[i]).AsVariant;
AQuery.Close;
AQuery.Open;
AQuery.Locate(FieldsForSearch, AVarArray, []);
finally
AList.Free;
AVarArray.Free;p
end;
end;