Re: Buttons to change SQL in ADOQuery



"Graham" <print@xxxxxxxxxxxxxx> wrote in
news:4630659d@xxxxxxxxxxxxxxxxxxxxxx:

I would like to be able to click on a set of small buttons captioned
A, B, C etc, to change the SQL statement of an ADOQuery. in Delphi 6.
The SQL refers to an Access table called HomeAd, and the buttons
should filter out records where the Surname field begins with A,B,C
etc. This shows what I have done:

procedure TfmHomeAddMain.btnAClick(Sender: TObject);
begin
with HomeQuery do
begin
Close;
SQL.Clear;
SQL.Add('SELECT * FROM HomeAd');
if Sender = btnA then
SQL.Append('WHERE Surname LIKE''A%''');
Open;
end;
end;

If I use a Parameter, I think it would have to take the whole of the
surname field, not just the first letter?. There is a calculated field
which displays the first latter, but it is not in the underlying
dataset.

Is there some simple way to repeat the above procedure without having
one for each of 26 buttons? I thought of a case statemetn, but it
needs an integer in case xxx of, I can't see how to use Sender here.

Can anyone advise please?

Graham.

Set the integer Tag field of each button to be the Ord value of each
character, e.g. btnA.Tag := Ord('A'); etc

then, in the event routine (i.e. the same routine for each button),
something like

SQL.Clear;
SQL.Add(Format('SELECT * FROM HomeAd WHERE Surname LIKE ''%s%%''',
[Char((Sender as TButton).Tag)]));

HTH
.