Dbctrlgrid record positioning

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
LVL 1
alanjbrownAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

kretzschmarCommented:
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
alanjbrownAuthor Commented:
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
kretzschmarCommented:
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
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

alanjbrownAuthor Commented:
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
geobulCommented:
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
geobulCommented:
Oops,
recordno := adoDiary.fieldbyname('ref').asinteger; // save record number

should be

recordno := adoDiary.RecNo;

Regards, Geo
0
alanjbrownAuthor Commented:
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
geobulCommented:
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
kretzschmarCommented:
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
geobulCommented:
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
alanjbrownAuthor Commented:
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
kretzschmarCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
alanjbrownAuthor Commented:
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
kretzschmarCommented:
>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
alanjbrownAuthor Commented:
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
kretzschmarCommented:
>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
alanjbrownAuthor Commented:
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
kretzschmarCommented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.