PC Review


Reply
Thread Tools Rate Thread

additem removeitem

 
 
Chip Pearson
Guest
Posts: n/a
 
      20th Jan 2008
When you remove items from a ListBox or a ComboBox (or, also, Rows from a
Range), you should work your way from the bottom up, not top down. That is,
your loop should go from ListCount-1 to 0, not from 0 to ListCount-1.
Otherwise, your index will get screwed up as you delete an item. For
example, if you select Item(5) and delete it, what was originally Item(6) is
now Item(5). If you then delete Item(6), you are deleting what was
originally Item(7). All this can be avoided by starting with ListCount-1 and
moving down to 0. E.g.,

Dim N As Long
For N = .ListCount-1 To 0 Step -1
If Something(N) = True Then
.RemoveItem N
End If
Next N


--
Cordially,
Chip Pearson
Microsoft Most Valuable Professional
Excel Product Group, 1998 - 2008
Pearson Software Consulting, LLC
www.cpearson.com
(email on web site)


"BigPig" <(E-Mail Removed)> wrote in message
news:66001489-809A-41CC-B233-(E-Mail Removed)...
> Hi All,
>
> Can't figure out why the following script won't remove selected items
> properly:
>
> With frm_map
>
> Dim X As Integer
> For X = 0 To .lst_map_prop_for_trans.ListCount - 1
> On Error Resume Next
> If .lst_map_prop_for_trans.Selected(X) = True Then
> .lst_map_trans_to.AddItem .lst_map_prop_for_trans.List(X, 0)
> .lst_map_trans_to.List(.lst_map_trans_to.ListCount - 1, 0) =
> .lst_map_prop_for_trans.List(X, 0)
> .lst_map_trans_to.List(.lst_map_trans_to.ListCount - 1, 1) =
> .lst_map_prop_for_trans.List(X, 1)
> .lst_map_trans_to.List(.lst_map_trans_to.ListCount - 1, 2) =
> .lst_map_prop_for_trans.List(X, 2)
> .lst_map_trans_to.List(.lst_map_trans_to.ListCount - 1, 3) =
> .lst_map_prop_for_trans.List(X, 3)
> .lst_map_prop_for_trans.RemoveItem (X)
> End If
>
> Next X
> X = X + 1
>
> End With
>
> Other info: both listboxes are in separate frames (in the same form) and
> are
> set to multiselect. The issue is that when I select one item to "transfer"
> it
> works, but with two or more items selected the first or half of the items
> selected will move, but not the second or second half of the items
> selected.
>
> Any and all advice would be greatly appreciated.
>
> Thank you.


 
Reply With Quote
 
 
 
 
BigPig
Guest
Posts: n/a
 
      20th Jan 2008
Hi All,

Can't figure out why the following script won't remove selected items
properly:

With frm_map

Dim X As Integer
For X = 0 To .lst_map_prop_for_trans.ListCount - 1
On Error Resume Next
If .lst_map_prop_for_trans.Selected(X) = True Then
.lst_map_trans_to.AddItem .lst_map_prop_for_trans.List(X, 0)
.lst_map_trans_to.List(.lst_map_trans_to.ListCount - 1, 0) =
..lst_map_prop_for_trans.List(X, 0)
.lst_map_trans_to.List(.lst_map_trans_to.ListCount - 1, 1) =
..lst_map_prop_for_trans.List(X, 1)
.lst_map_trans_to.List(.lst_map_trans_to.ListCount - 1, 2) =
..lst_map_prop_for_trans.List(X, 2)
.lst_map_trans_to.List(.lst_map_trans_to.ListCount - 1, 3) =
..lst_map_prop_for_trans.List(X, 3)
.lst_map_prop_for_trans.RemoveItem (X)
End If

Next X
X = X + 1

End With

Other info: both listboxes are in separate frames (in the same form) and are
set to multiselect. The issue is that when I select one item to "transfer" it
works, but with two or more items selected the first or half of the items
selected will move, but not the second or second half of the items selected.

Any and all advice would be greatly appreciated.

Thank you.
 
Reply With Quote
 
BigPig
Guest
Posts: n/a
 
      20th Jan 2008
I'm sorry I mis-spoke

I meant to say that the script will remove the first item or first half of
items selected, versus all of them.

Sorry.

"BigPig" wrote:

