[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

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

Posted on 2010-09-14
7
Medium Priority
?
608 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 32

Expert Comment

by:Ephraim Wangoya
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

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 2000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
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…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

650 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