Delete selected item from list box (1 Viewer)

HelpMoses76

Member
Local time
Today, 10:20
Joined
Sep 17, 2020
Messages
45
del.JPG


I am trying to write a piece of code that works on button click and deletes a selected row from the Table . Not sure how I can reference the entire row. ID is the primary key in the table . Any ideas ?
 

Isaac

Lifelong Learner
Local time
Today, 07:20
Joined
Mar 14, 2017
Messages
8,738
Sorry, I meant the column number in the listbox properties.
 

theDBguy

I’m here to help
Staff member
Local time
Today, 07:20
Joined
Oct 29, 2018
Messages
21,357
Okay, for a non-multi select Listbox, you might be able to do something like this:
Code:
Dim strSQL As String
If MsgBox("Are you sure you want to delete this item?",vbQuestion+vbYesNo,"Confirm!")=vbYes Then
    strSQL = "DELETE FROM TableName WHERE ID=" & Me.ListboxName
    CurrentDb.Execute strSQL, dbFailOnError
End If
Hope that helps...
 

HelpMoses76

Member
Local time
Today, 10:20
Joined
Sep 17, 2020
Messages
45
Okay, for a non-multi select Listbox, you might be able to do something like this:
Code:
Dim strSQL As String
If MsgBox("Are you sure you want to delete this item?",vbQuestion+vbYesNo,"Confirm!")=vbYes Then
    strSQL = "DELETE FROM TableName WHERE ID=" & Me.ListboxName
    CurrentDb.Execute strSQL, dbFailOnError
End If
Hope that helps...

Thank you so much for taking the time to reply . However it gives me an error on the last statement

CurrentDb.Execute strSQL, dbFailOnError . I wonder if I have to do some kind of variable declaration.
 

theDBguy

I’m here to help
Staff member
Local time
Today, 07:20
Joined
Oct 29, 2018
Messages
21,357
Thank you so much for taking the time to reply . However it gives me an error on the last statement

CurrentDb.Execute strSQL, dbFailOnError . I wonder if I have to do some kind of variable declaration.
What was the error message?
 

Gasman

Enthusiastic Amateur
Local time
Today, 14:20
Joined
Sep 21, 2011
Messages
14,043
You did change the table and listbox names to match yours, didn't you :unsure:
 

theDBguy

I’m here to help
Staff member
Local time
Today, 07:20
Joined
Oct 29, 2018
Messages
21,357
You did change the table and listbox names to match yours, didn't you :unsure:
And the Field's Name too please (in case it's called other than just "ID").
 

theDBguy

I’m here to help
Staff member
Local time
Today, 07:20
Joined
Oct 29, 2018
Messages
21,357
error 3131 - syntax error in from clause
Also attached a screen shot.
Thank you so much for helping View attachment 85228
Can you please add the following line before the CurrentDb.Execute one and post a screenshot of the message you get? Thanks.
Code:
MsgBox strSQL
 

Gasman

Enthusiastic Amateur
Local time
Today, 14:20
Joined
Sep 21, 2011
Messages
14,043
I might say "need" is a strong word. :)
Well I was not sure it was mandatory as you had not used it, but I always have not knowing any better? , so I had to check with that link?

Edit: In fact that link says [table.*] is optional ??
 

Users who are viewing this thread

Top Bottom