Connecting Tech Pros Worldwide Help | Site Map

Listbox.clear

Newbie
 
Join Date: Oct 2009
Posts: 9
#1: 3 Weeks Ago
Hello All,

i have a listbox control named lstDisplay. I have added items to the list using the additem method. But when i tried clearing the listbox using Clear method i cannot find it. i searched for the listbox control's methods and properties using the object browser but cant find Clear in it. Please help.....Access 2003

Thanks
Sassy2009
best answer - posted by ADezii
Just as a sidenote, because of the dynamic Re-Indexing of List Box contents after a RemoveItem has been applied, the following code will always remove all entries in a List Box named [lstTest]:
Expand|Select|Wrap|Line Numbers
  1. Dim intItemsInList As Integer
  2. Dim intCounter As Integer
  3.  
  4. intItemsInList = Me![lstTest].ListCount
  5.  
  6. For intCounter = 0 To intItemsInList - 1
  7.   Me![lstTest].RemoveItem 0
  8. Next
Expert
 
Join Date: Jun 2007
Location: Derbyshire, UK
Posts: 346
#2: 3 Weeks Ago

re: Listbox.clear


Hi

Assumning the list Row Source Type is 'Value List' then perhapse
Expand|Select|Wrap|Line Numbers
  1. lstDisplay.RowSource = ""
??

MTB
NeoPa's Avatar
Administrator
 
Join Date: Oct 2006
Location: London - UK
Posts: 15,672
#3: 3 Weeks Ago

re: Listbox.clear


MTB is on the button with this one.

The opposite of .AddItem though, is .RemoveItem. This enables you to clear a single item from the ListBox.
ADezii's Avatar
Expert
 
Join Date: Apr 2006
Location: Philadelphia
Posts: 5,213
#4: 3 Weeks Ago

re: Listbox.clear


Just as a sidenote, because of the dynamic Re-Indexing of List Box contents after a RemoveItem has been applied, the following code will always remove all entries in a List Box named [lstTest]:
Expand|Select|Wrap|Line Numbers
  1. Dim intItemsInList As Integer
  2. Dim intCounter As Integer
  3.  
  4. intItemsInList = Me![lstTest].ListCount
  5.  
  6. For intCounter = 0 To intItemsInList - 1
  7.   Me![lstTest].RemoveItem 0
  8. Next
NeoPa's Avatar
Administrator
 
Join Date: Oct 2006
Location: London - UK
Posts: 15,672
#5: 3 Weeks Ago

re: Listbox.clear


When dealing with a diminishing set (This also applies to rows in Excel where you may delete some within the loop) I always start from the end and work backwards. It makes for easier and more obvious code (not that there is anything wrong with ADezii's code you understand) :
Expand|Select|Wrap|Line Numbers
  1. Dim intX As Integer
  2.  
  3. With Me.lstTest
  4.   For intX = .ListCount - 1
  5.     Call .RemoveItem(intX)
  6.   Next intX
  7. End With
Reply