Code to delete item selected in list box from table

I have some code that should take a selected item from a list box and delete it from a table. Here is the code:
Private Sub btnDeleteStaff_Click()

Dim frm As Form
Dim ctl As Control
Dim db As DAO.Database
Dim strsql As String

Dim i As Variant
Set frm = Forms("frm_Staff")
Set ctl = frm![lstExistingStaff]

Set db = CurrentDb

For Each i In ctl.ItemsSelected

 strsql = "DELETE FROM Staff WHERE " & _
                 "[Staffid] = " & ctl.Column(2, i) & " AND [Lname] = '" & ctl.Column(3, i) & "' AND [fName] = '" & ctl.Column(4, i) & "' And [StartDate] = " & ctl.Column(5, i) & " And [EndDate] = " & ctl.Column(6, i) & "AND [Compid] = " & ctl.Column(7, i) & ""

     db.Execute strsql, dbFailOnError
Next i

CurrentDb.Execute strsql, dbFailOnError

End Sub

Open in new window

When I try to run it, it does nothing. I don't even get an error code.

I know there is a way to check to see if the code is running at all, but I can't remember how to do that. Also, this is basically the same code I use in another form and it worked fine there. Of course, I changed out the form name, table name, and control names, but everything else is the same.

Thank you, in advance!
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Rey Obrero (Capricorn1)Commented:
you need a space between
" & ctl.Column(6, i) & "AND [Compid] = " & ctl.Column(7, i) & ""

 strsql = "DELETE FROM Staff WHERE " & _
                 "[Staffid] = " & ctl.Column(2, i) & " AND [Lname] = '" & ctl.Column(3, i) & "' AND [fName] = '" & ctl.Column(4, i) & "' And [StartDate] = " & ctl.Column(5, i) & " And [EndDate] = " & ctl.Column(6, i) & " AND [Compid] = " & ctl.Column(7, i) & ""

and perhaps wrapping the date values with #

 strsql = "DELETE FROM Staff WHERE " & _
                 "[Staffid] = " & ctl.Column(2, i) & " AND [Lname] = '" & ctl.Column(3, i) & "' AND [fName] = '" & ctl.Column(4, i) & "' And [StartDate] = #" & ctl.Column(5, i) & "# And [EndDate] = #" & ctl.Column(6, i) & "# AND [Compid] = " & ctl.Column(7, i) & ""

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
MeginAuthor Commented:
That made something happen!

But I think I have another problem. Can you verify that this is true?

The code is deleting records from a table. Those records, at least the primary key for those records, is used in other relationships in the database.  I think that is keeping me from deleting the records with this code.

The error message I am getting is basically telling me this, but I want to verify that this is how it works and I am not getting this message because of something in the code that is wrong.

Also, can you tell me why the # worked in that situation? Is that specifically related to dates?
Rey Obrero (Capricorn1)Commented:
as i mentioned above, date values needs to be wrapped in #.
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

MeginAuthor Commented:
Sorry. I didn't read that closely enough. I went straight to the code and didn't look back.

Thank you for the answer!
MeginAuthor Commented:
I've requested that this question be closed as follows:

Accepted answer: 0 points for Megin's comment #a40261584

for the following reason:

This solution made my code work!
Scott McDaniel (Microsoft Access MVP - EE MVE )Infotrakker SoftwareCommented:
I know there is a way to check to see if the code is running at all, but I can't remember how to do that.
Cap has your syntax straight, I believe, but to be sure that your code is running, open the form in Design view and select the button named btnDeleteStaff. In the Properties dialog, make sure the Click event shows [Event Procedure].

You can place a breakpoint in your code to verify if it's running, and to check it during a debug session. To do that, place your cursor at the beginning of your code and then press F9. This should set a Breakpoint (and color the line red). Now when you run that code (i.e. click the button), Access will throw you into the VBA Editor in Debug mode, and you can use the Debug menu to step through your code line-by-line.
Rey Obrero (Capricorn1)Commented:
are you sure you want to accept your own comment #a40261584 as the solution?
MeginAuthor Commented:
I had picked the wrong answer as a solution last time. Thank you for catching that!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.