Move ListBox Items Up/down

  • Thread starter Daama via AccessMonster.com
  • Start date
D

Daama via AccessMonster.com

Hi All,

I am using the code below to move up the items in a list box named
lstSelected
(for the move down, I just use the reverse )

But I keep getting an error message pointing to the word (.list) that is in
the line:
tmp = lstSelected.List(intg)

The message say: "Compile Error: Method or data number not found"

Can anyone help me figure out what is wrong with the code.

P.S. I also check some of the solutions posted, I keep running on the same
problem.

Thanks,

Daama


Dim tmp As String
Dim intg As Integer

index = lstSelected.ListIndex

'something's selected
If intg > -1 Then

'and its not at the top already
If intg > 0 Then


tmp = lstSelected.List(intg)
lstSelected.RemoveItem intg
lstSelected.AddItem tmp, intg - 1
lstSelected.Selected(intg - 1) = True
lstSelected.SetFocus


End If


End If


End Sub
 
O

OfficeDev18 via AccessMonster.com

Daama,

Is that a typo

index = lstSelected.ListIndex

or did you mean to say

intg= lstSelected.ListIndex?

As for the line tmp = lstSelected.List(intg), there is no such member, ".
List". It's either ListCount or ListIndex. You obviously mean to say tmp =
lstSelected.ListIndex(intg)

Hope this helps,

Sam
 
D

Daama via AccessMonster.com

Sam,
I made some changes. Now it moves itesm upward. But it changes the name of
item it starts from by assigning it the same name as the listbox that items
are in. for instance, it had items listed as:

One
Two
Three
lstSelected

and we start from the last one. After 3 clicks I will have 4 items all named
lstSelected.

Do you have an idea why

Below is the code

Daama



Private Sub btnMOveUp_Click()

'move up items in the list box one at a time

Dim strg As String
Dim intg As Integer

intg = lstSelected.ListIndex

'something's selected
If intg > -1 Then

'and its not at the top already
If intg > 0 Then

strg = lstSelected.Name
lstSelected.RemoveItem intg
lstSelected.AddItem strg, intg - 1
lstSelected.Selected(intg - 1) = True
lstSelected.SetFocus

End If

End If



End Sub














Daama,

Is that a typo

index = lstSelected.ListIndex

or did you mean to say

intg= lstSelected.ListIndex?

As for the line tmp = lstSelected.List(intg), there is no such member, ".
List". It's either ListCount or ListIndex. You obviously mean to say tmp =
lstSelected.ListIndex(intg)

Hope this helps,

Sam
[quoted text clipped - 39 lines]
 
O

OfficeDev18 via AccessMonster.com

Daama,

The first lines of your code confuse me.

intg = lstSelected.ListIndex

'something's selected
If intg > -1 Then
..
..
..
"intg = lstSelected.ListIndex" This line simply initializes intg to 0. The
ListIndex of a listbox, when first visited, is always 0. Why not simply say
"intg = 0"?

" If intg > -1 Then" This next executable line is even more confusing. If
intg starts at 0, it MUST BE greater than -1. Why the If statement?

If, as I think you mean, you are trying to determine if an item is selected
in the listbox, you need to access the lstSelected.Selected property, and
then have code like this;

If lstSelected.Selected Then
intg=lst.Selected.ListIndex
End If

Hope this helps,

Sam
Sam,
I made some changes. Now it moves itesm upward. But it changes the name of
item it starts from by assigning it the same name as the listbox that items
are in. for instance, it had items listed as:

One
Two
Three
lstSelected

and we start from the last one. After 3 clicks I will have 4 items all named
lstSelected.

Do you have an idea why

Below is the code

Daama


Private Sub btnMOveUp_Click()

'move up items in the list box one at a time

Dim strg As String
Dim intg As Integer

intg = lstSelected.ListIndex

'something's selected
If intg > -1 Then

'and its not at the top already
If intg > 0 Then

strg = lstSelected.Name
lstSelected.RemoveItem intg
lstSelected.AddItem strg, intg - 1
lstSelected.Selected(intg - 1) = True
lstSelected.SetFocus

End If

End If

End Sub
[quoted text clipped - 19 lines]
 
D

Douglas J. Steele

ListIndex will be -1 if nothing's selected. If something's selected,
ListIndex will contain the index number of the last item selected (in other
words, if you've selected the first item, it'll be 0. If you multi-selected
the 1st item, then the 4th item, then the 2nd item, it'll be 1, since you
selected the 2nd item last.)

It's not actually necessary to check the Selected property of a given row to
determine whether or not it's selected: the indexes of the selected items
are contained in the list box's ItemsSelected collection.

--
Doug Steele, Microsoft Access MVP

(no private e-mails, please)


OfficeDev18 via AccessMonster.com said:
Daama,

The first lines of your code confuse me.

intg = lstSelected.ListIndex

'something's selected
If intg > -1 Then
.
.
.
"intg = lstSelected.ListIndex" This line simply initializes intg to 0. The
ListIndex of a listbox, when first visited, is always 0. Why not simply
say
"intg = 0"?

" If intg > -1 Then" This next executable line is even more confusing. If
intg starts at 0, it MUST BE greater than -1. Why the If statement?

If, as I think you mean, you are trying to determine if an item is
selected
in the listbox, you need to access the lstSelected.Selected property, and
then have code like this;

If lstSelected.Selected Then
intg=lst.Selected.ListIndex
End If

Hope this helps,

Sam
Sam,
I made some changes. Now it moves itesm upward. But it changes the name of
item it starts from by assigning it the same name as the listbox that
items
are in. for instance, it had items listed as:

One
Two
Three
lstSelected

and we start from the last one. After 3 clicks I will have 4 items all
named
lstSelected.

Do you have an idea why

Below is the code

Daama


Private Sub btnMOveUp_Click()

'move up items in the list box one at a time

Dim strg As String
Dim intg As Integer

intg = lstSelected.ListIndex

'something's selected
If intg > -1 Then

'and its not at the top already
If intg > 0 Then

strg = lstSelected.Name
lstSelected.RemoveItem intg
lstSelected.AddItem strg, intg - 1
lstSelected.Selected(intg - 1) = True
lstSelected.SetFocus

End If

End If

End Sub
[quoted text clipped - 19 lines]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top