Solved

ListIndex error 7777 You've used the ListIndex property incorrectly

Posted on 2009-05-11
8
4,580 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 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
Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

 

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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

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…
Traditionally, the method to display pictures in Access forms and reports is to first download them from URLs to a folder, record the path in a table and then let the form or report pull the pictures from that folder. But why not let Windows retr…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.

734 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