Solved

Grid & SQL

Posted on 2002-06-28
4
250 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

930 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now