Solved

VBA to delete selected item in list box

Posted on 2014-07-22
4
969 Views
Last Modified: 2014-07-22
I want to delete items selected from a list box in my form.
I have a piece of code that worked well in another form and tried to make it work here, but it isn't working.
I get the following error message "You can't assign a value to this object"

Am I missing something really simple?

Private Sub btnDelete_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_SubTaskOrders")
Set ctl = frm![lstSubTaskOrders]


Set db = CurrentDb




For Each i In ctl.ItemsSelected
     strsql = "DELETE FROM SubTaskOrders WHERE"
                [STOid] = " & ctl.Column(0, i)" & " AND [STONo] = " & ctl.Column(1, i) & " AND [STOName] = " & ctl.Column(2, i) & " And [Toid] = " & ctl.Column(3, i)
     db.Execute strsql, dbFailOnError
Next i
ctl.Requery



CurrentDb.Execute strsql, dbFailOnError



End Sub

Open in new window


Thank you!
0
Comment
Question by:Megin
  • 2
4 Comments
 
LVL 39

Expert Comment

by:als315
ID: 40212398
Try this
 strsql = "DELETE FROM SubTaskOrders WHERE " & _
                "[STOid] = " & ctl.Column(0, i) & " AND [STONo] = " & ctl.Column(1, i) & " AND [STOName] = " & ctl.Column(2, i) & " And [Toid] = " & ctl.Column(3, i)
 

Open in new window

If any of parameters is string, double quotes before and after it:
chr(34) &  ctl.Column(2, i) & chr(34)
0
 

Author Comment

by:Megin
ID: 40212503
Now I am getting a different error:

Syntax error (missing operator) in query expression '[STOid] = 9 AND [STONo] = 2 AND [STOName] = Tech Coordination AND [TOid] = 1 AND [OrderNumber]=2'

It appears to be pulling information from the table in this error.

Also, I forgot that I had one more field I needed to delete information from in this table (OrderNumber), so I added it to the list box and to the code. So this is what I have:

Private Sub btnDelete_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_SubTaskOrders")
Set ctl = frm![lstSubTaskOrders]


Set db = CurrentDb




For Each i In ctl.ItemsSelected

 strsql = "DELETE FROM SubTaskOrders WHERE " & _
                "[STOid] = " & ctl.Column(0, i) & " AND [STONo] = " & ctl.Column(1, i) & " AND [STOName] = " & ctl.Column(2, i) & " And [Toid] = " & ctl.Column(3, i) & " And [OrderNumber] = " & ctl.Column(4, i)
 

     db.Execute strsql, dbFailOnError
Next i
ctl.Requery



CurrentDb.Execute strsql, dbFailOnError



End Sub

Open in new window

0
 
LVL 120

Accepted Solution

by:
Rey Obrero (Capricorn1) earned 500 total points
ID: 40212514
try changing your strsql with this


strsql = "DELETE FROM SubTaskOrders WHERE " & _
                "[STOid] = " & ctl.Column(0, i) & " AND [STONo] = " & ctl.Column(1, i) & " AND [STOName] = '" & ctl.Column(2, i) & "' And [Toid] = " & ctl.Column(3, i) & " And [OrderNumber] = " & ctl.Column(4, i)
0
 

Author Closing Comment

by:Megin
ID: 40212736
That worked.

Thank you!
0

Featured Post

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It’s been over a month into 2017, and there is already a sophisticated Gmail phishing email making it rounds. New techniques and tactics, have given hackers a way to authentically impersonate your contacts.How it Works The attack works by targeti…
As tax season makes its return, so does the increase in cyber crime and tax refund phishing that comes with it
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question