> Hi All,
>
> Can't figure out why the following script won't remove selected items
> properly:
>
> With frm_map
>
> Dim X As Integer
> For X = 0 To .lst_map_prop_for_trans.ListCount - 1
> On Error Resume Next
> If .lst_map_prop_for_trans.Selected(X) = True Then
> .lst_map_trans_to.AddItem .lst_map_prop_for_trans.List(X, 0)
> .lst_map_trans_to.List(.lst_map_trans_to.ListCount - 1, 0) =
> .lst_map_prop_for_trans.List(X, 0)
> .lst_map_trans_to.List(.lst_map_trans_to.ListCount - 1, 1) =
> .lst_map_prop_for_trans.List(X, 1)
> .lst_map_trans_to.List(.lst_map_trans_to.ListCount - 1, 2) =
> .lst_map_prop_for_trans.List(X, 2)
> .lst_map_trans_to.List(.lst_map_trans_to.ListCount - 1, 3) =
> .lst_map_prop_for_trans.List(X, 3)
> .lst_map_prop_for_trans.RemoveItem (X)
> End If
>
> Next X
> X = X + 1
>
> End With
>
> Other info: both listboxes are in separate frames (in the same form) and are
> set to multiselect. The issue is that when I select one item to "transfer" it
> works, but with two or more items selected the first or half of the items
> selected will move, but not the second or second half of the items selected.
>
> Any and all advice would be greatly appreciated.
>
> Thank you.

 
Reply With Quote
 
Rick Rothstein \(MVP - VB\)
Guest
Posts: n/a
 
      20th Jan 2008
Try removing them the in reverse order...

For X = .lst_map_prop_for_trans.ListCount - 1 To 0 Step -1

Rick


"BigPig" <(E-Mail Removed)> wrote in message
news:66001489-809A-41CC-B233-(E-Mail Removed)...
> Hi All,
>
> Can't figure out why the following script won't remove selected items
> properly:
>
> With frm_map
>
> Dim X As Integer
> For X = 0 To .lst_map_prop_for_trans.ListCount - 1
> On Error Resume Next
> If .lst_map_prop_for_trans.Selected(X) = True Then
> .lst_map_trans_to.AddItem .lst_map_prop_for_trans.List(X, 0)
> .lst_map_trans_to.List(.lst_map_trans_to.ListCount - 1, 0) =
> .lst_map_prop_for_trans.List(X, 0)
> .lst_map_trans_to.List(.lst_map_trans_to.ListCount - 1, 1) =
> .lst_map_prop_for_trans.List(X, 1)
> .lst_map_trans_to.List(.lst_map_trans_to.ListCount - 1, 2) =
> .lst_map_prop_for_trans.List(X, 2)
> .lst_map_trans_to.List(.lst_map_trans_to.ListCount - 1, 3) =
> .lst_map_prop_for_trans.List(X, 3)
> .lst_map_prop_for_trans.RemoveItem (X)
> End If
>
> Next X
> X = X + 1
>
> End With
>
> Other info: both listboxes are in separate frames (in the same form) and
> are
> set to multiselect. The issue is that when I select one item to "transfer"
> it
> works, but with two or more items selected the first or half of the items
> selected will move, but not the second or second half of the items
> selected.
>
> Any and all advice would be greatly appreciated.
>
> Thank you.


 
Reply With Quote
 
Dave Peterson
Guest
Posts: n/a
 
      20th Jan 2008
First, I'd remove the "On error resume next" line. It can be hiding the error
from you.

Second start at the bottom of the list and work your way to the top:

For X = .lst_map_prop_for_trans.ListCount - 1 to 0 step -1



BigPig wrote:
>
> Hi All,
>
> Can't figure out why the following script won't remove selected items
> properly:
>
> With frm_map
>
> Dim X As Integer
> For X = 0 To .lst_map_prop_for_trans.ListCount - 1
> On Error Resume Next
> If .lst_map_prop_for_trans.Selected(X) = True Then
> .lst_map_trans_to.AddItem .lst_map_prop_for_trans.List(X, 0)
> .lst_map_trans_to.List(.lst_map_trans_to.ListCount - 1, 0) =
> .lst_map_prop_for_trans.List(X, 0)
> .lst_map_trans_to.List(.lst_map_trans_to.ListCount - 1, 1) =
> .lst_map_prop_for_trans.List(X, 1)
> .lst_map_trans_to.List(.lst_map_trans_to.ListCount - 1, 2) =
> .lst_map_prop_for_trans.List(X, 2)
> .lst_map_trans_to.List(.lst_map_trans_to.ListCount - 1, 3) =
> .lst_map_prop_for_trans.List(X, 3)
> .lst_map_prop_for_trans.RemoveItem (X)
> End If
>
> Next X
> X = X + 1
>
> End With
>
> Other info: both listboxes are in separate frames (in the same form) and are
> set to multiselect. The issue is that when I select one item to "transfer" it
> works, but with two or more items selected the first or half of the items
> selected will move, but not the second or second half of the items selected.
>
> Any and all advice would be greatly appreciated.
>
> Thank you.


--

Dave Peterson
 
Reply With Quote
 
BigPig
Guest
Posts: n/a
 
      20th Jan 2008
Chip,

Thank you very much! And for the great explanation.
 
Reply With Quote
 
