Solved

Overriding the Dobeforepost procedure in Tquery Help!

Posted on 2014-01-06
21
720 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
  • 11
  • 7
  • 2
21 Comments
 
LVL 25

Expert Comment

by:Sinisa Vuk
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 25

Expert Comment

by:Sinisa Vuk
Comment Utility
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 36

Expert Comment

by:Geert Gruwez
Comment Utility
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
Comment Utility
??
0
 
LVL 36

Expert Comment

by:Geert Gruwez
Comment Utility
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
Comment Utility
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
Comment Utility
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 36

Expert Comment

by:Geert Gruwez
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 36

Expert Comment

by:Geert Gruwez
Comment Utility
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
Comment Utility
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 36

Accepted Solution

by:
Geert Gruwez earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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 36

Expert Comment

by:Geert Gruwez
Comment Utility
it was somewhat longer than a second by my reckoning :)
0
 

Author Closing Comment

by:Remmie26
Comment Utility
Thanks
0
 

Author Comment

by:Remmie26
Comment Utility
Geert Gruwez what wrong with the way I closed the question?
0
 
LVL 36

Expert Comment

by:Geert Gruwez
Comment Utility
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
Comment Utility
LOL I guess I am going crazy after all
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

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…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

772 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

11 Experts available now in Live!

Get 1:1 Help Now