Solved

Delphi inserting records into SQL table but only inserting 1st record, should insert all

Posted on 2010-09-14
7
564 Views
Last Modified: 2012-05-10
This code is supposed to take the recordset on the gridview and insert them all into another table along with other fields.  It is ONLY inserting the very 1st record...
Thanks!
// insert new record in Marketing_EMarketing_History

    with gvEMarketingRecipients do begin

      //Update the datacontroller with the new date values.

      for J := 0 to gvEMarketingRecipients.DataController.FilteredRecordCount - 1 do begin

        iRecIndex   := DataController.FilteredRecordIndex[J];

        iContactID  := gvEMarketingRecipients.DataController.GetValue(iRecIndex, gvEMarketingRecipientsID.Index);

//        sEmail      := gvEMarketingRecipients.DataController.GetValue(iRecIndex, gvEMarketingRecipientsEmail.Index);

      end;



        // update Marketing_Emarketing_History table with new contact info.

        with dmMarketing.qrySearch do begin

          Close;

          sql.Clear;

          SQL.Add('SELECT * FROM Marketing_EMarketing_History WHERE ContactID = ' + IntToStr(-1) + ' AND Issue =''' + sIssue + '''');

          Open;

          Insert;

          FieldByName('ContactID').AsInteger    := iContactID;

          FieldByName('Issue').AsString         := sIssue;

          FieldByName('Address2').AsString      := gvEMarketingRecipients.DataController.GetValue(iRecIndex, gvEMarketingRecipientsEmail.Index);

          FieldByName('ExportDate').AsString    := DateToStr(Now);

          FieldByName('ExportBy').AsString      := gasBinder.UserID;

          Post;

          end;  // with qrySearch2

        frmProgress.ProgressBar.StepBy(1);

        frmProgress.Refresh;

        end;

      end;  // for J := 0 to...

   FreeAndNil(frmProgress);

end;

Open in new window

0
Comment
Question by:Bianca
7 Comments
 
LVL 32

Expert Comment

by:ewangoya
ID: 33678166
//you have to iterate your source query
//while not dmMarketing.qrySearch.Eof do


with gvEMarketingRecipients do
  begin
    //Update the datacontroller with the new date values.
    for J := 0 to gvEMarketingRecipients.DataController.FilteredRecordCount - 1 do
    begin
      iRecIndex   := DataController.FilteredRecordIndex[J];
      iContactID  := gvEMarketingRecipients.DataController.GetValue(iRecIndex, gvEMarketingRecipientsID.Index);
      // sEmail      := gvEMarketingRecipients.DataController.GetValue(iRecIndex, gvEMarketingRecipientsEmail.Index);

      // update Marketing_Emarketing_History table with new contact info.
      with dmMarketing.qrySearch do
      begin
        Close;
        sql.Clear;
        SQL.Add('SELECT * FROM Marketing_EMarketing_History WHERE ContactID = ' + IntToStr(-1) + ' AND Issue =''' + sIssue + '''');
        Open;

        while not dmMarketing.qrySearch.Eof do
        begin
          with qrySearch2 do
          begin
            Insert;
            FieldByName('ContactID').AsInteger    := iContactID;
            FieldByName('Issue').AsString         := sIssue;
            FieldByName('Address2').AsString      := gvEMarketingRecipients.DataController.GetValue(iRecIndex, gvEMarketingRecipientsEmail.Index);
            FieldByName('ExportDate').AsString    := DateToStr(Now);
            FieldByName('ExportBy').AsString      := gasBinder.UserID;
            Post;
          end;  // with qrySearch2
          dmMarketing.qrySearch.Next;
        end;
      end;

      frmProgress.ProgressBar.StepBy(1);
      frmProgress.Refresh;
    end; // for J := 0 to...
  end; //gvEMarketingRecipients
0
 
LVL 24

Expert Comment

by:jimyX
ID: 33679057
Isn't it better if you use nested queries for the insertion as well, so it becomes (insert into Table1 fields1 (select fields2 from Table2)

JG
0
 
LVL 7

Expert Comment

by:samenglish
ID: 33679270
try this
with gvEMarketingRecipients do
begin
  for J := 0 to Pred(DataController.FilteredRecordCount) do
  begin
    iRecIndex := DataController.FilteredRecordIndex[J];
    iContactID := DataController.GetValue(iRecIndex, Index);
    sEmail := DataController.GetValue(iRecIndex, Index);

    with dmMarketing.qrySearch do 
    begin
      Close;
      SQL.Clear;
      SQL.Add('INSERT INTO Marketing_EMarketing_History
              (ContactId, Issue, Address2, ExportDate, ExportBy)
              VALUES (:ContactId, :Issue, :Address2, :ExportDate,
                      :ExportBy)');

      ParamByName('ContactID').AsInteger := iContactID;
      ParamByName('Issue').AsString := sIssue;
      ParamByName('Address2').AsString := 
                  DataController.GetValue(iRecIndex, Index);
      ParamByName('ExportDate').AsString := DateToStr(Now);
      ParamByName('ExportBy').AsString := gasBinder.UserID;
      ExecSQL;
    end;  // with qrySearch2
  end; // for
end;  // with

Open in new window

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.

 

Author Comment

by:Bianca
ID: 33684701
@samenglish

ParamByName is unrecognized and it keeps erroring out even before it goes into Insert.
0
 

Author Comment

by:Bianca
ID: 33685020
The code I have works in a different app.. I'm wondering if I'm missing a step in this particular app?
0
 
LVL 24

Accepted Solution

by:
jimyX earned 500 total points
ID: 33685186
The for-loop is not reaching the insert part, actually you closed the loop at line 8. just remove the end; at line 8 and give it a try.
0
 

Author Closing Comment

by:Bianca
ID: 33686128
That was it! Thanks!
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

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
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…

920 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

14 Experts available now in Live!

Get 1:1 Help Now