Jump to content
PenelopeSkye

DB field update using "DataSet.FieldByName" is not working

Recommended Posts

I am trying to update an app.  I type 871 into an edit box and close the app.  When that happens I check the database.  The cell associated with the DBEdit23 field is 871, but the IsMetallic field is not updated.

 

What am I doing wrong?

 

Please let me know what other info you need.  Thanks!

 

procedure TfDesignMaster.DBEditPMSExit(Sender: TObject);

begin

    if DBEdit23.Field.AsString ='871' then
    begin

    dm.dsVendorProd.DataSet.Edit;
    dm.dsVendorProd.DataSet.FieldByName('IsMetallic').Value:='Y';
    dm.dsVendorProd.DataSet.Post;

    end;

 end;

Share this post


Link to post

Exit is not triggered for a control when the application is closed as focus never moves off it. You could move focus somewhere in the form's OnClose to trigger it.

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  Form1.SetFocusedControl(Form1.Button1);
end;

 

Edited by Brian Evans

Share this post


Link to post

Hi Brian!  After seeing your post (thank you!) just to test the basic code I updated the text in the edit box, manually moved focus to another tab, then exited.

 

Shouldn't that have updated the database?  Sorry if I am being obtuse!

Share this post


Link to post

If that is hooked up to the edit in question. I have doubts since DBEditPMSExit wouldn't be the default event name for a control called DBEdit23.

Share this post


Link to post

Erm... I created that name.  

 

I modeled it on another procedure called procedure DBEdit19Exit(Sender: TObject).  

 

I just assumed they made that up as well.

 

Thanks Brian!

Edited by PenelopeSkye

Share this post


Link to post

I changed my code to procedure TfDesignMaster.DBEdit23Exit(Sender: TObject) but it didn't work.  FYI that I have also tried FieldByName('IsMetallic').AsString

 

This is the code for a similarly named procedure.  Does my procedure not work because I am trying to write to a db and they are not?  Apologies for my noobness. 

 

procedure TfDesignMaster.DBEdit19Exit(Sender: TObject);
var
stuffCustom: string;
begin
  if (dm.tb_design_master.state=dsEdit) or (dm.tb_design_master.state=dsInsert) then
  begin
    //MessageDlg( 'From DBEdit19 designmaster IS in dsEdit or dsInsert', mtInformation, [mbOK], 0);
    if Length(DBEdit19.Field.AsString) > 0 then
    begin
      stuffCustom:='Custom';
      wwDBLookupCombo2.Value:=stuffCustom;
    end;
  end else
    //MessageDlg('From DBEdit19 designmaster IS NOT in dsEdit or dsInsert', mtInformation, [mbOK], 0);
  end;

Edited by PenelopeSkye

Share this post


Link to post

OT

if (dm.tb_design_master.state=dsEdit) or (dm.tb_design_master.state=dsInsert) then

I prefer to use

if (dm.tb_design_master.state in [dsEdit, dsInsert) then

In such cases

if Length(DBEdit19.Field.AsString) > 0 then

I use

if Length(Trim(DBEdit19.Field.AsString)) > 0 then

 

Share this post


Link to post

To everybody who answered this thread: Apologies!  I had no idea that you had to highlight the object, go to events, and add the procedure name!!

Edited by PenelopeSkye

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×