Delete a record based on ListBox selection

Using a button “Add Name”, I am adding a name to a listbox while also adding it to a table in a database.
This was pretty straightforward.
I also have a “Delete Name” button that currently deletes the name entry from the listbox. I ALSO want it to delete the corresponding row in the database table.

When setting up the Add Name action, I was easily able to store the name typed by the user into a variable. Then take the variable and send it to the InsertRecord method for the database.
But with deleting a record I know I have to use an sql statement, and I’m unsure of how to get the selected row from the table into a way that I can use that in a query.

Any help would be greatly appreciated.

Put the name into the rowtag for the row. Then when you want to delete the row, get the name back from the rowtag.

when you insert the record into the database, gzt back the rowid of that record you just created ( using lastrowid if using sqlite)
then store this rowid into the rowtag of the listbox current row .
then use the sql query delete from my_table where rowid_field=(the_rowid_stored_in_the_rowtag)

What is the advantage of duplicating the value that is already in the List, on the rowtag?

To get the name:

https://documentation.xojo.com/api/deprecated/listbox.html#listbox-listIndex

https://documentation.xojo.com/api/deprecated/listbox.html.Cell

To delete de data:

https://documentation.xojo.com/api/deprecated/deprecated_class_members/database.sqlexecute.html

Or better, use:

https://documentation.xojo.com/api/databases/preparedsqlstatement.html

Hi. I am in a hurry, but here is a Snippet to start with deleting a Record from a Database.

Dim ps As PreparedSQLStatement ps = Database.Prepare( "DELETE FROM TableName WHERE ID=?" ) ps.Bind( ListBox.RowTag( ListIndex ).IntegerValue, MySQLPreparedStatement.MYSQL_TYPE_INTEGER ) ps.SQLExecute

Written from my mind. May contain errors.

Sasha, in a hurry on Karfreitag ? :wink:
(same day here in Alsace / Lorraine)

4 tags mann, 4 tags :wink:

4 tage long no hurry, please. :slight_smile:

Or skip it all and go get ARGen and move on with your project. Dissecting ARGen will teach you a ton about ways to answer your question. But it would take years to learn how to do what ARGen already does out of the box. Buy it, use it. learn from it. :slight_smile:

Well, I would caution against using ARGen without having a thorough understanding of how the Xojo database classes work first. Otherwise you’re trading one mystery for another and are no better off.

ARGen should not be used as a crutch in learning the Xojo database classes.

I’d guess that depends on what your long-term goals are for suing Xojo.

[quote=433303:@Dave Kondris]suing[/quote] ???

How do I do that ?

At Listbox populating time, I add a Row, populate all the cells with a Record data and place the Primary Key (Unique ID) in the RowTag for this Row.

Then, if the user modify or delete a Row, I use the RowTag value (the unique ID) to modify or delete that Record.

Works like a charm in production since April 2013…

http://documentation.xojo.com/api/deprecated/deprecated_class_members/database.sqlexecute.html
Shows how to create a TABLE with a Primary Key (Unique ID).

hehe, USING of course )))

OK Dave, I only saw the order you write the characters (suing); usually, I also see “the other” way (using). :frowning:

'twas a typo, my bad.