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

ADOQuery posting to Database immediately

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
Big Monty
Asked:
Big Monty
  • 13
  • 6
1 Solution
 
esoftbgCommented:
> 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
 
esoftbgCommented:
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
 
Big MontySenior Web Developer / CEO of ExchangeTree.org Author Commented:
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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
esoftbgCommented:
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
 
esoftbgCommented:
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
 
Big MontySenior Web Developer / CEO of ExchangeTree.org Author Commented:
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
 
esoftbgCommented:
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
 
Big MontySenior Web Developer / CEO of ExchangeTree.org Author Commented:
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
 
esoftbgCommented:
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
 
esoftbgCommented:
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
 
esoftbgCommented:
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
 
esoftbgCommented:
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
 
Big MontySenior Web Developer / CEO of ExchangeTree.org Author Commented:
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
 
esoftbgCommented:
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
 
Big MontySenior Web Developer / CEO of ExchangeTree.org Author Commented:
why dont you like it if i may ask?
0
 
esoftbgCommented:
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
 
esoftbgCommented:
I think the solution with a TStringList is a better one ....
0
 
esoftbgCommented:
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
 
Big MontySenior Web Developer / CEO of ExchangeTree.org Author Commented:
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
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 13
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now