Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Dbctrlgrid record positioning

Posted on 2003-11-11
18
Medium Priority
?
1,308 Views
Last Modified: 2010-04-03
I have a dbCtrlGrid linked to an ado datasource. After the user updates a record I re- query the datasource and the record updated is not returned by the query. I am trying to position the next record in the dataset on the panel that was active previously. The code below does not give the correct results
Any help would be appreciated.


      adoDiary.next;  // move to next record
      recordno := adoDiary.fieldbyname('ref').asinteger; // save record number
      activePanel := dbCtrlgrid1.PanelIndex;    // save index of current active panel
      doquery;                                               // re query data
      adoDiary.Locate('ref', recordno, []);        // locate record
      dbCtrlgrid1.PanelIndex := activePanel;    //



Regards

Alan
0
Comment
Question by:alanjbrown
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 7
  • 4
18 Comments
 
LVL 27

Expert Comment

by:kretzschmar
ID: 9721422
usual you don't need to query again

just use the requery-method of the dataset

i don't know what your doQuery does,
but if you do so the query must be closed and
reopened for fetching new

meikl ;-)
0
 
LVL 1

Author Comment

by:alanjbrown
ID: 9721451
I don't seem to have a requery method. I am using Delphi4 with Adonis Ado components linked to an MS Sql 7 database.

The query shows records where a certain field is Null after data has been entered in that field it should not be displayed on the grid.
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 9721470
oh, no standard ado,
well, then there may be no requery-method

??
>The query shows records where a certain field is Null
>after data has been entered in that field it should not be displayed on the grid.

i don't understand, why do u use locate then?

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 1

Author Comment

by:alanjbrown
ID: 9721491
I move to the next record before re querying the data, I then store the reference number of the next record after the one updated. I then requery the data and return all the records that meet the criterior. Locate then moves to the actual record that I think is the one that the user will be interested in, but all the records meeting the criterior are displayed. I then try to position this record in the same position on the screen that was occupied before the requery.

0
 
LVL 17

Expert Comment

by:geobul
ID: 9721789
Hi,

Does your ado query have MoveBy method? if yes then:

      ...
      recordno := adoDiary.fieldbyname('ref').asinteger; // save record number
      activePanel := dbCtrlgrid1.PanelIndex;    // save index of current active panel
      doquery;
      adoDiary.MoveBy(recordno);
      dbCtrlgrid1.PanelIndex := activePanel;

Regards, Geo
0
 
LVL 17

Expert Comment

by:geobul
ID: 9721800
Oops,
recordno := adoDiary.fieldbyname('ref').asinteger; // save record number

should be

recordno := adoDiary.RecNo;

Regards, Geo
0
 
LVL 1

Author Comment

by:alanjbrown
ID: 9722037
Geo,

yes I do have a Moveby method.

I used adoDiary.fieldbyname('ref').asinteger because it uniquely identifies a record whereas using adoDiary.RecNo may not return the same record the next time the query is run as other users may have added, deleted or amended records in the underlying table.

Alan
0
 
LVL 17

Expert Comment

by:geobul
ID: 9722160
Yes, but using the same logic your adoDiary.fieldbyname('ref').asinteger record could have been deleted meanwhile. Perhaps combination could be a better solution. Remember the ref and the recno in different variables. Then first search for 'ref' and if locate returns false use RecNo.

What type is your 'ref' field in the database table? You said that Locate method doesn't work. Well, make it yourself:

function MyLocate(DataSet: TDataSet; FieldName: string; AValue: Variant): boolean;
begin
  result := false;
  with DataSet do begin
    First;
    while not eof do begin
      if FieldByName(FieldName).Value = AValue then begin
        result := true;
        break;
      end;
      Next;
    end;
  end;
end;

Regards, Geo
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 9722203
sorry, was absent,

following i thought
- you need to remember additional the record which is displayed in the first panel
- after your requery use moveby or locate for this record
-> so you have the record again which was displayed on top on top again
- step forward from this (next method) until you come to the record which was the next of the updated record

well, just a thought

meikl ;-)
0
 
LVL 17

Expert Comment

by:geobul
ID: 9722229
1. You may add to the function above:

with DataSet do begin
  SetTempState(dsFilter);
  try
    ...
  finally
    RestoreState(dsBrowse);
  end;
end;

2. If you decide to make both checks then before adoDiary.MoveBy add adoDiary.First; because when MyLocate returns false then the dataset cursor is eof.

Regards, Geo
0
 
LVL 1

Author Comment

by:alanjbrown
ID: 9722239
Hi Kretzschmar

I think your suggestion sounds as if it should give the correct result. Do you have a way of returning the 'ref' of the top record of the display?
0
 
LVL 27

Accepted Solution

by:
kretzschmar earned 2000 total points
ID: 9722584
well, not tested, but
setting the panelindex to 0,
the dataset should move to this there displayed record,
where u can then get some unique info for a later locate

simplified code flow:

var
  curIndex, topref, TargetRef : Integer;
begin

  do_yourUpdate;

  curIndex := dbctrlGrid1.panelIndex;
  dbctrlGrid1.panelIndex := 0;
  topref := dataset.fieldbyname('Ref').asinteger;
  dbctrlGrid1.panelIndex := curIndex;
  dataset.next;
  targetref := dataset.fieldbyname('Ref').asinteger;

  do_requery; //do requery

  dataset.locate('Ref',TopRef,[]);
  //well now we have to ways

  //simplest
  dbctrlGrid1.panelIndex := curIndex;

  //or
  while (not(dataset.eof)) and
          (dataset.fieldbyname('Ref').asinteger <> TargetRef) do
     dataset.next;
...

just from head, typos possible

meikl ;-)
 
0
 
LVL 1

Author Comment

by:alanjbrown
ID: 9723048
Kretzschmar

That looking good!
 UnfortunatlyI won't be able to test it until tommorrow.
One final point, how can I switch off the updating of the display during the process?

Alan
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 9723134
>UnfortunatlyI won't be able to test it until tommorrow.
dito, i can't test my suggestion myself until tomorrow :-))

we will see, if it works

meikl ;-)
0
 
LVL 1

Author Comment

by:alanjbrown
ID: 9730837
Kretzschmar

It seems to work. Thank you very much.

I will award the points very soon. Is it possible to switch off the updating of the dbctrlGrid during the proccess.


Alan
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 9730894
>It seems to work.

glad to hear this,
sorry, i didn't got the time to test myself

>Is it possible to switch off the updating of the
>dbctrlGrid during the proccess

try to use

dataset.disableControls;
try
  ...
finally
  dataset.enableControls;
end;

but i don't know, if then,
dbctrlGrid-methods are reflected to the dataset
-> do a try

meikl ;-)
0
 
LVL 1

Author Comment

by:alanjbrown
ID: 9731006
Kretzschmar

Unfortunatly disableControls stops the system working. However I am very happy with the result even without this as the effect of the movement of the ctrlgrid is hardly noticable on a fast system. I have awarded you the points
Thank you for your help, it was much appreciated.


Alan
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 9731077
well, thanks for the points, glad to helped you :-))

sorry, that it will not work with disable-controls,
u could also try

LockWindowsUpdate(dbctrlGrid1.handle);
try
  ... do your stuff here
finally
  LockWindowsUpdate(0);
end;

but this may have the side-effect,
that the desktop is refreshing its icons after
  LockWindowsUpdate(0);

meikl ;-)
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

721 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