Access 2007-2019
Access 2000-2003
Access Seminars
TechHelp Support
Tips & Tricks
Access Forum
Course Index
Topic Glossary
Insider Circle
 
Home   Courses   Seminars   Templates   Help   TechHelp   Forums   Contact   Join   Order   Logon  
 
Tips & Tricks

New Tips Added Weekly!
Click here to get on our Mailing List

   
 

Listbox Add/Remove Item AC2000

How to Add and Remove an Item from a Listbox in Access 97 or 2000

Q: How can I add and remove items from a Listbox in Access 2000?

A: In my Access 321 class, I spend a great deal of time teaching you how to work with unbound Listboxes to store temporary lists of information. I show you the AddItem and RemoveItem methods of the Listbox object. The problem is that these methods weren't added until Access XP (2002). So if you have Access 97 or 2000, you can't use them. Listbox controls existed back then, but you had to directly access the RowSource property in order to do anything.

The RowSource property is essentially a list of the items in the box separated by semicolons. It's not hard to work with, but you just have to understand how to deal with text strings.

So, in this lesson, I'll show you how to essentially create your own AddItem and RemoveItem code. First, we'll start out creating a simple unbound form with an unbound Listbox (MyList), an unbound text box (MyText) and two buttons (AddButton, RemoveButton). Make sure the Row Source Type property of the listbox is set to Value List.

Listbox

We want to type some data into MyText and then click on the Add button to add that information to our list box. So here's the code we need to put in the AddButton build event:

    MyList.RowSource = MyList.RowSource & MyText & ";"

Now go ahead and save it. Type a name into the text box, and then click Add. It should be added to the listbox.

That was the easy part. Basically we're just adding text string values on to the end of the RowSource of the listbox. Now to remove items, it's a little tougher. Essentially if the word "Joe" is typed in to the box and someone clicks Remove, we need to find "Joe;" in the RowSource and remove it. Here's the code I used:

  Dim S As String
  S = MyText & ";"
  MyList.RowSource = Replace(MyList.RowSource, S, "")

I basically create my own string equal to whatever was typed into the MyText textbox plus a semicolon. Then, I'll use the Replace function to change that string into an empty string, effectively removing it from the list.

If you want to remove the item from the box that the user clicks on instead of having to make them type it in the text box, then you just need to know how to set the text box equal to whatever the user clicks on. You can do that with an OnClick event for the Listbox:

  MyText = MyList.ItemData(MyList.ListIndex)

This will now set MyText equal to whatever value is currently clicked on in the list box. Then if the user clicks on Remove, the value is set for the remove operation.

So there you have it. That's how you would add and remove items from a listbox in Access 97 or 2000 without the AddItem and RemoveItem methods. This will allow you to now follow all of the examples in Access 321.

 


By Richard Rost   Richard Rost on Twitter Richard Rost on LinkedIn Email Richard Rost
Click here to sign up for more FREE tips

 

You may want to read these articles from the 599CD News:

 

Learn
 
Access - index
Excel - index
Word - index
Windows - index
PowerPoint - index
Photoshop - index
Visual Basic - index
ASP - index
Seminars
More...
Customers
 
Account Login
Online Theater
Downloads
Lost Password
Memberships
Student Databases
Change Email
Info
 
Latest News
New Releases
User Forums
Topic Glossary
Tips & Tricks
Search The Site
Code Vault
Collapse Menus
Help
 
Customer Support
Web Site Tour
FAQs
TechHelp
Consulting Services
About
 
Background
Testimonials
Jobs
Affiliate Program
Richard Rost
Free Lessons
Mailing List
Order
 
Video Tutorials
Handbooks
Memberships
Learning Connection
Idiot's Guide to Excel
Volume Discounts
Payment Info
Shipping
Terms of Sale
Contact
 
Contact Info
Support Policy
Email Richard
Mailing Address
Phone Number
Fax Number
Course Survey
Blog RSS Feed    Twitter

YouTube Channel    LinkedIn