Delete selected only....

M

mo

A listbox on a form has the following code which is meant to delete only the
seelcted items from the underlying table, but instead deletes all the
records in the listbox (anf the table). Can anyone see what I'm doing wrong?

Thanks for any help.

Dim db As DAO.Database
Dim strSQL As String
Dim strWhere As String
Dim varSelected As Variant

For Each varItem In Me.lst_selected.ItemsSelected

strWhere = "HospNum = " & "'" & Me.lst_selected.ItemData(varSelected) &
"'"

Next varItem

strSQL = "DELETE * FROM tbl_Lists "
strSQL = strSQL & "WHERE " & strWhere

'Debug.Print strSQL

Set db = CurrentDb()

db.Execute strSQL

Me.list_main.Requery

Set db = Nothing
 
R

Rod Scoullar

Try this,

For Each varPtr In lstSelected.ItemsSelected
strList = strList & strLink & "'" & lstSelected.ItemData(varPtr) & "'"
strLink=","
Next
strSQL = "Delete * From tblList Where HosNum In (" & strList & ")"

I haven't declared the variables

Rod Scoullar
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top