Solved

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

Posted on 2010-09-14
7
585 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Downloading email attachments 2 92
Making delphi communicate with a c# service 16 135
Delphi IDE crash without error message ... 7 134
Delphi Seattle StructureView color 1 30
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
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…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

738 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