473,811 Members | 3,424 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Remove selected items from List Box in Microsoft Access

7 New Member
I'm using Access 2003.

I have a List Box which I populate by selecting items from a drop down Combo Box. I now want to delete from the List Box only the selected, but not necessarily sequencial items (either one at a time or several).

I found some code at this website:
http://www.599cd.com/tips/access/listbox-additem-2000/

The author suggests creating a Text Box, then first temporarily storing the text that is selected in the List Box as follows:

Expand|Select|Wrap|Line Numbers
  1. MyText = MyList.ItemData(MyList.ListIndex)
Then, by use of the following code, the text present in the Text Box is used to search the List Box, and the corresponding text is deleted:

Expand|Select|Wrap|Line Numbers
  1. Dim S As String
  2. S = MyText & ";"
  3. MyList.RowSource = Replace(MyList.RowSource, S, "")
The first bit works fine, i.e. the text selected in the List Box appears in the Text Box, but I have no luck when I choose to delete this text from the List Box.

My understanding is that this method would also only work for one line of text, and not multiple selections.

Any suggestions would be helpful.
Apr 13 '10 #1
10 25652
NeoPa
32,579 Recognized Expert Moderator MVP
This approach will only work if the ListBox is populated manually. If RowSource is a query then this will not work.

Multiple selections can be made to work, but I suggest you get the fundamentals working correctly first.

Welcome to Bytes!
Apr 13 '10 #2
ADezii
8,834 Recognized Expert Expert
If the Row Source of your List Box is a Value List, you can use the following code which does not require the use of a Text Box:
Expand|Select|Wrap|Line Numbers
  1. Dim lst As ListBox
  2. Dim intRowCtr As Integer
  3. Dim strBuild As String
  4.  
  5. 'Substitute your own List Box Name
  6. Set lst = Me![lstTest]
  7.  
  8. With lst
  9.   If .ItemsSelected.Count = 0 Then Exit Sub
  10.  
  11.   For intRowCtr = 0 To .ListCount - 1
  12.     If Not .Selected(intRowCtr) Then
  13.       strBuild = strBuild & .ItemData(intRowCtr) & ";"
  14.     End If
  15.   Next
  16.  
  17.   strBuild = Left$(strBuild, Len(strBuild) - 1)
  18.  
  19.   .RowSource = strBuild
  20. End With
Apr 13 '10 #3
errol999
7 New Member
@NeoPa
Thanks. The Listbox is populated manually - I'll work on the fundamentals first.
Apr 14 '10 #4
errol999
7 New Member
@ADezii
Thanks ADezii. I'll give that a try.
Apr 14 '10 #5
errol999
7 New Member
@errol999
Ok, this worked perfectly, until I had just one item left if in the List Box. If I try and delete the last item I get run-time error 5 for this line:

Expand|Select|Wrap|Line Numbers
  1.    strBuild = Left$(strBuild, Len(strBuild) - 1) 
which makes sense I think . I've not figured out yet how to get round this but might try putting in an If Then line to deal with this last item. If you have a suggestion that would be great. Thanks
Apr 14 '10 #6
NeoPa
32,579 Recognized Expert Moderator MVP
I would replace lines #12 through #19 with :
Expand|Select|Wrap|Line Numbers
  1.     If Not .Selected(intRowCtr) Then
  2.       strBuild = strBuild & ";" & .ItemData(intRowCtr)
  3.     End If
  4.   Next intRowCtr
  5.  
  6.   If strBuild = "" Then strBuild = ";"
  7.  
  8.   .RowSource = Mid(strBuild, 2)
Apr 14 '10 #7
errol999
7 New Member
Ok. That did the trick - thanks so much!
Apr 14 '10 #8
NeoPa
32,579 Recognized Expert Moderator MVP
Pleased to hear it Errol :)

Basically, each element was attached to a separator character when found. If none was found, then the code that stripped out the separator character would fail as it was expecting one, but we had none there. Adding that to the situation where nothing was found allowed the code to work and pick up the empty string. All fine & dandy.
Apr 15 '10 #9
errol999
7 New Member
@NeoPa
Thanks for the explanation which has helped me make more sense of it. I can see I still have a lot to learn, but have picked up some really useful tips on this question. I'll now persevere with getting the rest of the dbase functional!
Apr 15 '10 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

0
968
by: Ryan Ternier | last post by:
I'm having an issue with a listbox. I want to allow users to multi select items in a list box, click a button and have them moved somewhere else. This works fine for the the first 2 tries. However, a user also has the ability to add these items back to the listbox (with the same value and Text). The new Items get moved to the bottom of the list box.
1
3570
by: Robin Tucker | last post by:
Hi ppl, My owner draw list box controls do not "refresh" old selected items when a new selection is made. This means that as you click to make selections, the previously selected items stay highlighted along with the new ones too. It draws correctly when I minimize the window and then maximise it again, but I just don't seem to be getting a "DrawItem" event for switching of an item from Selected to NotSelected! Any ideas??? Here is...
0
7546
by: Brian Henry | last post by:
Since no one else knew how to do this I sat here all morning experimenting with this and this is what I came up with... Its an example of how to get a list of items back from a virtual mode list view in .NET 2.0 with multiple selection turned on... since the .NET documentation is EXTREAML vague on how to do this and offers no clue on what the virtual mode events do or how to use them... just thought this might help someone else out...
7
14610
by: Flavio | last post by:
Hi, I have a QListview widget that allows me to store a bunch of strings in it. This strings can be visualized, sorted, selected, etc. My Problem is that I cant find a way to get the user selected items back from it! I looked over the Qt documentation many times over but there is no method to that end. Any PyQt guru lurking around?
2
2705
by: Srimadhi | last post by:
Displaying selected items at the top of the listbox Hi, I am having two listboxes - one with ids and second with the related names. When user selects an item in one listbox, the corresponding item will be selected in the second listbox too. The problem is, when the user selects an item and it is in the middle of the list, the user is not able to know whether it is selected or not. Is there a way to display all the selected items...
2
2764
by: John | last post by:
I have a listbox that is databound when my form loads. A user can then select and option using a drop down box. When the user selects an option the corresponding items in the listbox gets selected. How can I show those selected items at the top of my listbox. The issue is when the user selects an item and it is in the middle of the list, the user is not able to know an item it is selected or not unless they scroll through the listbox.
1
1461
by: BillAtWork | last post by:
Hi, I have a listbox (in a user control) which has items add/removed via javascript (i.e. an "add item" link which uses a popup selection screen to add items and a "remove item" link which deletes any selected row(s)). There is a strange problem that when I inspect the list of items during a postback the list appears to be empty (even though I added items). I noticed that when I multiselect one or more items, only those selected items...
1
2563
by: chinkyk | last post by:
I have a form that has a list box(multiple values selected) and multiple report names(when clicked on the button, it will run the report) on the form. I have selected multiple items from the list box. Now I want to use the listbox selected items, in the reports query. All the reports have a query attached to it. So I want to use the selected items from the list box in the query, in the In clause of the query. Please help.
7
2512
by: xraive | last post by:
Currently I only get the file path. Is there way to retrieve the file name or do I have to just use the split function. Dim fDialog As Office.FileDialog Dim varFile As Variant 'Clear listbox contents. 'Me.FileList.RowSource = "" 'Set up the File Dialog.
0
9730
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10651
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10392
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10403
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9208
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6893
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5693
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4341
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3868
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.