2005. március 8., kedd

Filter Table,Query with Exception Handling


Problem/Question/Abstract:

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.

Nincsenek megjegyzések:

Megjegyzés küldése