Delete selected item from list box (1 Viewer)

HelpMoses76

Member
Local time
Today, 14:50
Joined
Sep 17, 2020
Messages
45
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
error 1.JPG
error 1.JPG
 

Gasman

Enthusiastic Amateur
Local time
Today, 19:50
Joined
Sep 21, 2011
Messages
14,273
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 ??
Well I have learnt something new tonight. (y)
 

theDBguy

I’m here to help
Staff member
Local time
Today, 11:50
Joined
Oct 29, 2018
Messages
21,469
Hi. Can you please do me one more favor? Go to the Ribbon > Create > Query and go to SQL View and paste the following and then try to Run it. Does it run? If you get an error message, please post a screenshot of it here. Thanks.

Code:
DELETE FROM BR-DETAILS WHERE ID=60
 

HelpMoses76

Member
Local time
Today, 14:50
Joined
Sep 17, 2020
Messages
45
Hi. Can you please do me one more favor? Go to the Ribbon > Create > Query and go to SQL View and paste the following and then try to Run it. Does it run? If you get an error message, please post a screenshot of it here. Thanks.

Code:
DELETE FROM BR-DETAILS WHERE ID=60

1) It gave me a syntax error first

error2.JPG


2) I put a bracket around the table name and ran it from SQL view and it worked...confirmed by opening the table as well
3) However when I put it back on the button on the form and put the bracket there ..no error message comes up anymore but it does not delete from the table either.

Any ideas?
 

Isaac

Lifelong Learner
Local time
Today, 11:50
Joined
Mar 14, 2017
Messages
8,777
Been away for a little while, but just to clarify and add.
Did you mean the total number of columns in the lisbox ? If yes it is 22
No, what I meant is, the column that contains the ID, which column number is that in the Listbox properties?
For example, if your listbox rowsource is:
Code:
Select col1, IDCOLUMN, col2 from table
...then you could say the ID column is the "second one". And (by the way), when referring to it in code, it would have an Index of 1, since listbox columns begin numbering at 0 when it comes to their indexing, so the second column is column(1)
I couldn't necessarily tell if ID was the "first" or "second" column in your listbox, because, it might have had the same rowsource (as I just put above), but, you could have the Listbox's ColumnWidths property set to 0";1";1" (which would make the first column invisible, at runtime display).

The reason that I asked all of this is because, while a lot of people write code like this:
Code:
VariableName=Me.ListBoxName
or
Code:
strSQL = "select col1 from table where ID=" & Me.ListBoxName
...Both of those two bits of code, when they refer to Me.ListBoxName, rely on the "Default Value" of the Listbox, which will be the value in the Bound Column. (You can choose ANY of the listbox's columns to be the Bound Column, in the properties - 1st, 2nd, 3rd, so on).

That means, when writing code, you always have to remember which column happens to be the "Bound" column, and it also means, as your project grows and sometime you write code that needs to refer to a DIFFERENT column - which is not the Bound column, you can't use that method anyway.

This is why I often steer people away from relying overly on default properties, especially in this particular context, and simply using another technique that will ALWAYS work--whether you're referring to the bound column or not:
Code:
Me.ListboxName.Column(0) 'to get the value in the FIRST column
Me.ListboxName.Column(1) 'to get the value in the SECOND column

In order to suggest that you use that method, we would need to know which column (first, second, etc) of the Listbox the ID was in, hence that line of questioning.

Looks like you have plenty of help at this point, I just wanted to clarify where I was going, and more importantly, why.
 

theDBguy

I’m here to help
Staff member
Local time
Today, 11:50
Joined
Oct 29, 2018
Messages
21,469
1) It gave me a syntax error first

View attachment 85231

2) I put a bracket around the table name and ran it from SQL view and it worked...confirmed by opening the table as well
3) However when I put it back on the button on the form and put the bracket there ..no error message comes up anymore but it does not delete from the table either.

Any ideas?
Hi. Glad to hear you're making good progress. Since we cannot see what you're actually doing, can you please show us (screenshots) the latest changes you've made to the code? Specifically, how did you add the square brackets to the code? If you manually deleted ID=60, I hope you selected a different ID from the Listbox before trying to run the code again, correct?
 

HelpMoses76

Member
Local time
Today, 14:50
Joined
Sep 17, 2020
Messages
45
Been away for a little while, but just to clarify and add.

