1 (edited by CDB 2019-12-16 09:42:15)

Topic: How to delete a database item from a combobox

Hello,

I am having a problem  working out how to delete an item in a database from an entry in a combobox.

What I'm trying to do is to select an item from a combobox and then click on a button to delete the entry out of both the database and the combobox itself.

The code I currently have is:

procedure frmMain_btnDeleteSupplier_OnClick (Sender: TObject; var Cancel: boolean);
var
    item: integer;
    idx:string;

begin
  
  idx := frmMain.ComboBox2.text;  //get the entry selected from the combobox

{  frmMain.ComboBox2.dbDeleteRecord(16);}  // I feel I should be able to use this somehow
  
  showmessage('Item ' + idx); // shows a message box just for debugging purposes. At this point the correct item shows in the message

  
 frmMain.ComboBox2.dbSQLExecute('DELETE FROM supplier WHERE sup_name =' + idx); //Delete the selected item from the database

 frmMain.ComboBox2.dbUpdate; //Update the database and possibly the combobox
end;

One of my problems is, the above SQL produces and error message 'no such column: item text that I've selected

If someone could point me in the right direction I'd be very grateful.

Do I need to somehow read in the row ID from the DB and then use 'dbDeleteRecord(rowID)'? If so I'm not sure how to do that in MVD.

I am using the trial version 6.1.

On a clear disk you can seek forever

Re: How to delete a database item from a combobox

Hello.


procedure Form1_Button1_OnClick (Sender: TObject; var Cancel: boolean);
begin
    if Form1.ComboBox1.dbItemID<>-1 then
    begin
        SQLExecute('DELETE FROM supplier WHERE id='+Form1.ComboBox1.sqlValue);
        Form1.ComboBox1.dbUpdate;
    end;
end;
Dmitry.

3 (edited by CDB 2019-12-16 11:05:30)

Re: How to delete a database item from a combobox

Thank you very much Dmitry, that worked a treat.

On a clear disk you can seek forever