0

I got a problem with saving all values from 3 datasources into a SMDBGrid with another datasouce.

I got AdressID, ContactpersonID and RelationID.

Those all dont match each others.

The problem is that my SMDBGrid has another datasource then those 3. I wanna save them with one button.

Tried many ways but can't find a good result.

this is the code i use right now for my Insert button:

procedure TFRelatiebeheer.ToolButton1Click(Sender: TObject);
begin
  DRelatiebeheer.ContactpersonID.Insert;
  DRelatiebeheer.RelationID.Insert;
  DRelatiebeheer.AdressID.Insert;
end;

This is the code i use for my save button right now

  if (DRelatiebeheer.ContactpersonID.State in dsEditModes) then
    if not (DRelatiebeheer.ContactpersonID.State in [dsInsert]) then
    begin
      KJSMDBGrid1.RefreshData;
      KJPanel4.Visible := True;
    end
    else
    begin
      if (DRelatiebeheer.ContactpersonID.State IN dsEditModes) then
        DRelatiebeheer.ContactpersonID.Post;
      if (DRelatiebeheer.AdressID.State IN dsEditModes) then
        DRelatiebeheer.AdressID.Post;
    end;

Hope you have a good sight for what I am doing right now, if not please notify.

I got the problem with the datasources that need to be saved on 1 click and then be refreshed in the database and in the Grid. That means that when I insert a Contactperson there needs to be a AdressID and a RelationID coupled with it. After that the grid needs to reload all of the data.

bummi
  • 27,123
  • 14
  • 62
  • 101
Daan Kleijngeld
  • 1,375
  • 2
  • 10
  • 13
  • 3
    Is not the same question http://stackoverflow.com/questions/19837294/delphi-save-all-values-from-different-datasources-with-1-save-button ??? – Agustin Seifert Nov 11 '13 at 13:55
  • @AgustinSeifert Yeah that is true but dont know how to fix it right i am new to delphi so i dont know it that good. Hoped for that someone can make it me more easy to try it – Daan Kleijngeld Nov 11 '13 at 14:11
  • 2
    It's difficult to answer without knowing what happens/should happen. Is it a problem of the refreshing if the dataset bound to the grid? Just the missing post of RelationID? The inconsistence due to dsEditModes / dsInsert. Are there relations between the datasets which cause the need of a special sequence on posting to get AutoID values needed as reference in an other table, if so is there already a mechanism in before post? – bummi Nov 11 '13 at 15:13
  • @bummi I got the problem with the datasources that need to be saved on 1 click and then be refreshed in the database and in the Grid. That means that when I insert a Contactperson there needs te be a AdressID and a RelationID coupled with it. After that the grid needs to reload al of the data. Hope you can do anything with this information – Daan Kleijngeld Nov 11 '13 at 15:16
  • The problem here in my eyes is you already accepted an answer to a question which is not containing the needed information to answer it complete. You might consider refininig your question and add the missing informations. The comments are not place to add essential informations. This would enhance the question and make it more interessant for future users, which could lead to an upvoting of your question and an answer which would cover the complete problem. I have a close idea how the answer could look, but answering a vague question is not recommended on Stack Overflow. – bummi Nov 12 '13 at 06:51

2 Answers2

2

This code just looks somewhat random to me. What SHOULD happen there ?

  if (DRelatiebeheer.ContactpersonID.State in dsEditModes) then 
  // remember this check (1)
    if not (DRelatiebeheer.ContactpersonID.State in [dsInsert]) then
    // this check better written as "...State = dsInsert"
    begin
    // why don't you call DRelatiebeheer.ContactpersonID.Post to save chanegs ?
      KJSMDBGrid1.RefreshData;
      KJPanel4.Visible := True;
    end
    else
    begin
      if (DRelatiebeheer.ContactpersonID.State IN dsEditModes) then
      // you already checked this above (1), why check again ?
        DRelatiebeheer.ContactpersonID.Post;
      if (DRelatiebeheer.AdressID.State IN dsEditModes) then
        DRelatiebeheer.AdressID.Post;
    end;

    // so what about  DRelatiebeheer.RelationID ?

For what i may deduce, you don't have to make any complex if-ladders, you just have to literally translate your words to Delphi. You want to save three tables and then refresh the grid. Then just do it.

