Title: Acces Style Filtering in Tables
Question: How I can filter in tables like access style. just selecting cell and push the button filter
Answer:
It is very easy. You have to just find oppraparate cell. I mean active cell then send the value of the active cell to the string then just put thar string to slq with its field name.
Like this
var
FSQLWord: string;
FSQLWord:string
if (key=$26) or (key=$25)or(key=$28)or(key=$27)then
FSQLWord:=DBGrid1.SelectedField.DisplayText;
FSQLFieldName:=DBGrid1.SelectedField.FieldName;
Now I am showing you the full unit with its code.
unit TBLViewUnit;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls, DBCtrls, Grids, DBGrids, ComCtrls, ImgList, ToolWin;
type
TTableViewForm = class(TForm)
DBGrid1: TDBGrid;
ControlBar1: TControlBar;
ToolBar1: TToolBar;
DBNavigator1: TDBNavigator;
ImageList1: TImageList;
SelectBt: TToolButton;
ToolButton2: TToolButton;
procedure DBGrid1KeyUp(Sender: TObject; var Key: Word;
Shift: TShiftState);
procedure DBGrid1CellClick(Column: TColumn);
procedure SelectBtClick(Sender: TObject);
private
{ Private declarations }
public
{$H-}
FSQLWord:String;
FSQLFieldName:String;
{$H+}
end;
var
TableViewForm: TTableViewForm;
implementation
uses DMod1, main;
{$R *.dfm}
procedure TTableViewForm.DBGrid1KeyUp(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
if (key=$26) or (key=$25)or(key=$28)or(key=$27)then
FSQLWord:=DBGrid1.SelectedField.DisplayText;
FSQLFieldName:=DBGrid1.SelectedField.FieldName;
end;
procedure TTableViewForm.DBGrid1CellClick(Column: TColumn);
begin
FSQLWord:=DBGrid1.SelectedField.DisplayText;
FSQLFieldName:=DBGrid1.SelectedField.FieldName;
end;
procedure TTableViewForm.SelectBtClick(Sender: TObject);
{$H-}
var TblName:string;
{$H+}
begin
if(SelectBt.Down)then
begin
if DMod.GaraTableView.TableName'' then
begin
TblName:=DMod.GaraTableView.TableName;
try
with DMod do
begin
GaraTableView.Active:=false;
GaraTableView.Sql.Clear;
GaraTableView.Sql.Add('select * from '+TblName);
GaraTableView.Sql.Add('where '+FSQLFieldName+' like '+''''+FSQLWord+'%'+'''');
GaraTableView.Active:=true;
end;
except on E:Exception do
ShowMessage('Error in SQL');
end;
end;
end else
begin
if DMod.GaraTableView.TableName'' then
begin
TblName:=DMod.GaraTableView.TableName;
try
with DMod do
begin
GaraTableView.Active:=false;
GaraTableView.Sql.Clear;
GaraTableView.Sql.Add('select * from '+TblName);
GaraTableView.Active:=true;
end;
except on E:Exception do
ShowMessage('Error in SQL2');
end;
end;
end;
end;
end.
I hope that it will help you