Title: Filter Table,Query with Exception Handling
Question: This demonstrates how to filter a table with exception handling and also demonstrates how the overload directive can be used
Answer:
function FilterTable(Data:TQuery;Filter:String):String;overload;
function ExecuteSQL(Data:TQuery;F:TStrings):String;
function ExecuteSQL(Data:TQuery;F:TStrings):String;
var
TSQL:TStrings;
begin
try
TSQL:=TStringList.Create;
TSQL.Assign(Data.SQL);
try
Result:=Data.Bookmark;
Data.Active:=False;
Data.SQL.Assign(F);
Data.Active:=True;
except
on EDBEngineError do
begin
Data.SQL.Assign(TSQL);
TSQL.Free;
TSQL:=Nil;
Data.Active:=True;
end;
end;//try except
finally
if TSQL nil then TSQL.Free;
end;
end;
function FilterTable(Data:TTable;Filter:String):String;
begin
try
Result:=Data.Bookmark;
Data.Active:=False;
Data.Filtered:=True;
Data.FilterOptions:=[foCaseInsensitive];
Data.Filter:=Filter;
Data.Active:=True;
except
on EDatabaseError do
begin
Data.Filter:='';
Data.Active:=True;
end;
end;//try except
end;
A few routines that can be used to clean up code.