Solved

ADOQuery posting to Database immediately

Posted on 2004-09-17
19
1,498 Views
Last Modified: 2012-06-27
Hiya experts,
This is probably a simple solution, but I'm teaching myself Delphi so I'm somewhat new to it. I've got a MSAccess database where I'm saving data to. No problem here. In my application, I'm filling up a grid (from devExpress) with data by bounding it to a ADOQuery object and inserting directly into the query. The problem is, when I do the following:

qry.Insert;
qry.FieldByName('column').value := someVal;

it updates the database immediately. Any way to prevent this?

B.D.
0
Comment
Question by:Big Monty
  • 13
  • 6
19 Comments
 
LVL 12

Expert Comment

by:esoftbg
ID: 12086615
> qry.Insert;
> qry.FieldByName('column').value := someVal;

> it updates the database immediately. Any way to prevent this?

I can not believe that. To be updated it must to be:
qry.Post;

I will test that and will post a new comment after that ....
0
 
LVL 12

Expert Comment

by:esoftbg
ID: 12086830
I already tested your case and I'm sure that after:

  ADOQuery.Insert;
  ADOQuery.FieldByName('FIRST_NAME').Value := 'Emil';

the database is not updated immediately !

May be you use the event:
procedure TForm1.ADOQueryNewRecord(DataSet: TDataSet);
begin
  ADOQuery.FieldByName('ID').Value := Last_ID + 1;
  ADOQuery.Post;
end;
0
 
LVL 32

Author Comment

by:Big Monty
ID: 12087047
thats what i assumed i had to use to commit to the DB, qry.Post. But i kid you not that i don't have that line anywhere in my code. is there a property that automatically posts after you insert a record?
0
 
LVL 12

Expert Comment

by:esoftbg
ID: 12087132
There is not a property that automatically posts after you insert a record ....
There is events like:
procedure TForm1.ADOQueryNewRecord(DataSet: TDataSet);
begin
//........
end;

procedure TForm1.ADOQueryBeforeInsert(DataSet: TDataSet);
begin
//........
end;

procedure TForm1.ADOQueryAfterInsert(DataSet: TDataSet);
begin
  ADOQuery.FieldByName('MIDDLE_NAME').Value := 'S.';
  ADOQuery.Post;
end;

where is possible to be Posted a record in mistake
0
 
LVL 12

Expert Comment

by:esoftbg
ID: 12087295
But a post could be fired indirectly by DBGrid when the user changes the row wich contains the new record ....
A post could be fired by DBNavigator ....
In these cases there is not:
  ADOQuery.Post;
0
 
LVL 32

Author Comment

by:Big Monty
ID: 12087310
i'll check the grid settings when i get home tonight and see if thats whats causing the problem, although i think i tried it without the grid
0
 
LVL 12

Expert Comment

by:esoftbg
ID: 12087369
I don't mean the DBGrid settings, but the user activity with DBGrid when ADOQuery is in Insert mode:
If the user presses the down-arrow or the up-arrow keyboard keys, or clicks on another DBGrid row (not the current row where is the new inserted value) it fires internal Post for ADOQuery.
0
 
LVL 32

Author Comment

by:Big Monty
ID: 12108927
below is the code i'm using. i commented out the grid population mechanism so I know thats not it. is it because of the looping? is there a way around it?

procedure tform1.getalldir(s:string);
var rec :tsearchrec;
folderName :string;
index: integer;
begin
qry.Open;

index := 0;
if sysutils.findfirst(s + '*.*', faAnyFile, Rec)=0 then  begin
   repeat
     if (rec.name<>'.') and (rec.name<>'..') then
     begin
 //    gridView.DataController.FocusedRowIndex := index;
      folderName := Copy(rec.Name, pos('dmb', rec.Name), 13);
      qry.Insert;
     qry.FieldByName('showdate').Value := folderName;
     end;
     index := index+ 1;
   until SysUtils.FindNext(Rec) <> 0;
  sysutils.FindClose(rec);
end;
end;

B.D.
0
 
LVL 12

Expert Comment

by:esoftbg
ID: 12111384
I tested your code: You are absolutely right ! All the Inserted records are updated into the database .... It is very strange it is done without qry.Post ????
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 12

Expert Comment

by:esoftbg
ID: 12111409
But I do not understand why You do qry.Insert, but do not want to update the Table ???? If you don't want to update the Table, then do not do qry.Insert !!!!
0
 
LVL 12

Expert Comment

by:esoftbg
ID: 12111488
You can store the FileNames from the Directory search into TStringList and when really want to insert them into the Table to do that ....

procedure TForm1.FormCreate(Sender: TObject);
begin
  StringList := TStringList.Create;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  StringList.Free;
end;

