Full Version: Delete Selected Record in Listbox
UtterAccess Discussion Forums > Microsoft® Access > Access Forms
rjAccDB
Hello Fellow UA,

Can someone please help me to achieve this.

I have a form with listbox to display a record from my table, I want to put a delete button to delete selected record when I click the button.

Please need help,
rj
rjAccDB
Please anybody there to please help me out...

I would be thankful please.
Larry Larsen
Hi

The record in the listbox or is the record some where else..!!
thumbup.gif
rjAccDB
Hi Larry,

The record(s) that was displayed in the listbox were from table from the current database.
Larry Larsen
Hi
Some thing like this using the double click event of the listbox or behind a command button:
CODE
Private Sub lstOne_DblClick(Cancel As Integer)
    CurrentDb.Execute "DELETE FROM table_name WHERE {condition}"
    Me.lstOne.Requery
End Sub


You may want to incorporate some message prompt before deleting the item..

Also I'm a big fan of flagging item if it's active or not.. so rather than remove/deleting I use a flag (Y/N) and simple change the items status.. (you never know you may want that item back in the list some day..)

HTH's
thumbup.gif
rjAccDB
Hi Larry,

Many thanks on the solution, but got sorted out earlier..

You are right, flagging is I think good practise. Could you please give me an idea how to flag that record and don't show anymore in my listbox, please.

Thank you,
rj
Larry Larsen
Hi
My listbox sql:
CODE
SELECT tblReports.ReportID, tblReports.ReportName, tblReports.ReportFlag
FROM tblReports
WHERE (((tblReports.ReportFlag)=False));


And the code I run in the double click event simple updates the flag field in the table:

CurrentDb.Execute "UPDATE tblReports SET ReportFlag = True Where ReportID =" & Me.lstOne
Me.lstOne.Requery

My listbox returns those records who#s flag is "False", to exclude it from the listbox I change/update the flag to "True"

HTH's
thumbup.gif
rjAccDB
Thanks Larry,

I will check this out tom in the office.

Many thanks again for your time.

rj
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.