Problem with database control and data transfer

Hi!

I have an application that uses 5 databases in the transfer of data from a
standalone application to an accounting package. We scan the standalone base
for an e-mail address, match it with the client (accounts) database, filter
data and then if certain criteria are met, create a new order in the
accounts package (hence the requirement for several databases, as we obtain
product and other data from the remaining databases).

My problem is that when filtering the data the program seems to get trapped
in a loop that it can't get out of. I have below given the code as it is
NOW...it's long-winded and I am sure not the best approach to the solution,
but I'm not a hard-core programmer.

I think part of my problem is caused during the filter process, as I need to
scan Table1 several times, and I make calls to "Table1.First" on several
occassions. When a table is filtered, where does Table1.First go, and what
happens when you release the filter?

ANY advice will be appreciated!

Thanks in advance,
Alistair

Code follows:    Table1 is the external database, Table2 is the accounts
package database...

  Table1.Filter := '';
  Table1.Filtered := False;
  Table1.First;
  while not Table1.Eof do
    begin
      if not (Table1['Exported'] = True) then
        begin
          Exported_Order := False;
          Customer_Rec := Table1.RecNo;
          Table1.Filter := 'Email = '+QuotedStr(Table1['Email']);
          Table1.Filtered := True;

// Scan Debitor.DB for e-mail address and get client details
          Table2.First;
          while not Table2.Eof do
            begin
              if (Table2['Alfa5'] = Table1['Email']) then
                begin
                  Edit1.Text := Table2['ClientNumber'];
                  Edit2.Text := Table2['ClientName'];
                  Edit3.Text := Table1['Email'];
                  Edit1.Update;
                  Edit2.Update;
                  Edit3.Update;    // Just to punch the data on the screen
                  begin
                    Invoice_Value := 0;
                    Table1.First;
                    while not Table1.Eof do
                      begin
                        Invoice_Value := Invoice_Value + Table1['Amount'];
                        Table1.Next;
                      end;
                    Edit4.Text := IntToStr(Round(Invoice_Value));
                    Edit4.Update;
// If subtotal > minimum then create order
                    Table1.First;
                    if Invoice_Value >= Minimum_Order_Value then
                      begin
                        while not Table1.Eof do
                          begin
                            Table1.Edit;
                            Table1['Exported'] := 'True';
                            Table1.Post;
                            Table1.Next;
                          end;
                        Exported_Order := True;
                      end;
                  end;
                end;
              Table2.Next;
            end;
        end;
      Table1.Filter := '';
      Table1.Filtered := False;
      Table1.Next;
    end;

End of code segment!