Topic: select * from ??

please i want to know how i can do a simple query to fetch a value from the database and attach it to text field as a default value for my next insert
ex: I have the last id 1  un the field text should have 2 by default.
new id = DB id+1 ??

2 (edited by CDB 2020-11-19 09:25:20)

Re: select * from ??

Hi salahnecibi,

If you have table grid then try something like this.

procedure frmMyForm_grdTableGrid_OnCellClick (Sender: TObject; ACol, ARow: Integer);
    id: integer;
   id := (frmMyForm.grdTableGrid.dbItemID); // Get ID

   id := id + 1; //increment ID

// use this if you want to fill the edit box before you do the database search
  frmMyForm.edtEditBox.Text := intToStr(id);  

 // use this if for some reason if you want to keep th eID number for use elswhere in your program
   frmMyForm.edtIDEditBox.Text := sqlExecute('SELECT id FROM TABLE WHERE id="'+intToStr(id)+'"');

If you are not using a table grid then just shout and alternative code can be provided.

On a clear disk you can seek forever

Re: select * from ??

Hi CDB and thank you.

i don't use table grid i am using the text fields to have predefined values from the db

Re: select * from ??

Form1.edit1.value := SQSExecute('select max(id) from table') + 1;

Use this queery on form onshow event or button on click

Re: select * from ??

ok how to echo it as fetched result

Re: select * from ??


procedure Form2_OnShow (Sender: TObject; Action: string);
    If Action = 'NewRecord' then Form2.edit1.Text := SqlExecute('Select Max(id) From tablename') + 1;

If the value you are pulling is the primary key(id), It's ok to show this value, but I would be very careful in allowing users to manipulate this value.