Solved

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

Posted on 2010-09-14
7
558 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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
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…
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, fr…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

707 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

17 Experts available now in Live!

Get 1:1 Help Now