Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

ListIndex error 7777 You've used the ListIndex property incorrectly

Posted on 2009-05-11
8
Medium Priority
?
4,814 Views
Last Modified: 2013-11-28
I am trying to running the following code, to select the list box value where there is only one item in the list. However I keep getting the error "7777 You've used the ListIndex property incorrectly". I have observed the following points.

1. I have SetFocus to the control that I have tried set the value for.
2. I have tried to use the selListBox.Selected(0) method, but keep getting the "Object deosn't support this property or method"
3. The selListBox is passed as a variant because it is passed from a collection. I have changed this to selListBox as ListBox - but the same error occurs.
4. Although the error message ""7777 You've used the ListIndex property incorrectly" occurs,  the correct list box item does get selected.

I am using Access 2003. Any suggestions? Thanks
Function SelectOneListItem(selListBox As Variant)
 
If selListBox.ListIndex = -1 Then
    If selListBox.ListCount = 1 Then selListBox.SetFocus: selListBox.ListIndex = 0: selListBox.Selected(0) = True
 
End If
End Function

Open in new window

0
Comment
Question by:HeronandBrearley
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
8 Comments
 
LVL 65

Accepted Solution

by:
rockiroads earned 750 total points
ID: 24353154
Not sure why u need to check the ListIndex
You can use IemData to refer to the first item

Function SelectOneListItem(selListBox As Variant)
 
    If selListBox.ListCount = 1 Then
        selListBox.SetFocus
        selListBox.value = selListBox.itemdata(0)
    End If
End Function

0
 
LVL 85
ID: 24353159
don't use ListIndex; just set the value directly:

selListBox = 0

the Selected method expects a True or False:

selListbox.Selected(0) = True

And I'd encourage you to avoid the colon method of code; place each of your statements on a individual line. It's MUCH easier to read, and will be much appreciated by any developer who comes behind you to work on your app (or by yourself when you must make changes 6 months from now). it's difficult to scan through code and find what you're looking for with code constructed in this manner.
0
 
LVL 85
ID: 24353164
Also, the correct item is "selected" since you're setting the focus to the listbox, which will select the FIRST item (i.e. the 0 item).
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:HeronandBrearley
ID: 24353218
LSMConsulting:

Thanks for the response - after all the swapping and changing code to check if various methods work, I have copied the worng code into my question. - Sorry. In fact my code is:

Function SelectOneListItem(selListBox As Variant)
If selListBox.ListIndex = -1 Then
    If selListBox.ListCount = 1 Then
        selListBox.SetFocus
        selListBox.Selected(0) = True
    End If
End If
End Function

However, I still get : "Object deosn't support this property or method".

Rockiroads:

I have used your selListBox.value = selListBox.itemdata(0) and it works. Seems obvious, now you've said it. Thanks.
0
 
LVL 85
ID: 24353241
rocki's method will work, of course.
0
 
LVL 65

Expert Comment

by:rockiroads
ID: 24353562
With your current code, it would probably work if defined as a listbox

Function SelectOneListItem(selListBox As ListBox)
If selListBox.ListIndex = -1 Then
    If selListBox.ListCount = 1 Then
        selListBox.SetFocus
        selListBox.Selected(0) = True
    End If
End If
End Function
0
 
LVL 65

Expert Comment

by:rockiroads
ID: 24353579
One thing I forgot to add, and the reason why I prefer to use itemdata, is that I dont believe selected sets the listbox value, it just merely highlights an entry. Use of itemdata in setting the value sets both the value and selects item
0
 
LVL 85
ID: 24353692
Yes, the Selected property simply selects that item. Of course, at that point the Listbox would return whatever value is in the bound column for that listbox. IOW, if I select item 2, the listbox would be "set" to whatever value is in the ItemData for Item 2.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows how to get a list of available printers for display in a drop-down list, and then to use the selected printer to print an Access report or a Word document filled with Access data, using different syntax as needed for working with …
Instead of error trapping or hard-coding for non-updateable fields when using QODBC, let VBA automatically disable them when forms open. This way, users can view but not change the data. Part 1 explained how to use schema tables to do this. Part 2 h…
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.
Suggested Courses

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question