Solved

Grid & SQL

Posted on 2002-06-28
4
252 Views
Last Modified: 2010-04-04
I have a Grid that has been populated with information retrieved via a SQL query.

I want to be able to multi-select or use a check-box to select only the records I want to use.

When I have chosen my records I then need to find a way of sending only those records selected to a table.

Any ideas??
Thanks
0
Comment
Question by:HotRod40
  • 3
4 Comments
 
LVL 27

Expert Comment

by:kretzschmar
ID: 7115954
thats easy,

sample follows in some hours,
because i have a ready sample,
but just not here

therefore just some hints

set the rowselect and multiselect-options of the gris to true
after selection, iterate through the selected rows

meikl ;-)
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 7116245
here it comes,
sorry its not commented/explained

just ask, if unclear

two grids, where the selected rows are transfered to another table

unit uebertrag_u;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  DBTables, Grids, DBGrids, Db, ExtCtrls, ComCtrls, Buttons, DBCtrls,
  StdCtrls;

type
  TUForm = class(TForm)
    QSource: TQuery;
    DSSource: TDataSource;
    DDest: TDBGrid;
    TDest: TTable;
    DSDest: TDataSource;
    DSource: TDBGrid;
    Sp: TSplitter;
    Panel1: TPanel;
    StatusBar: TStatusBar;
    DBN: TDBNavigator;
    UB: TSpeedButton;
    CB: TComboBox;
    In1: TEdit;
    In2: TEdit;
    Label1: TLabel;
    RR: TSpeedButton;
    SBUP: TSpeedButton;
    SBDN: TSpeedButton;
    TDestTmp: TTable;
    procedure SpMoved(Sender: TObject);
    procedure UBClick(Sender: TObject);
    procedure RRClick(Sender: TObject);
    procedure DSourceKeyPress(Sender: TObject; var Key: Char);
    procedure FormResize(Sender: TObject);
    procedure SBUPClick(Sender: TObject);
    procedure SBDNClick(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  UForm: TUForm;

implementation

{$R *.DFM}

procedure TUForm.SpMoved(Sender: TObject);
begin
  If Sp.left - UB.Width > 0 Then
    UB.Left := Sp.left - (UB.Width div 2)
  else
    UB.Left := 0;
end;

procedure TUForm.UBClick(Sender: TObject);
var i,j : Integer;
begin
  if DSource.SelectedRows.Count > 0 then
  begin
    for j := 0 to DSource.SelectedRows.Count - 1 do
    begin
      QSource.GotoBookmark(TBookMark(DSource.SelectedRows.Items[J]));
      TDest.Append;
      For i := 0 to QSource.FieldCount - 1 do
        TDest.Fields[i] := QSource.Fields[I];
      TDest.Post;
    end;
  end;
end;



procedure TUForm.RRClick(Sender: TObject);
const SQLs = 'select name, adrl1, street, zip, city, state from adressen';
var s : String;
begin
  QSource.DisableControls;
  QSource.Active := False;
  QSource.SQL.Clear;
  If CB.Text = '' Then S := ''
  else
    s := ' where '+CB.Text+' between "'+In1.Text+'" and "'+In2.Text+'"';
  QSource.SQL.Add(SQLs+s);
  Try
    QSource.Active := True;
  except
    Showmessage('Eingabefehler ! Startzustand wird wieder hergestellt ! Statement = '+SQLs+S);
    QSource.Active := False;
    QSource.SQL.Clear;
    QSource.SQL.Add(SQLs);
    QSource.Active := True;
  end;
  QSource.EnableControls;
end;

procedure TUForm.DSourceKeyPress(Sender: TObject; var Key: Char);
begin
  If Key = #13 then UBClick(Self);
end;

procedure TUForm.FormResize(Sender: TObject);
begin
  SBUP.Left := DBN.Left - 50;
  SBDN.Left := DBN.Left - 25;
end;

procedure TUForm.SBUPClick(Sender: TObject);
var i : Integer;
begin
  TDestTmp.GotoCurrent(TDest);
  TDestTmp.Prior;
  TDestTmp.Insert;
  For i := 0 to TDest.FieldCount - 1 do
    TDestTmp.Fields[i] := TDest.Fields[I];
  TDest.Delete;
  TDest.GotoCurrent(TDestTmp);
end;

procedure TUForm.SBDNClick(Sender: TObject);
var i : Integer;
begin
  TDestTmp.GotoCurrent(TDest);
  TDestTmp.Next;
  TDestTmp.Next;
  If not TDestTmp.EOf then
  begin
    TDestTmp.Insert;
    For i := 0 to TDest.FieldCount - 1 do
      TDestTmp.Fields[i] := TDest.Fields[I];
    TDest.Delete;
    TDest.GotoCurrent(TDestTmp);
  end;
end;

end.


hope this helps

meikl ;-)
0
 
LVL 27

Accepted Solution

by:
kretzschmar earned 75 total points
ID: 7116403
just to point out what you need, i guess

procedure TUForm.UBClick(Sender: TObject);
var i,j : Integer;
begin
 if DSource.SelectedRows.Count > 0 then
 begin
   for j := 0 to DSource.SelectedRows.Count - 1 do
   begin
     QSource.GotoBookmark(TBookMark(DSource.SelectedRows.Items[J]));
     TDest.Append;
     For i := 0 to QSource.FieldCount - 1 do
       TDest.Fields[i] := QSource.Fields[I];
     TDest.Post;
   end;
 end;
end;


meikl ;-)
0
 

Author Comment

by:HotRod40
ID: 7121806
Thanks but theres one more thing.

I want to be able to update a field but only with the records that have been selected.
How can I do this?

I have tried your example above and it works great with a Select SQL query. I have tried it with an Update SQL query and it says the dataset is not open even though I have actually called the ExecSQL command.

Can I not use an Update within this loop that you have demonstrated for me??

Thanks
Scott
0

Featured Post

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question