?
Solved

Overriding the Dobeforepost procedure in Tquery Help!

Posted on 2014-01-06
21
Medium Priority
?
740 Views
Last Modified: 2014-01-07
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
Comment
Question by:Remmie26
[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
  • 11
  • 7
  • 2
21 Comments
 
LVL 27

Expert Comment

by:Sinisa Vuk
ID: 39760687
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
 

Author Comment

by:Remmie26
ID: 39760693
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
 

Author Comment

by:Remmie26
ID: 39760702
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 27

Expert Comment

by:Sinisa Vuk
ID: 39761408
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
 
LVL 38

Expert Comment

by:Geert Gruwez
ID: 39761507
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
 

Author Comment

by:Remmie26
ID: 39761512
??
0
 
LVL 38

Expert Comment

by:Geert Gruwez
ID: 39761523
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
 

Author Comment

by:Remmie26
ID: 39761526
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
 

Author Comment

by:Remmie26
ID: 39761532
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
 
LVL 38

Expert Comment

by:Geert Gruwez
ID: 39761533
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
 
LVL 38

Expert Comment

by:Geert Gruwez
ID: 39761541
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
 

Author Comment

by:Remmie26
ID: 39761542
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
 
LVL 38

Accepted Solution

by:
Geert Gruwez earned 2000 total points
ID: 39761544
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
 

Author Comment

by:Remmie26
ID: 39761546
haha  I guess  "wanted to send you on a wild goose chase" thanks I thought i am going crazy for a second.
0
 

Author Comment

by:Remmie26
ID: 39761571
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
 
LVL 38

Expert Comment

by:Geert Gruwez
ID: 39761548
it was somewhat longer than a second by my reckoning :)
0
 

Author Closing Comment

by:Remmie26
ID: 39761572
Thanks
0
 

Author Comment

by:Remmie26
ID: 39761674
Geert Gruwez what wrong with the way I closed the question?
0
 
LVL 38

Expert Comment

by:Geert Gruwez
ID: 39761678
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
 

Author Comment

by:Remmie26
ID: 39761689
LOL I guess I am going crazy after all
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses
Course of the Month10 days, 6 hours left to enroll

764 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