Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 328
  • Last Modified:

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!
  • 4
  • 3
1 Solution
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) & ""
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 #.
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

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!

Featured Post

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now