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

Similar Threads

Simplify Code-Delete Query 3
Sanda Daigle Listbox Error 2
Listbox execute SQL error 2
HOW TO Simplify following code 2
HELP with ERROR Message 9
List Box Selection 3
Multi Select ListBox 11
Create Report On the Fly 1

Top