BigPig
Guest
Posts: n/a
 
      20th Jan 2008
Rick,

Just read your response, thank you!

"Rick Rothstein (MVP - VB)" wrote:

> Try removing them the in reverse order...
>
> For X = .lst_map_prop_for_trans.ListCount - 1 To 0 Step -1
>
> Rick
>
>
> "BigPig" <(E-Mail Removed)> wrote in message
> news:66001489-809A-41CC-B233-(E-Mail Removed)...
> > Hi All,
> >
> > Can't figure out why the following script won't remove selected items
> > properly:
> >
> > With frm_map
> >
> > Dim X As Integer
> > For X = 0 To .lst_map_prop_for_trans.ListCount - 1
> > On Error Resume Next
> > If .lst_map_prop_for_trans.Selected(X) = True Then
> > .lst_map_trans_to.AddItem .lst_map_prop_for_trans.List(X, 0)
> > .lst_map_trans_to.List(.lst_map_trans_to.ListCount - 1, 0) =
> > .lst_map_prop_for_trans.List(X, 0)
> > .lst_map_trans_to.List(.lst_map_trans_to.ListCount - 1, 1) =
> > .lst_map_prop_for_trans.List(X, 1)
> > .lst_map_trans_to.List(.lst_map_trans_to.ListCount - 1, 2) =
> > .lst_map_prop_for_trans.List(X, 2)
> > .lst_map_trans_to.List(.lst_map_trans_to.ListCount - 1, 3) =
> > .lst_map_prop_for_trans.List(X, 3)
> > .lst_map_prop_for_trans.RemoveItem (X)
> > End If
> >
> > Next X
> > X = X + 1
> >
> > End With
> >
> > Other info: both listboxes are in separate frames (in the same form) and
> > are
> > set to multiselect. The issue is that when I select one item to "transfer"
> > it
> > works, but with two or more items selected the first or half of the items
> > selected will move, but not the second or second half of the items
> > selected.
> >
> > Any and all advice would be greatly appreciated.
> >
> > Thank you.

>
>

 
Reply With Quote
 
BigPig
Guest
Posts: n/a
 
      20th Jan 2008
Hi Dave,

Thank you for your response! I hadn't thought about the "On error..."

Thank you.

"Dave Peterson" wrote:

> First, I'd remove the "On error resume next" line. It can be hiding the error
> from you.
>
> Second start at the bottom of the list and work your way to the top:
>
> For X = .lst_map_prop_for_trans.ListCount - 1 to 0 step -1
>
>
>
> BigPig wrote:
> >
> > Hi All,
> >
> > Can't figure out why the following script won't remove selected items
> > properly:
> >
> > With frm_map
> >
> > Dim X As Integer
> > For X = 0 To .lst_map_prop_for_trans.ListCount - 1
> > On Error Resume Next
> > If .lst_map_prop_for_trans.Selected(X) = True Then
> > .lst_map_trans_to.AddItem .lst_map_prop_for_trans.List(X, 0)
> > .lst_map_trans_to.List(.lst_map_trans_to.ListCount - 1, 0) =
> > .lst_map_prop_for_trans.List(X, 0)
> > .lst_map_trans_to.List(.lst_map_trans_to.ListCount - 1, 1) =
> > .lst_map_prop_for_trans.List(X, 1)
> > .lst_map_trans_to.List(.lst_map_trans_to.ListCount - 1, 2) =
> > .lst_map_prop_for_trans.List(X, 2)
> > .lst_map_trans_to.List(.lst_map_trans_to.ListCount - 1, 3) =
> > .lst_map_prop_for_trans.List(X, 3)
> > .lst_map_prop_for_trans.RemoveItem (X)
> > End If
> >
> > Next X
> > X = X + 1
> >
> > End With
> >
> > Other info: both listboxes are in separate frames (in the same form) and are
> > set to multiselect. The issue is that when I select one item to "transfer" it
> > works, but with two or more items selected the first or half of the items
> > selected will move, but not the second or second half of the items selected.
> >
> > Any and all advice would be greatly appreciated.
> >
> > Thank you.

>
> --
>
> Dave Peterson
>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Listbox RemoveItem Raymond Clarke Jr Microsoft Access Form Coding 1 13th May 2009 06:52 PM
removeitem in listbox =?Utf-8?B?RGF2ZQ==?= Microsoft Access 2 5th Apr 2007 10:38 PM
ListBox using RemoveItem =?Utf-8?B?R3VzIENodWNo?= Microsoft Access VBA Modules 2 17th Mar 2006 02:47 PM
Listbox .additem / .removeitem ? kelly draper via AccessMonster.com Microsoft Access Form Coding 4 4th Jun 2005 12:40 AM
.RemoveItem Methode Marcel Stoop Microsoft Access 4 11th Mar 2005 12:53 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 04:04 AM.