Solved

how to select or focus row there id is 500?

Posted on 2004-09-02
21
557 Views
Last Modified: 2010-04-05
my sql is:
AdoQuery1.SQL.Clear;
AdoQuery1.SQL.Add('Select * from klientai order by id');
AdoQuery1.Open;

and i need show 500 row...
hot to do it?
0
Comment
Question by:selas
  • 6
  • 4
  • 4
  • +3
21 Comments
 
LVL 22

Accepted Solution

by:
Ferruccio Accalai earned 500 total points
ID: 11964533
you can use locate

Adoquery.Locate('ID',500,[]);

If it's true then the cursor is moved to ID 500
0
 
LVL 6

Expert Comment

by:vadim_ti
ID: 11964577
AdoQuery1.SQL.Clear;
AdoQuery1.SQL.Add('Select TOP 500 * from klientai order by id');
AdoQuery1.Open;

0
 
LVL 12

Expert Comment

by:esoftbg
ID: 11965362
download an example from:
page:        http://www.geocities.com/esoftbg/
  link:        Q_20998677.zip
0
 
LVL 12

Expert Comment

by:esoftbg
ID: 11965395
unit Unit_Q_20998677;

interface

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

type
  TForm1 = class(TForm)
    qryNAMES: TQuery;
    dsqNAMES: TDataSource;
    dbgNAMES: TDBGrid;
    spbSelectAll: TSpeedButton;
    spbSelectCurrent: TSpeedButton;
    spbUnSelectAll: TSpeedButton;
    spbUnSelectCurrent: TSpeedButton;
    DBNavigator1: TDBNavigator;
    spbSelect5th_1_: TSpeedButton;
    spbSelect5th_2_: TSpeedButton;
    SpeedButton1: TSpeedButton;
    procedure FormCreate(Sender: TObject);
    procedure spbSelectAllClick(Sender: TObject);
    procedure spbUnSelectAllClick(Sender: TObject);
    procedure spbSelect5th_1_Click(Sender: TObject);
    procedure spbSelect5th_2_Click(Sender: TObject);
    procedure spbSelectCurrentClick(Sender: TObject);
    procedure spbUnSelectCurrentClick(Sender: TObject);
    procedure SpeedButton1Click(Sender: TObject);
  private{ Private declarations }
    SID:      string;
  public { Public declarations }
    procedure SelectAllNames(Value: Boolean);
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
  qryNAMES.Active := True;
  SID := dbgNAMES.DataSource.DataSet.FieldByName('ID').AsString;
end;

procedure TForm1.SelectAllNames(Value: Boolean);
var
  C:      Integer;
  I:      Integer;
begin
  with dbgNAMES.DataSource.DataSet do
  try
    dbgNAMES.Visible := False;
    begin
      DisableControls;
      First;
      while not EOF do
      begin
        dbgNAMES.SelectedRows.CurrentRowSelected := Value;
        Next;
      end;
    end;
  finally
    EnableControls;
    Val(SID, I, C);
    if (I>0) and (C=0) then
      Locate('ID', SID, []);
    dbgNAMES.Visible := True;
  end;
end;

procedure TForm1.spbSelectAllClick(Sender: TObject);
begin
  SID := dbgNAMES.DataSource.DataSet.FieldByName('ID').AsString;
  SelectAllNames(True);
end;

procedure TForm1.spbUnSelectAllClick(Sender: TObject);
begin
  SID := dbgNAMES.DataSource.DataSet.FieldByName('ID').AsString;
  SelectAllNames(False);
end;

procedure TForm1.spbSelect5th_1_Click(Sender: TObject);
begin
  if dbgNAMES.DataSource.DataSet.Locate('ID', 5, []) then // the value could be 500
    with dbgNAMES.DataSource.DataSet do
      dbgNAMES.SelectedRows.CurrentRowSelected := True;
end;

procedure TForm1.spbSelect5th_2_Click(Sender: TObject);
var
  I:       Integer;
begin
  with dbgNAMES.DataSource.DataSet do
  try
    First;
    I := 1;
    while (I<5) do
    begin
      Next;
      Inc(I);
    end;
  finally  
    dbgNAMES.SelectedRows.CurrentRowSelected := True;
  end;
end;

procedure TForm1.spbSelectCurrentClick(Sender: TObject);
begin
  with dbgNAMES.DataSource.DataSet do
    dbgNAMES.SelectedRows.CurrentRowSelected := True;
end;

procedure TForm1.spbUnSelectCurrentClick(Sender: TObject);
begin
  with dbgNAMES.DataSource.DataSet do
    dbgNAMES.SelectedRows.CurrentRowSelected := False;
end;

procedure TForm1.SpeedButton1Click(Sender: TObject);
begin
  with TStringGrid(dbgNAMES) do
  begin
    Row:=5;
    Col:=1;
    SetFocus;
  end;
  with dbgNAMES.DataSource.DataSet do
  begin
//    Refresh;
    dbgNAMES.SelectedRows.CurrentRowSelected := True;
  end;
end;

end.
0
 
LVL 17

Expert Comment

by:Wim ten Brink
ID: 11965416
Selecting the 500th record... Hmm...

AdoQuery.First;
Count:=0;
while (Count<500) and not AdoQuery.EOF do begin
  Inc(Count);
  AdoQuery.Next;
end;

:-)

I know, it's a bit silly but hey, it should work.
0
 
LVL 17

Expert Comment

by:Wim ten Brink
ID: 11965468
AdoQuery.First;
AdoQuery.MoveBy(500);