procedure tform1.getalldir(s:string);
  var
    rec :tsearchrec;
    folderName :string;
    index: integer;
begin
  qry.Open;
  index := 0;
  if sysutils.findfirst(s + '*.*', faAnyFile, Rec)=0 then  begin
  repeat
     if (rec.name<>'.') and (rec.name<>'..') then
     begin
 //    gridView.DataController.FocusedRowIndex := index;
       folderName := Copy(rec.Name, pos('dmb', rec.Name), 13);
       StringList.Add(folderName);
       {
       qry.Insert;
       qry.FieldByName('showdate').Value := folderName;
       }
       end;
       index := index+ 1;
     until SysUtils.FindNext(Rec) <> 0;
    sysutils.FindClose(rec);
  end;
end;
0
 
LVL 12

Expert Comment

by:esoftbg
ID: 12112048
Forgot to mention about the declaration of the StringList

type
  TForm1 = class(TForm)
    //........
    private{ Private declarations }
    public { Public declarations }
      StringList:TStringList;
      procedure GetAlldir(S: string);
  end;
0
 
LVL 32

Author Comment

by:Big Monty
ID: 12112741
well, let me explain what I'm designing here. basically its a program that scans a directory for the user for music folders, then it will populate a grid. the grid i'm using is tied to a datasource, so i chose a query object to use. The only time i want to post to the database is when the user clicks the save button. the grid i'm using only populates when the datasource is populated. does that make any sense? :)

thx for the help
B.D.
0
 
LVL 12

Accepted Solution

by:
esoftbg earned 125 total points
ID: 12112768
There is another way using Transaction, but I don't like it ....
Anyway here it is:

procedure TForm1.GetAlldir(S: string);
var
  rec:           tsearchrec;
  folderName:    string;
//  index:         integer;
begin
//  index := 0;
  if sysutils.findfirst(S + '*.*', faAnyFile, Rec)=0 then
  try
    if not ADOConnection.InTransaction then
      ADOConnection.BeginTrans;
    repeat
      if (rec.name<>'.') and (rec.name<>'..') then
      begin
 //     gridView.DataController.FocusedRowIndex := index;

       folderName := Copy(rec.Name, pos('dmb', rec.Name), 13);
       StringList.Add(folderName);
       qry.Append;
       qry.FieldByName('USER_NAME').Value := folderName;
     end;
//     index := index+ 1;
    until SysUtils.FindNext(Rec) <> 0;
    sysutils.FindClose(rec);
  finally
    ListBox.Items.Assign(StringList);
  end;
end;


procedure TForm1.btnAppendClick(Sender: TObject);
begin
  GetAlldir('');
end;

procedure TForm1.btnCommitClick(Sender: TObject);
begin
  if ADOConnection.InTransaction then
  try
    if (qry.State in [dsInsert,dsEdit]) then
      qry.Post;
  finally
    ADOConnection.CommitTrans;
  end;
end;

procedure TForm1.btnRollBackClick(Sender: TObject);
var
  SID:    string;
begin
  if ADOConnection.InTransaction then
  begin
    SID := qry.FieldByName('ID').AsString;
    ADOConnection.RollbackTrans;
    qry.Active := False;
    qry.Active := True;
    qry.Locate('ID', SID, []);
  end;
end;
0
 
LVL 32

Author Comment

by:Big Monty
ID: 12112781
why dont you like it if i may ask?
0
 
LVL 12

Expert Comment

by:esoftbg
ID: 12112936
I don't like it because the Table usually has primary key which is an autoincrement field. This way every time there are inserted new records the field ID acquires new values, but even you press the button for RollBack theese values never be available for use ....
0
 
LVL 12

Expert Comment

by:esoftbg
ID: 12113437
I think the solution with a TStringList is a better one ....
0
 
LVL 12

Expert Comment

by:esoftbg
ID: 12149500
Using a TStringList you may visualize it on a TListBox instead of TDBGrid. But when you decide to post data from TStringList into Database by clicking some Button, just should do a loop:

procedure TForm1.btnPostClick(Sender: TObject);
var
  I:      Integer;
begin
  for I := 0 to StringList.Count-1 do
  begin
    qry.Insert;
    qry.FieldByName('column').AsString := StringList[I];
    qry.Post;
  end;
end;
0
 
LVL 32

Author Comment

by:Big Monty
ID: 12169654
my apologies for not closing this sooner, I've been away all of last week.

This worked so for now I'm going to use it. I don't care too much about the auto numbers not being re-usable, this is more of a personal project than anything.

Cheers
B.D.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Filemaker 14 vs Delphi Embarcadero 7 186
Newbie Thread Programming 1 139
Strange behavior when a form is closed 6 51
delphi parse string to params 3 100
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…
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…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

863 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

28 Experts available now in Live!

Get 1:1 Help Now