No, what I meant is, the column that contains the ID, which column number is that in the Listbox properties?
For example, if your listbox rowsource is:
Code:
Select col1, IDCOLUMN, col2 from table
...then you could say the ID column is the "second one". And (by the way), when referring to it in code, it would have an Index of 1, since listbox columns begin numbering at 0 when it comes to their indexing, so the second column is column(1)
I couldn't necessarily tell if ID was the "first" or "second" column in your listbox, because, it might have had the same rowsource (as I just put above), but, you could have the Listbox's ColumnWidths property set to 0";1";1" (which would make the first column invisible, at runtime display).

The reason that I asked all of this is because, while a lot of people write code like this:
Code:
VariableName=Me.ListBoxName
or
Code:
strSQL = "select col1 from table where ID=" & Me.ListBoxName
...Both of those two bits of code, when they refer to Me.ListBoxName, rely on the "Default Value" of the Listbox, which will be the value in the Bound Column. (You can choose ANY of the listbox's columns to be the Bound Column, in the properties - 1st, 2nd, 3rd, so on).

That means, when writing code, you always have to remember which column happens to be the "Bound" column, and it also means, as your project grows and sometime you write code that needs to refer to a DIFFERENT column - which is not the Bound column, you can't use that method anyway.

This is why I often steer people away from relying overly on default properties, especially in this particular context, and simply using another technique that will ALWAYS work--whether you're referring to the bound column or not:
Code:
Me.ListboxName.Column(0) 'to get the value in the FIRST column
Me.ListboxName.Column(1) 'to get the value in the SECOND column

In order to suggest that you use that method, we would need to know which column (first, second, etc) of the Listbox the ID was in, hence that line of questioning.

Looks like you have plenty of help at this point, I just wanted to clarify where I was going, and more importantly, why.


That was it . The bound column was the issue . I changed it to 22 and that took care of the issue.
Also the bracket looks like it is a must.

Many thanks once again . I do not know how I would learn anything without this forum.
 

theDBguy

I’m here to help
Staff member
Local time
Today, 11:50
Joined
Oct 29, 2018
Messages
21,469
That was it . The bound column was the issue . I changed it to 22 and that took care of the issue.
Hi. Congratulations! Glad to hear you got it sorted out. Are you saying ID=60 didn't register to you earlier that it was the wrong ID? Just curious...
 

HelpMoses76

Member
Local time
Today, 14:50
Joined
Sep 17, 2020
Messages
45
Hi. Glad to hear you're making good progress. Since we cannot see what you're actually doing, can you please show us (screenshots) the latest changes you've made to the code? Specifically, how did you add the square brackets to the code? If you manually deleted ID=60, I hope you selected a different ID from the Listbox before trying to run the code again, correct?



This is the latest version of the SQL string : strSQL = "DELETE FROM [BR-DETAILS] WHERE ID=" & Me.List248

Thank you once again for your help "theDBGuy" "Isaac" "Gassman"

Much much appreciated.
 

theDBguy

I’m here to help
Staff member
Local time
Today, 11:50
Joined
Oct 29, 2018
Messages
21,469
This is the latest version of the SQL string : strSQL = "DELETE FROM [BR-DETAILS] WHERE ID=" & Me.List248

Thank you once again for your help "theDBGuy" "Isaac" "Gassman"

Much much appreciated.
Hi. You're welcome. We were all happy to assist. Good luck with your project.
 

Isaac

Lifelong Learner
Local time
Today, 11:50
Joined
Mar 14, 2017
Messages
8,777
That was it . The bound column was the issue . I changed it to 22 and that took care of the issue.
Also the bracket looks like it is a must.

Many thanks once again . I do not know how I would learn anything without this forum.
Very glad to hear you got it working!
 

HelpMoses76

Member
Local time
Today, 14:50
Joined
Sep 17, 2020
Messages
45
re you saying ID=60 didn't register to you earlier
Hi. Congratulations! Glad to hear you got it sorted out. Are you saying ID=60 didn't register to you earlier that it was the wrong ID? Just curious...


Curiously it was showing the right IDs in the message box no doubt about that . It is after I changed the bound column it worked !
I have a lot to learn !
 

theDBguy

I’m here to help
Staff member
Local time
Today, 11:50
Joined
Oct 29, 2018
Messages
21,469
Curiously it was showing the right IDs in the message box no doubt about that . It is after I changed the bound column it worked !
I have a lot to learn !
Okay. We'll just chalk it up to experience. Cheers!
 

Gasman

Enthusiastic Amateur
Local time
Today, 19:50
Joined
Sep 21, 2011
Messages
14,273
Curiously it was showing the right IDs in the message box no doubt about that . It is after I changed the bound column it worked !
I have a lot to learn !
FWIW I *always* make my bound column the first field and hidden, as up to now they have always been autonumber fields.
 

Users who are viewing this thread

Top Bottom