Solved

ListIndex error 7777 You've used the ListIndex property incorrectly

Posted on 2009-05-11
8
4,521 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 250 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 84
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 84
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
Independent Software Vendors: 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 84
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 84
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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

739 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