Deleting records, using parameters from a ListBox

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have a table (tblControlATB) with three data elements: ID, Type,
Description. On a form (tblATBDelete), I have a ListBox which displays these
same data elements and is populated from data pulled from this table. I want
to be able to select one of the line-items in the ListBox, click a button
(executing a command in VBA), which then deletes the "selected" record. Any
ideas on how to do this,
I've been experimenting with it for weeks now. Any help is greatly
appreciated!

Thanks,
David
 
David,

Assuming the listbox is called List1 and returns the value of field ID,
the code behind the button would look something like:

strSQL = "DELETE * FROM tblControlATB "
strSQL = strSQL & "WHERE ID = " & Me.List1
CurrentDb.Execute strSQL
Me.List1.Requery

where you muct change my assumed names to the actual ones.
Note: the code above assumes that field ID is numeric; in case it is
text, then the second line needs to be changed to:

strSQL = strSQL & "WHERE ID = '" & Me.List1 & "'"

By the way, although it won't affect the functionality of your project,
having a form's name start with tbl can be very confusing not only to
another developer, but even to yourself if you revisit it after some
time! A good practice is to precede each object's name with an
abbreviation of its type, like tbl, frm, qry etc.

HTH,
Nikos
 
Back
Top