• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 795
  • Last Modified:

Overriding the Dobeforepost procedure in Tquery Help!

Hi can any one help I need to solve this

Query object inherited from TQuery which will only post a
record if it is currently in dsInsert or dsEdit state by overriding the
protected DoBeforePost procedure.

any ideas?
0
Remmie26
Asked:
Remmie26
  • 11
  • 7
  • 2
1 Solution
 
Sinisa VukCommented:
No need to override anything.Just check dataset state and do post. I use before scroll event. You can use OnClose event of form too.
procedure TForm1.dataBeforeScroll(DataSet: TDataSet);
begin
   PostData(DataSet);
end;

procedure TForm1.PostData(DataSet: TDataSet);
begin
    if DataSet.Active and (DataSet.State in [dsEdit, dsInsert]) then
    begin      
      DataSet.Post;
    end;
end;

Open in new window

0
 
Remmie26Author Commented:
I know, what you are saying and i agree with you however, I have a requirement to create an object base on TQuery and override its doBeforepost procedure to not post a
record if it is currently in dsInsert or dsEdit
0
 
Remmie26Author Commented:
I tried creating a component and it didn't work

I also tried  

 TxQuery = class(TQuery)
  public
    procedure DoBeforePost; override;
  end;


procedure TxQuery.DoBeforePost;
var
  aallow : boolean;
begin

  aallow :=  TcpQuery(self).DataSource.State in [dsedit,dsinsert];
  if not aallow  then
     abort;
 inherited DoBeforePost;
end;

but its not working what am I doing wrong?
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

 
Sinisa VukCommented:
You want to do Post or not when you in Edit/Insert state? Why do you need this - to make dataset readonly?

Anyway - try this:

procedure TxQuery.DoBeforePost;
var
  bEditIns : boolean;
begin

  bEditIns :=  Self.State in [dsEdit,dsInsert];
  if bEditIns then
  begin
    Self.Cancel;
    Abort;
  end;
end;

Open in new window


Your code test if dataset is in edit/insert and if it is not then do Abort !?!
0
 
Geert GOracle dbaCommented:
you want to edit/insert records and not post the changes to the database ?

Have you tried setting the following properties:
Query.CachedUpdates := True;

and in OnUpdateRecord event, reply with uaAbort

procedure TForm1.Query1UpdateRecord(DataSet: TDataSet; UpdateKind: TUpdateKind;
  var UpdateAction: TUpdateAction);
begin
  UpdateAction := uaAbort;
end;

this way you can edit the dbgrid without posting changes to the database
0
 
Remmie26Author Commented:
??
0
 
Geert GOracle dbaCommented:
a TQuery object only ever posts a record it is in dsEdit or dsInsert
otherwise the methode DoBeforePost doesn't get called

This the source for the Post method of TDataset:

procedure TDataSet.Post;
begin
  UpdateRecord;
  case State of
    dsEdit, dsInsert:
      begin
        DataEvent(deCheckBrowseMode, 0);
        DoBeforePost;
        CheckOperation(InternalPost, FOnPostError);

Open in new window


what are you trying to accomplish ?
0
 
Remmie26Author Commented:
code test to see if data set is in edit or insert mode and if not abort the post otherwise continue. It is a something I need to answer and I can can not understand the logic behind it by still need to answer.

also I need  the new DoBeforePost to override the Tdataset.dobeforepost (because is a subclass of Tquery). and unless I call it manually xquery1.dobeforepost it does not execute

help
0
 
Remmie26Author Commented:
Geert_Gruwez you are correct, I looked at the DB.pas as well to see the original dobeforepost and for the life of me I can not figure out why they want what they want,. but still i need to answer :).  how do I make sure the overriden procedure is the one that get executed?
0
 
Geert GOracle dbaCommented:
that didn't make sense to me

DoBeforePost does not get called when the Dataset is not in Edit or Insert mode
you will never get that event called, so it's pointless overriding it

if i just open a query/datasource/grid on my form, i get this error:

procedure TForm1.Button1Click(Sender: TObject);
begin
  Query1.Post;
end;

>> Error: Query1: Dataset not in edit or insert mode
0
 
Geert GOracle dbaCommented:
you can catch the error if it is not in that mode:

procedure TForm1.Button1Click(Sender: TObject);
begin
  try
    Query1.Post;
  except
    on E: Exception do
      ShowMessage(Format('class: %s, message: %s', [E.ClassName, E.Message]));
  end;
end;

>> this gives:
---------------------------
Project1
---------------------------
class: EDatabaseError, message: Query1: Dataset not in edit or insert mode
---------------------------
OK  
---------------------------
0
 
Remmie26Author Commented:
LOL what I thought and spend hours to get it to work with no luck. I did everything you did  just want to make sure I give the correct advice

BTW this is the original request

Write a TxQuery object inherited from TQuery which will only post a
record if it is currently in dsInsert or dsEdit state by overriding the
protected DoBeforePost procedure.
0
 
Geert GOracle dbaCommented:
the answer is the following

DoBeforePost only gets called when the dataset is in dsEdit or dsInsert start
otherwise an error is raised
The  checks are done in the Post methode of the TDataset component

the one asking you the question is either new to Delphi, has no clue what the underlying objects do or wanted to send you on a wild goose chase
0
 
Remmie26Author Commented:
haha  I guess  "wanted to send you on a wild goose chase" thanks I thought i am going crazy for a second.
0
 
Remmie26Author Commented:
I've requested that this question be closed as follows:

Accepted answer: 0 points for Remmie26's comment #a39761546

for the following reason:

this it the answer i was seeking
0
 
Geert GOracle dbaCommented:
it was somewhat longer than a second by my reckoning :)
0
 
Remmie26Author Commented:
Thanks
0
 
Remmie26Author Commented:
Geert Gruwez what wrong with the way I closed the question?
0
 
Geert GOracle dbaCommented:
you accepted your own answer #a39761546 at first which only indicated someone maybe being crazy but didn't have any solution to the question

now it's ok

it's possible to accept multiple answers also if multiple answers helped.
0
 
Remmie26Author Commented:
LOL I guess I am going crazy after all
0
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.

Join & Write a Comment

Featured Post

Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

  • 11
  • 7
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now