Deselect Last Selected Item in a Multi-Select Listbox

priceman31

New member
Local time
Today, 17:10
Joined
Dec 19, 2016
Messages
9
My Name is Don and I'm from Florida. I have been working with Access for a bit and have come across a problem that I haven't found a solution to.

The problem: I have a multi-select list box that each selection is being evaluated to see if it meets criteria. If it does I want the last selected entry to deselect, not all selections. I have not had any luck in trying to accomplish this. :banghead:

Any help would be greatly appreciated.

Thanks

Don
 
Multi selects are not smart enough to do this.
You'd have to record the index of the last picked entry,then build a method to undo

This is all useless since a user can just Unselect it himself.
There can't be that many items to pick, or re-pick.
 
Try.
Code:
Me[COLOR="Blue"].ListBoxName[/COLOR].Selected(Me.[COLOR="blue"]ListBoxName[/COLOR].ListCount - 1) = False

Where you would replace ListBoxName with the actual name of your listbox.
 
Thanks for the quick replies. I tried your method Steve and unfortunately it didn't work. Any other thoughts?
 
This may sound daft, but who or what is applying the criteria to de-select the item?
If it's the database, why don't you not display it in the first place if it's not a valid selection option?
 
It doesn't sound daft at all. I should better clarify what I'm trying to do. There is a query looking at criteria on the form including the list item selected. I am sending the item selected value to a textbox(invisible) each time an item is selected. A on click procedure is evaluating each item as it is clicked. The ones that don't meet the criteria I want deselected while leaving the previous selections selected.
Another way I thought might work is to send the index number of the item selected to a another textbox and deselect it using the index number, but I haven't had any luck their either.

I hope that make sense
 
Okay that sort of makes sense.
Why not populate ListBoxA with your current list, and have a command button that moves all items that meet the forms current criteria to ListBoxB . You can then process those items on another command button.
 
That's not a bad idea but would require a major form overhaul. I was hoping for something to solve the issue not require a major re-work. Is there a way to deselect an item by list index?
 
Thanks for the quick replies. I tried your method Steve and unfortunately it didn't work. Any other thoughts?

Sorry misread your post. This
Code:
Me.ListBoxName.Selected(Me.ListBoxName.ListCount - 1) = False

deselects the last item in the list, not the last item selected, but if you knew the index number of the item then

Code:
Me.ListBoxName.Selected(IndexNumber) = False

should worked. Note that the index number are zero based, e.g.,


Code:
Me.ListBoxName.Selected(0) = False

would deselect the first item.
 
I figured if I passed the selected item list index to a textbox, I could deselect the item using the value of the textbox of as part of the on click event if it didn't meet criteria. But I'm not sure how to do that.
 
To be honest I've never tried to deselect via VBA. Select I have -
Code:
lstYourListBox.Selected(i) = True
where (i) is the index I want to select. I'm guessing that setting it to false doesn't work?
 
Yes setting it to false didn't work. The attachment downloaded as a php file not as an accdb file
 
Yes setting it to false didn't work. The attachment downloaded as a php file not as an accdb file
I think that's your browser doing that. I'd try just renaming it to .accdb and if that doesn't work can you try a different browser?

What version of Access are you using? When you tried
Code:
lstYourListBox.Selected(i) = False

did it give you an error or did it just not deselected the item?
 
Access 2010 is the version I'm using. (lstYourListBox.Selected(i) = False) Only deselected only the top item in the list. If I selected a different item that should have fired the deselect code it didn't deselect it.
 
The version is 2010. I tried the above code and it only deselects the top item in the list. if I select any different item it doesn't deselect it. I was able to download the attachment using a different browser
 
The version is 2010. I tried the above code and it only deselects the top item in the list. if I select any different item it doesn't deselect it. I was able to download the attachment using a different browser
It works in Access 2013. It's kind of weird that something like this would change between 2010 and 2013. I sent the database to my sister who has Access 2010 for her to test this. I'll let you know if she confirms your experience.

If this doesn't work in Access 2010 I think you are stuck with doing this some other way. I doubt if there's another way to deselect an item programmatically.
 
It works on 2010 here. @Priceman are you setting i to the last selected value?
If not it will always select the top item as i (as an integer) would be set to 0.
 

Users who are viewing this thread

Back
Top Bottom