to move the cursor 500 records down from the first record. It's the easiest solution...
0
 
LVL 6

Expert Comment

by:bpana
ID: 11965890
>> Workshop_Alex
AdoQuery.First;
AdoQuery.MoveBy(500);
0
 
LVL 6

Expert Comment

by:bpana
ID: 11965912
sorry, submitted by mistake, it should be:
AdoQuery.First;
AdoQuery.MoveBy(499);
0
 
LVL 6

Expert Comment

by:bpana
ID: 11965938
this should be faster:

AdoQuery1.DisableControls;
AdoQuery1.First;
AdoQuery1.MoveBy(499);
AdoQuery1.EnableControls;
0
 
LVL 17

Expert Comment

by:Wim ten Brink
ID: 11967101
bpana, well found, that bug. :-)

DisableControls/EnableControls seem so obvious to me, though, that I tend to skip them in any example.
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 6

Expert Comment

by:vadim_ti
ID: 11967138
if ADOQuery1.RecordCount >= 500
  ADOQuery1.RecNo := 500;
0
 
LVL 6

Expert Comment

by:bpana
ID: 11967537
anyway, the best answer is the one of Ferruccio68.
0
 
LVL 22

Expert Comment

by:Ferruccio Accalai
ID: 11967845
tnx bpana :)
two words about my suggestion:
we don't know if ID values are really in sequential order (some intemediate ID value could have been deleted) so going to recno isn't always true.
TOP is another good suggestion, but it isn't available in all SQL languages, and we don't know wich SQL DB is used here.

SO my best suggestion still is about using locate.

F68 ;-)
0
 
LVL 17

Expert Comment

by:Wim ten Brink
ID: 11968781
Well, the question is a bit unclear. Does he want the 500th row or the record with ID=500, which could be the first record anyway?

If he wants the row where ID=500 then yes, your solution is the best, Ferruccio68.

It could be that he meant to select the top-500 records only, not more. Then vadim_ti would be correct. But TOP only works for MS SQL Server, if I'm correct. PostGre and MySQL use the LIMIT keyword instead. Oracle doesn't seem to support it at all... But isn't there a property in the ADO component that can specify that you only want an x amount of records returned?

But if he just wanted to select the 500th record, then I would be correct.
0
 
LVL 12

Expert Comment

by:esoftbg
ID: 11970175
The question title is:
> how to select or focus row there id is 500?

it is absolutelly clear about ID is 500 (but not 500-th record)

it is unclear about:
row  -  row of DBGrid   ?
row  -  record of table  ?

What about if he just want to select the row of the DBGrid with ID=500 without limitation for the count of records ?
0
 
LVL 1

Expert Comment

by:primusmagestri
ID: 11970675
Selecting the record in the query with Locate will take him to the row in grid, assuming he has set the datasource correctly :D

>What about if he just want to select the row of the DBGrid with ID=500 without limitation for the count of records ?
If you are concerned about the number of rows in DBGrid because of his (all so) simple query, then you are right. Querying big tables to display records in grids should never be done without a filtering on some fields and a configurable limit for the number of rows fetched (this helps the server, too :) ). So, selas, be advised, and add at least AND ROWNUM < MAX_ROW_NUM at the end of your query; MAX_ROW_NUM will get a value that you can configure through the GUI or some other method.
0
 
LVL 12

Expert Comment

by:esoftbg
ID: 11970875
> Selecting the record in the query with Locate will take him to the row in grid, assuming he has set the datasource correctly

Just terminology:    selected and current is not the same about DBGrid row ....
Locate does not select a row, it just does it to be the current record for a DataSet ....

      DBGrid.SelectedRows.CurrentRowSelected := True;  // does the current DBGrid row selected

What he wants exactly:
  to select the DBGrid row (ID=500);
  or just to do it the current one with procedure Locate ....;
  may be both of it  ????

with / without limitation for the count of records is a little bit out of the main point for this question ....
0
 
LVL 6

Expert Comment

by:bpana
ID: 11971486
why i didn't receive the last 4 comments (i've just received the accepted answer) ????
and why the order of the last comments is like this: ???????

Comment from Workshop_Alex
Date: 09/03/2004 12:03AM CEST

Comment from esoftbg
Date: 09/03/2004 06:00AM CEST

Comment from primusmagestri
Date: 09/03/2004 09:12AM CEST

Comment from esoftbg
Date: 09/03/2004 10:30AM CEST
0
 
LVL 6

Expert Comment

by:bpana
ID: 11971512
i've just received some of them right now (comments from esoftbg, primusmagestri)

do you have the same problem or it's hotmail?
0
 
LVL 1

Expert Comment

by:primusmagestri
ID: 11971560
I didn't receive the comments notification, myself; I am using a corporate mail server. I don't think the time field is relevant, but the order is correct.

> DBGrid.SelectedRows.CurrentRowSelected := True;  // does the current DBGrid row selected
It's true that I am using a  wraper over TQuery, but I don't know if that solves the problem. For me, Locate does the trick.
0
 
LVL 22

Expert Comment

by:Ferruccio Accalai
ID: 11972279
Selas, tnx, glad to have helped you :)

Just one more comment: maybe next time could be better to add some more explanation about your need (wich database, components used and so on). Sometime more than one comment can help and in this thread i think that if your exact need would be knowed every expert would been pointed to the right direction.

F68 ;-)
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

707 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

17 Experts available now in Live!

Get 1:1 Help Now