Itemsselected.count reports wrong figure

Heathy

Registered User.
Local time
Today, 08:58
Joined
Jul 6, 2006
Messages
17
Hi. I have a listbox which is Extended Multiselect. I have code that run based on the click of a command button, the code run through all itemselected.

However, when the command button is clicked the number of itemselected.count is one more that that selected.

I have simplified the code, to just one line which still reports the wrong result.

MsgBox (Me!Stk_Pals.ItemsSelected.Count)

Anyone come accross this problem before? I certainly have not. This problem has just started happending the code has been running fine until recently.

Any ideas, would be greatly appreciated.

Access 2007

Thanks.
 
Last edited:
If you just select one item from the list then was does it return? if the result is consitant then simply add -1 from the count.

David
 
Thanks for reply David.

However, I didn't mention the results are not consistant. The reported figure changes, sometimes giving the correct result and not in other cases. Very strange.
 
It appears, I may be getting closer to solving this, the problem looks like it is only happending when the shift key is used in conjuntion with the control key to select multiple items, when the control key is used by itself the code performs correctly.

Any further ideas would be great.

Thanks in advance.
 
You could do the count by looping threw the list count

Code:
For x = 0 to Me.Listbox.Listcount-1
   If Me.Listbox.Selected(x) = True
      Cnt = Cnt + 1
   End If
Next


David
 
Thanks again.

On looking at your code I thought, yes good workaround and tried it alongside the original test code:-

Private Sub Test_Click()
Dim x As Long, cnt As Long
cnt = 0
MsgBox (Me![stk pals].ItemsSelected.Count)
For x = 0 To Me.[stk pals].ListCount - 1
If Me.[stk pals].Selected(x) = True Then
cnt = cnt + 1
End If
Next
MsgBox (cnt)
End Sub

I have attached at jpg of screen output.

I am at a loss, as to why this is happening.

Both msgbox command return the same result, it definately looks as if it is only when using the shift key in the extended select listbox.

Stuart.
 

Attachments

  • Example.jpg
    Example.jpg
    68.7 KB · Views: 175
Ok next step is to find out which items it thinks are selected.

For x = 0 To Me.[stk pals].ListCount - 1
Code:
If Me.[stk pals].Selected(x) = True Then
    Debug.Print Me.[Stk Pals].Column(x,0)
    cnt = cnt + 1
End If

Then go into the immediate window to see which items have been printed.

if it all comes out the same swap round the x,0 to 0,x (not sure off hand which way round it is.

X = the index number
0 = column number (remember columns start at zero)

David
 
i saw that in an app i did some time ago

i showed a selected items count at the top, and it reported 1 more than were really selected.

I had to use

="Items Available: " & [mylist].[ListCount]-1

to get the correct total


This was A2003
 
Last edited:
I suspect it may have something to do with having the column headings set to yes in the properties. May be wrong but you never know.

David
 
could be to do with the header -

- i cant think I have used this anywhere else ever - so I dont know generally - processing the items via the itemsselected collection (I think thats the one from memory) worked ok anyway - it was just the count that was wrong
 
Thanks again for your responses.

Attached are screenshots of your suggestion David.

I have checked the listbox, there is no code associated with it, I have removed the headings and changes it from extended to simple and back again.

The problem does seem only to happen when using the shift and control key, the actual items selected are not those you would expect based on the keystrokes.

Also my inital post was misleading the number of items selected is not always one more than those showing as selected it depends on the behavoir of the list box during selection.

Thanks for your help.
 

Attachments

  • examplw1.jpg
    examplw1.jpg
    99.8 KB · Views: 148
  • example2.jpg
    example2.jpg
    30.1 KB · Views: 138
There are three tests to perform

Prior to this set up a command button that retrieves the items selected value as per the above code. And cal a msgbox to retunr the values

Test
Do not select any items -
Select 1 item
select more than 1

What is returned in each example

David
 
just out of interest for the OP - the correct code to process the selected items in a multi-select list box is this (this is slightly modified from my actual code, and may not be the exactly correct syntax. I think this shows just the bound column of the list box - I am not sure without checking how to code for other columns of the itemdata object)


Private Sub processitems()
Dim item As Variant

for Each item In mylist.ItemsSelected
msgbox(mylist.ItemData(item))
Next item
End Sub
 
Last edited:
I have attached ascreen shot without the header row on the listbox, to show that the problem still occurs.

I have done as you suggested and check the results under each set of circumstanses.

If I just select row 1, correct results.

control and row 2, correct results.

In fact all rows using just control returned correct results.

If I select row 1 and then shift and row 5, returns correct results.

If i then press control and select row 3.

Only rows 1 and 2 are hightlighted ?????

And code reurns data from 1,2,4 & 5.

Which as you know is the expected result, if the rows selected had shown correctly.

Very odd.

BTW. Thanks for your input gemma the husky, my original code was similar to that in your post, just trying differenct angles to resolve the issue. Thanks again.
 

Attachments

  • expample4.jpg
    expample4.jpg
    33.6 KB · Views: 163
i just tried with mine, and didnt get any issues at all

what events are you managing in the listbox - maybe there is something going on there, as you select items.

I thought this happened with clicks

shift turns on/off a range
control turns on/off individual items
 
That is correct.

What version of Access are you using, I am using 2007. There is no code/macros associated with the list box.
 
I have now, installed office 2007 sp2, but still no good. Thanks for all your help.
 
I can't find it, but I could have sworn there was either a hot fix for this or a service pack that addressed this problem.
 
Sometimes with multi-select list boxes an item may still be selected if it's got the dotted lines around the item's edges (even without it being highlighted). It happens sometimes after you requery the list box. Try these steps (in vba):

1. Set the value of the list box to null
2. Set the row source to null
3. Requery
4. Set back its row source
5. Requery

I never use the ItemsSelected collection, the other method from DCrake never fails. Saying that, it could also be that your list box is corrupt - could be when you copy and paste from another form. Was the db originally created in an older version and then to 2007?
 

Users who are viewing this thread

Back
Top Bottom