?
Solved

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

Posted on 2010-09-14
7
Medium Priority
?
596 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses
Course of the Month11 days, 18 hours left to enroll

752 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