procedure TFRelatiebeheer.SaveButtonClick(Sender: TObject);
begin
  DRelatiebeheer.ContactpersonID.Post;
  DRelatiebeheer.RelationID.Post;
  DRelatiebeheer.AdressID.Post;

  DatabaseConnection.CommitTrans;

  KJSMDBGrid1.RefreshData;
  KJPanel4.Visible := True;  
end;

Just like you was told in your other questions.

PS. ToolButton1Click - plase, DO rename the buttons. Believe me when you have 10 buttons named Button1, Button2, ...Button10 you would never be sure what each button should do and would mix everything and make all possible program logic errors.

Community
  • 1
  • 1
Arioch 'The
  • 15,799
  • 35
  • 62
2

Focusing on the given problem Depending on the intended behavior (should it be possible posting only one or two table(s) or is it necessary to post all tables) the first thing to do would be to ensure that the tables can be posted. You coulds create a function for each table e.g. CanAdressIDBePosted:Boolean to check if required fields are already entered. The condition of the table ContactpersonID would contain additional conditions: needed fields are entered AND CanAdressIDBePosted AND CanRelationIDBePosted. You could create an Action which would be bound on your button with an OnUpdate event which could look like this:

procedure TForm1.PostActionUpdate(Sender: TObject);
begin
   TAction(Sender).Enabled := CanAdressIDBePosted and CanContactpersonIDBePosted and CanRelationIDBePosted;
   // depending on your requirements (e.g. no need to post RelationID if not entered) it also could be
   TAction(Sender).Enabled := CanAdressIDBePosted or CanContactpersonIDBePosted ;
end;



procedure TForm1.PostActionExecute(Sender: TObject);
begin
   if CanAdressIDBePosted then AdressID.Post; // ensure ID fields will be generated
   if CanRelationIDBePosted  then  RelationID.Post; // ensure ID fields will be generated
   if CanContactpersonIDBePosted then
      begin
         ContactpersonID.FieldByName('AdressID').value := AdressID.FieldByName('ID').Value;
         ContactpersonID.FieldByName('RelationID').value := RelationID.FieldByName('ID').Value;
      end;
   DateSetBoundToTheGrid.Requery;
   // furthor actions you need
end;

Function TForm1.CanAdressIDBePosted:Boolean;
begin
  // example implementation
  Result := (AdressID.State in [dsEdit,dsInsert]) and (not AdressID.FieldByName('NeededField').IsNull);
end;


Function TForm1.CanContactpersonIDBePosted:Boolean;
begin
  // example implementation
  Result := (ContactpersonID.State in [dsEdit,dsInsert]) and (not ContactpersonID.FieldByName('NeededField').IsNull)
             and CanAdressIDBePosted and CanRelationIDBePosted;
end;

An addidtional Action should be created to cancel if needed:

procedure TForm1.CancelActionExecute(Sender: TObject);
begin
    AdressID.Cancel;
    RelationID.Cancel;
    ContactpersonID.Cancel;
end;

procedure TForm1.CancelActionUpdate(Sender: TObject);
begin
   TAction(Sender).Enabled := (AdressID.State in [dsEdit,dsInsert])
                           or (RelationID.State in [dsEdit,dsInsert])
                           or (ContactpersonID.State in [dsEdit,dsInsert]);
end;

In general I am not sure if the approach you took ist the best which can be taken, since from the structure given IMHO it should be possible to assign already existing relations and adresses to new generated contactpersons, but that would be another question.

bummi
  • 27,123
  • 14
  • 62
  • 101
  • Thanks this makes more sense to me! – Daan Kleijngeld Nov 12 '13 at 10:40
  • Hi Daan, since the intention of Stack Overflow is been useful for future users, could you please refine your question here, with the information you provided in the comments and in this question: http://stackoverflow.com/questions/19926149/set-a-afterpost-event-delphi which might be obsolete. – bummi Nov 12 '13 at 10:44
  • `PostActionUpdate` this can easily consume 100% CPU if not fixed. Code to throttle idle loop is better to be presented too – Arioch 'The Nov 12 '13 at 11:01
  • This is perfectly done here: http://stackoverflow.com/questions/14577659/why-are-visible-control-assigned-tactions-affecting-the-cpu-usage-of-the-appl . I am not sure if every mentioning of ActionUpdate needs a link to this answer. – bummi Nov 12 '13 at 13:13