Topic: procedure that is not triggered from a form

I have 2 sections of script that do the same thing in filling out a form.  I want to make it a separate procedure and call if from the places where that code needs to run.

I am not sure how to do this - the 2 places that it runs now need place an id value in a drop down filed and text in a text text field, then they need to end and call this other "procedure"  that takes the id form the drop down and does several sql selectes and completes other fields in the form.   

I tried putting the code in a procedure called formfill

procedure Formfill (Sender: string);
var
   Ccapacity, Ctare, Cdotdate, Ccustomern, trfcontid, ContID: string;
   dt: TDateTime;
   rt,lastcust,ContSTR,Len: integer;
begin
     ContID := IntToStr(subcontainerfind.TableGrid1.dbItemID);
     Ccapacity := SQLExecute('SELECT ContainerCapacity FROM Containers WHERE id=' + ContID );
     Ctare := SQLExecute('SELECT ContainerEmpty FROM Containers WHERE id=' + ContID );
     Cdotdate := SQLExecute('SELECT DOTInspDate FROM Containers WHERE id=' + ContID );
     lastcust := SQLExecute('SELECT id_Customers, id_Containers, max(id) from FillRecords where id_Containers =' + ContID + ' AND returned = 0 group by id_Customers limit 1');
     Ccustomern := SQLExecute('select CustomerWholeName FROM Customers WHERE  id=' + IntToStr(lastcust) );
end

and then calling it from the end of the original procedure like this

Formfill();

this isn't working or I am doing it wrong.

Re: procedure that is not triggered from a form

Please, send me your project to support@drive-software.com


Thanks.

Dmitry.

3 (edited by timlitw 2015-03-20 03:43:37)

Re: procedure that is not triggered from a form

I figured it out and have it working:

procedure subcontainerfind_TableGrid1_OnCellClick (Sender: string; ACol, ARow: Integer);
var
   ContID: string;
begin
     ContID := IntToStr(subcontainerfind.TableGrid1.dbItemID);
     FillAContainer.Containerid.dbItemID := subcontainerfind.TableGrid1.dbItemID;
     subcontainerfind.CloseSearch.Click;
     fillcontform(ContID);
end;
procedure FillAContainer_conttextlookup_OnExit (Sender: string);
var
   trfcontid, ContID: string;
begin
     trfcontid := FillAContainer.conttextlookup.Text;
     ContID := SQLExecute('SELECT id FROM Containers WHERE containerid=' + trfcontid );
     FillAContainer.Containerid.dbItemID := StrToInt(ContID);

     fillcontform(ContID);
end;

and then

procedure fillcontform (ContID2: string);
var
   Ccapacity, Ctare, Cdotdate, Ccustomern: string;
   dt: TDateTime;
   rt,lastcust,ContSTR,Len: integer;
begin
     ContID2 := IntToStr(FillAContainer.Containerid.dbItemID);
     Ccapacity := SQLExecute('SELECT ContainerCapacity FROM Containers WHERE id=' + ContID2 );
     Ctare := SQLExecute('SELECT ContainerEmpty FROM Containers WHERE id=' + ContID2 );
     Cdotdate := SQLExecute('SELECT DOTInspDate FROM Containers WHERE id=' + ContID2 );
     
     lastcust := SQLExecute('SELECT id_Customers, id_Containers, max(id) from FillRecords where id_Containers =' + ContID2 + ' AND returned = 0 group by id_Customers limit 1');
     Ccustomern := SQLExecute('select CustomerWholeName FROM Customers WHERE  id=' + IntToStr(lastcust) );


     // code to ask for container to be returned if it is currently out
    if (lastcust > 0) then
      begin
        ShowMessage('Warning this container has not been returned from ' + Ccustomern + ', Please Cancel and return the container before filling it again!');
        FillAContainer.CancelContAdd.Click;
        Form1.cont_q.Text := SQLExecute('SELECT ContainerId FROM Containers WHERE id=' + ContID2 );
      end
     else;

     // code to pop up warning if dot inspection is too old
     dt := FillAContainer.filldate.DateTime - FillAContainer.dotinspDate.DateTime;
     if ((Trunc(dt/30)) >= 30) then
        ShowMessage('Warning this container has not been inspected in ' + IntToStr(Trunc(dt/30)) + ' Months, Please Select another Container, or Inspect his continaer before continuing.')
     else;
     rt :=2;

    // fill form fields
     FillAContainer.dbcapacity.Text := Ccapacity;
     FillAContainer.prodTare.Text := Ctare;
     //FillAContainer.dotinspDate.DateTime := SQLDateTimeToDateTime(Cdotdate);
     FillAContainer.filldate.DateTime := Now;
     FillAContainer.filldate.Checked := True;


end;