Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

I can't update a table

Status
Not open for further replies.

aaanadie

Programmer
Mar 21, 2005
6
US
Hi,

I have a very simple code that works ok, but the ApplyUpdates don't have effect. This is the code:

procedure TFormPhonesFormat.ButtonBeginClick(Sender: TObject);
var
Phone: String;
FieldName: String;
begin
FieldName := 'Phone';
Table1.First;
while not Table1.Eof do
begin
if Table1.FieldByName(FieldName).OldValue <> null then
begin
Phone := Table1.FieldByName(FieldName).OldValue;
Phone := StringReplace(Phone, '(', '', [rfReplaceAll, rfIgnoreCase]);
Phone := StringReplace(Phone, ')', '', [rfReplaceAll, rfIgnoreCase]);
Phone := StringReplace(Phone, '-', '', [rfReplaceAll, rfIgnoreCase]);
Phone := StringReplace(Phone, ' ', '', [rfReplaceAll, rfIgnoreCase]);
Table1.Edit;
Table1.FieldByName(FieldName).NewValue := Phone;
end;
Table1.Next;
end;
Database1.ApplyUpdates([Table1]);
end;

Apparently everything is ok, but nothing change in Table1 after executing the code.

Thanks in advance.
 
I believe the correct code would be :

Code:
Table1.Edit;
Table1.FieldByName(FieldName).AsString := Phone;
Table1.Post;

--------------------------------------
What You See Is What You Get
 
Hi,

I changed my code like following your suggestion, but I had "Couldn't perform the edit because another user changed the record" executing "Database1.ApplyUpdates([Table1]);". I tried checking "Table1.UpdatesPending" in the original code and I realized that was not any update pending, I don't know why.

Thanks.
 
is your CachedUpdates property of ur dataset is true?
 
I also find that when updating an active control, it is best to disable/enable the control. The following uses a find dialog to do perform a locate - viz:
Code:
  with Database do
    begin
      try
        [b]DisableControls;[/b]
        First;
        bRecFound := Locate('Last_Name', 
                     FindDialog_Name.FindText, 
                     [loPartialKey, loCaseInsensitive]);
        if bRecFound then
          FindDialog_Patient.CloseDialog;
      finally
        [b]EnableControls;[/b]
      end;
    end;

Chris ;-)
 
Hi,

Thanks for your concern.

I don't have any control to disable. The program is very simple. It just have a form with 3 components: Database1 (TDatabase), Table1 (TTable) and ButtonBegin (TButton), and as code the following procedure:

procedure TFormPhonesFormat.ButtonBeginClick(Sender: TObject);
var
Phone: String;
FieldName: String;
begin
FieldName := 'Phone';
{
if not Database1.InTransaction then
Database1.StartTransaction;
}
Table1.First;
while not Table1.Eof do
begin
if Table1.FieldByName(FieldName).OldValue <> null then
begin
Phone := Table1.FieldByName(FieldName).OldValue;
Phone := StringReplace(Phone, '(', '', [rfReplaceAll, rfIgnoreCase]);
Phone := StringReplace(Phone, ')', '', [rfReplaceAll, rfIgnoreCase]);
Phone := StringReplace(Phone, '-', '', [rfReplaceAll, rfIgnoreCase]);
Phone := StringReplace(Phone, ' ', '', [rfReplaceAll, rfIgnoreCase]);
Table1.Edit;
Table1.FieldByName(FieldName).AsString := Phone;
{
Table1.Post; }
end;
Table1.Next;
end;
Database1.ApplyUpdates([Table1]);
{
Database1.Commit;
Table1.CommitUpdates;
}
end;

When I run it I get the error "Couldn't perform the edit because another user changed the record." at the line "Database1.ApplyUpdates([Table1]);".

This is a pretty simple phone format program. I know the answer should be very simple too, but I can't find it.

Thanks you.
 
Could you try table1.Applyupdates; instead of Database1.ApplyUpdates([Table1]);

Even your above code should work.


 
Hi edwinj,

I tried "table1.Applyupdates;" but I got the same result.

Thanks.
 
Could you let us know what database driver etc your using with the database - I assume the Database control is from the BDE.

Additionally, what settings have you used on the Table control? Using the default TTable control settings should be fine - variations in these could lead to update problems.

Chris ;-)
 
Try this ...

FIRSTLY
Create MyTable.db with DelphiDesktop whereby you have
HisNo N *
HisName A 20

Drop a TQuery (name it qryMyTable), TUpdate (name it updMyTable)and a TDataSource (name it dtsMyTable)on your form. Note that qryMyTableUpDatePropery must be updMyTable.

SECONDLY
In the SQL property of qryMyTable have
Code:
Select *
From "c:MyTable"

Set Active Property to True.
RIGHT Click on qryMyTable and include the two fields.
From a grid on your form set the DataSource Property to dtsMyTable.
Add a DBNavigator and set the DataSource Property also to dtsMyTable.

THIRDLY
RIGHT Click on updMyTable. Select ONLY HisNo on the Left.
Select ONLY HisName on the Right.
Generate the required code.

Run that and see how far you get and come back if you still don't win.

TERRY


 
Hi,

My program is ok at last. This is the code that works:

procedure TFormPhonesFormat.ButtonBeginClick(Sender: TObject);
var
Phone, FieldName, WhereFieldName, WhereFieldValue, TableName: String;
begin
FieldName := EditFieldName.Text;
WhereFieldName := EditWhereFieldName.Text;
TableName := EditTableName.Text;
if not Database1.InTransaction then
Database1.StartTransaction;
Table1.Active := False;
Table1.TableName := TableName;
Table1.Active := True;
Table1.First;
while not Table1.Eof do
begin
if Table1.FieldByName(FieldName).OldValue <> null then
begin
WhereFieldValue := Table1.FieldByName(WhereFieldName).AsString;
Phone := Table1.FieldByName(FieldName).OldValue;
Phone := StringReplace(Phone, '(', '', [rfReplaceAll, rfIgnoreCase]);
Phone := StringReplace(Phone, ')', '', [rfReplaceAll, rfIgnoreCase]);
Phone := StringReplace(Phone, '-', '', [rfReplaceAll, rfIgnoreCase]);
Phone := StringReplace(Phone, ' ', '', [rfReplaceAll, rfIgnoreCase]);
With Query1 do
begin
SQL.Clear;
SQL.Add('UPDATE ' + TableName + ' SET ' + FieldName + ' = ' + QuotedStr(Phone)
+ ' WHERE ' + WhereFieldName + ' = ' + QuotedStr(WhereFieldValue));
ExecSQL;
end;
end;
Table1.Next;
end;
try
Database1.Commit;
except
Database1.Rollback;
end;
end;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top