delete ListBox Row

C

Chris

How would I select a listbox row and Delete that row from a list box? (Using
a command button)
 
J

John Nurick

Hi Chris,

This depends on what the rowsource of the listbox is.

If it's a table or query, you need to write VBA code that deletes the
corresponding record from the underlying table.

If it's a value list, you need to write code that deletes the
corresponding entry from the string that makes up the list.
 
C

Chris

It's based on a query.

I'm sure I've done it with a "DoCmd" command (linking with the ID id the
listBox)

However I can't remember how to do that and have tried using
the recordset to delete the record. Again I just can't figure it out

Please Help me before New Year
 
J

John Nurick

Something like this air code should do it:

Dim strSQL As String

strSQL = "DELETE * FROM MyTable WHERE KeyField=""" _
& Me.lbXXX.Value & """;"
CurrentDB.Execute strSQL
Me.lbXXX.Requery

You'll need to replace
MyTable with the name of the table
KeyField with the name of the field in the table corresponding
to the bound column in the listbox
lbXXX with the name of the listbox (*not* the name of the field
the listbox is bound to).


It's based on a query.

I'm sure I've done it with a "DoCmd" command (linking with the ID id the
listBox)

However I can't remember how to do that and have tried using
the recordset to delete the record. Again I just can't figure it out

Please Help me before New Year
 
Top