Topic: A little help with "SQLExecute"

Dear all,i am building an autofill for my sendmail, so when i select a value on a tablegrid i can click a button and it should send an automatic email

Right now my code is stuck at:

procedure main_btemailfo_OnAfterClick (Sender: string);
var
    sFile: string;
    sEmail: string;
    sID: string;





begin
    main.btemailfo.Enabled := False;
    main.btemailfo.Caption := 'Please wait...';
    semail := SQLexecute ('SELECT clientes.email FROM clientes where main.Tabela_Main.sqlValue');
    sid := main.Tabela_Main.sqlValue;
    sFile := main.frxPDFExport1.FileName;

    if FileExists(sFile) then
        if SendMail('smtp.portugalmail.pt', 'xxxxxxxxxxxx@portugalmail.pt', 'xxxxxxxxxx1', 465 , 'xxxxxxxxx@portugalmail.pt', 'sEmail' , 'subject', 'your text', sFile) then
            ShowMessage('Message sent');

    main.btemailfo.Enabled := true;
    main.btemailfo.Caption := 'Email ';


end;

sEmail is the value i am looking for, i created an sID because if i use sqlvalue on main.Tabela_main i get the right ID for the job.
What i am trying to acomplish is retreiving an SQLexecute statement that uses this ID to fetch clientes.email data from table clientes

Can anyone help me?

Thanks

Re: A little help with "SQLExecute"

Hello,


Try this code:

procedure main_btemailfo_OnAfterClick (Sender: string);
var
    sFile: string;
    sEmail: string;
    sID: string;
begin
    main.btemailfo.Enabled := False;
    main.btemailfo.Caption := 'Please wait...';
    sid := main.Tabela_Main.sqlValue;    
    semail := SQLexecute ('SELECT clientes.email FROM clientes where id='+sid);
    
    sFile := main.frxPDFExport1.FileName;

    if FileExists(sFile) then
        if SendMail('smtp.portugalmail.pt', 'xxxxxxxxxxxx@portugalmail.pt', 'xxxxxxxxxx1', 465 , 'xxxxxxxxx@portugalmail.pt', semail, 'subject', 'your text', sFile) then
            ShowMessage('Message sent');

    main.btemailfo.Enabled := true;
    main.btemailfo.Caption := 'Email ';


end;
Dmitry.

3 (edited by VascoMorais 2015-12-30 20:34:30)

Re: A little help with "SQLExecute"

Almost...

Clientes.Email is located at table clientes

But Tablegrid1 gets its values from table assistencias , where i have clientes_ID

When i use your string i get the correct value for the ID that is located at table clientes, but i mean to get the value for clientes_ID.

So maybe... something like:

Getting the ID at ID_Clientes value

i think that will get me the job done! smile

4 (edited by VascoMorais 2015-12-31 11:58:03)

Re: A little help with "SQLExecute"

Ok, i got it to work, this is how i did it for future reference for everyone tongue

procedure main_btemailfo_OnAfterClick (Sender: string);
var
    resultid: TDataSet;
    resultemail: TDataSet;

    sFile: string;
    sEmailid: string;
    sID: string;
    sEmail: string;


begin

    sID := main.tabela_main.sqlvalue;
    SQLQuery('SELECT id_clientes FROM assistencia WHERE id="'+sid+'"', Resultid);
    semailid := resultid.fieldbyname('id_clientes').AsString;
    SQLQuery('SELECT Email FROM clientes WHERE id="'+semailid+'"', Resultemail);
    semail := resultemail.fieldbyname('email').AsString;



   main.btemailfo.Enabled := False;
   main.btemailfo.Caption := 'Please wait...';


   sFile := main.frxPDFExport1.FileName;

   if FileExists(sFile) then
       if SendMail('smtp.yourisp.com', 'yourusername@yourisp.com', 'xxxxxx', 465 , 'your name <youremail>', sEmail , 'subject', 'main text', sFile) then
           ShowMessage('Message sent');

   main.btemailfo.Enabled := true;
   main.btemailfo.Caption := 'Email ';


end;
     begin
     main.frxPDFExport1.OpenAfterExport := False;

     end.

Many thanks